1
0
Fork 0
mirror of https://gitlab.com/bramw/baserow.git synced 2025-01-30 10:26:29 +00:00
bramw_baserow/docs/patterns/date-and-time.md
Petr Stribny 2977bc43c8 Django 4
2023-12-22 10:58:36 +00:00

1.3 KiB

Dates and times

Backend

Time zones

  • Use datetime.timezone.utc to represent UTC timezone
  • Use zoneinfo.ZoneInfo object to represent any other timezone
  • Use datetime.tzinfo as a type for a timezone
  • Use datetime.now with tz set to UTC to get "now" datetime in UTC timezone
  • Use baserow.core.datetime.get_timezones to get the list of all available timezones
from zoneinfo import ZoneInfo
from datetime import datetime, timezone, tzinfo

# get a reference to UTC time zone
utc_timezone: tzinfo = timezone.utc

# get a reference to any other time zone
prague_timezone: tzinfo = ZoneInfo("Europe/Prague")

# create a new datetime in a timezone
my_datetime = datetime(..., tzinfo=timezone.utc)

# set a timezone to a datetime
my_datetime = my_datetime.replace(tzinfo=ZoneInfo("Europe/Prague"))

# convert a datetime into another timezone, preserving time in UTC
my_datetime = my_datetime.astimezone(tz=ZoneInfo("Europe/Prague"))

# get now in UTC
now = datetime.now(tz=timezone.utc)

# get all timezones
from baserow.core.datetime import get_timezones
all_timezones = get_timezones()

References