Skip to content

Datetime basics

Timeless dates and datetimes are represented by the Datetime class. All Timeless objects are assumed to be UTC if any other timezone is specified.

1
2
3
4
5
6
7
import timeless


dt = timeless.datetime(1994, 2, 26)

dt.zone  # => 'UTC'
dt.tzinfo  # => zoneinfo.ZoneInfo(key='UTC')

Timezones

Note that Timeless make use of the standard module zoneinfo to handle timezones. Said that, you can set any system timezone you want or any other listed at tzdata.

1
2
3
4
import timeless


timeless.datetime(1994, 2, 26, hour=11, minute=15, zone="America/Sao_Paulo")

Getting the current time

One can also use the convenience methods now and today to create a Datetime object. Since Timeless doesn't differentiate between datetime and date objects, now and today are almost equivalent, despite values other than day, month and year being zero on when using today.

1
2
3
4
5
6
7
import timeless


today = timeless.today()

# it's possible to use the `zone` keyword argument to specify the timezone
now = timeless.now(zone="America/Sao_Paulo")

Replacing object values

There are two ways to replace the values of a Datetime object.

If you just want to zero out hour, minute, second and microsecond, you can use the set_zero method.

1
2
3
4
5
6
7
8
9
import timeless


dt = timeless.datetime(1994, 2, 26, hour=11, minute=15, zone="America/Sao_Paulo")
dt.set_zero()
# Datetime(1994, 2, 26, 0, 0, tzinfo=zoneinfo.ZoneInfo(key='UTC'))

# or use the equivalent property:
dt.zero

However, if you want to replace other values of a Datetime object, you can use the set method.

1
2
3
4
5
6
import timeless


dt = timeless.datetime(1994, 2, 26, hour=11, minute=15, zone="America/Sao_Paulo")
dt.set(year=2100, zone="America/Sao_Paulo")
# Datetime(2100, 2, 26, 11, 15, tzinfo=zoneinfo.ZoneInfo(key='America/Sao_Paulo'))