Python timestamp with timezones

Because pytz isn't used anymore and I needed to add a timezone to a timestamp, I looked into using only the datetime module of Python.

>>> import datetime
>>>
>>> # based on 1970-01-01 00:00 UTC
>>> timestamp = 1643104698.5201788
>>>
>>> # using the local timezone (Europe/Berlin in my case)
>>> datetime.datetime.fromtimestamp(timestamp)
datetime.datetime(2022, 1, 25, 10, 58, 18, 520179)
>>>
>>> # with UTC as timezone
>>> datetime.datetime.utcfromtimestamp(timestamp)
datetime.datetime(2022, 1, 25, 9, 58, 18, 520179)
>>>
>>> # with timezone set to Europe/Berlin (needs Python 3.9 for ZoneInfo)
>>> from zoneinfo import ZoneInfo
>>> tz = ZoneInfo('Europe/Berlin')
>>> datetime.datetime.fromtimestamp(timestamp, tz)
datetime.datetime(2022, 1, 25, 10, 58, 18, 520179, tzinfo=zoneinfo.ZoneInfo(key='Europe/Berlin'))
>>>
>>> # with timezone set to UTC
>>> datetime.datetime.fromtimestamp(timestamp, datetime.timezone.utc)
datetime.datetime(2022, 1, 25, 9, 58, 18, 520179, tzinfo=datetime.timezone.utc)

In my application I used the last one because I don't need any information about Europe/Berlin.

Show Diff with Copy of Destination Text

For a web application I needed to show the difference between two texts.
First I tried calculating this in the backend (Python) but using fast-diff in Javascript was easier.
The important part here was the possibility to mark and copy only the destination part of the difference.

The result of a diff call is a list of tuples: "equal (0), insert (+1), delete (-1)" and the character at this position.
Now every change is added with either <span> or with <del>.
All <del> segements are not part of the marking when copied.
The color classes are for Bulma (yellow background for inserts, red strikethrough for deletes).

Example code:
var difference = diff("abc", "ace");
// result: [[0, 'a'], [-1, 'b'], [0, 'c'], [1, 'e']]
var r = ""
difference.forEach(e => {
  if (e[0] == 0) {
    r = r + e[1];
  } else if (e[0] == 1) {
    r = r + '<span class="has-background-warning">' + e[1] + "</span>";
  } else if (e[0] == -1) {
    r = r + '<del class="has-text-danger">' + e[1] + "</del>";
  }
});
console.log(r);
// "a<del class="has-text-danger">b</del>c<span class="has-background-warning">e</span>"
Example rendered:

diff-copy-example

The first line is the rendered string and the second line is the same rendered string marked. Important here, the <del> part is not part of the marking.

hledger: track if BahnCard was worth it

In November 2021 I got offered a 30€ discount on the BahnCard25 from the Deutsche Bahn AG. Some time ago I cancelled my BahnCard because I wasn't using it enough to justify having one. With a 30€ discount I should have no problem getting the remaining 25.7€ of value back.

But I wanted to track this. I am using hledger to track all my expenses and assets.

The ledger statement for the Bahncard looks like this:

2021-11-24 BahnCard 25
Expenses:misc:Bahncard                     €25.7
Assets:Checking Account
(worthIt:Bahncard2022)                     €55.7  ; full price
(worthIt:Bahncard2022)                      €-30  ; discount

And one statement of the travels looks like this:

2022-01-04 Travel S->KA
Expenses:Travel:Deutsche Bahn             €10.65
Assets:Checking Account
(worthIt:Bahncard2022)                    -€3.55

Result of my current balance:

hledger reg ^worthIt:Bahncard2022

Date

Description

Account

Amount

Balance

2021-11-24

BahnCard 25

(worthIt:Bahncard2022)

€55.70

€55.70

(worthIt:Bahncard2022)

€-30.00

€25.70

2022-01-04

Travel S->KA

(worthIt:Bahncard2022)

€-3.55

€22.15

2022-01-04

Travel KA->S

(worthIt:Bahncard2022)

€-3.55

€18.60

2022-01-06

Travel S->KA

(worthIt:Bahncard2022)

€-3.55

€15.05

2022-01-06

Travel KA->S

(worthIt:Bahncard2022)

€-3.55

€11.50


Now I have about 10 months to use my BahnCard25 and save the remaining 11.50€.
That should be possible. :)