Skip to content

DateTime

DateTime is a class-level utility that provides a consistent, timezone-aware API for all date and time operations in the framework. It manages a configurable default timezone and locale so that every method — from creating instants to formatting output — produces results in the expected context without requiring you to pass timezone arguments everywhere.

All methods are class methods. There is no need to instantiate DateTime; call methods directly on the class.

from orionis.support.time.datetime import DateTime

The class exposes three prefixed methods that are reserved for the framework’s internal bootstrap process:

MethodEffect
_loadConfig(timezone?, locale?)Overwrites the default timezone and locale for every subsequent call
_setTimezone(name)Replaces the default timezone globally
_setLocale(code)Replaces the default locale globally

Returns a standard-library ZoneInfo object for the configured timezone, useful when interfacing with code that expects zoneinfo types:

from zoneinfo import ZoneInfo
zi = DateTime.getZoneinfo() # ZoneInfo(key='Europe/Berlin')

Every instant method accepts an optional tz parameter. When omitted, the configured default timezone is used.

Returns the current date and time:

DateTime.now() # current moment in default tz
DateTime.now(tz="Asia/Singapore") # current moment in Singapore

Returns today’s date at midnight (00:00:00):

DateTime.today()
DateTime.today(tz="Australia/Sydney")

Return the date one day ahead or behind, respectively:

DateTime.tomorrow()
DateTime.yesterday(tz="Pacific/Auckland")

Factory method that creates a specific datetime with individual components. Only year is required; all other components default to their minimum:

DateTime.datetime(2024, 6, 15, 10, 30, 45)
# 2024-06-15 10:30:45 in the default timezone
DateTime.datetime(2024, tz="Europe/Madrid")
# 2024-01-01 00:00:00 in Madrid

Parses an ISO 8601 date string and converts it to the target timezone:

dt = DateTime.parse("2024-06-15T12:00:00+00:00")
# Converted to the default timezone
dt = DateTime.parse("2024-01-01T00:00:00+00:00", tz="Asia/Tokyo")
# Converted to Tokyo

Converts a Unix timestamp to a datetime:

dt = DateTime.fromTimestamp(0.0)
# 1970-01-01 00:00:00 UTC
dt = DateTime.fromTimestamp(1718400000, tz="America/Chicago")

Converts a standard-library datetime or an existing DateTime instance to the target timezone. Raises TypeError for unsupported types:

from datetime import datetime, timezone
# Naive datetime — assumed to be in the configured timezone
dt = DateTime.fromDatetime(datetime(2024, 3, 15, 10, 30))
# Aware datetime — converted to the configured timezone
aware = datetime(2024, 3, 15, 10, 30, tzinfo=timezone.utc)
dt = DateTime.fromDatetime(aware)
# With explicit target timezone
dt = DateTime.fromDatetime(aware, tz="Asia/Seoul")

Accepts a string, standard-library datetime, or DateTime instance and converts it to the configured timezone:

dt = DateTime.convertToLocal("2024-06-15T12:00:00+00:00")
dt = DateTime.convertToLocal(datetime(2024, 6, 15, 12, 0, 0))

Formats a datetime as a string. Defaults to YYYY-MM-DD HH:mm:ss. When no datetime is provided, formats the current moment:

dt = DateTime.datetime(2024, 6, 15, 10, 30, 45, tz="UTC")
DateTime.formatLocal(dt)
# "2024-06-15 10:30:45"
DateTime.formatLocal(dt, format_string="YYYY/MM/DD")
# "2024/06/15"
DateTime.formatLocal()
# current datetime formatted with the default pattern

Boundary methods snap a datetime to the start or end of a time period. When called without a dt argument, they use the current datetime.

ref = DateTime.datetime(2024, 6, 12, 14, 30, 45, tz="UTC")
DateTime.startOfDay(ref) # 2024-06-12 00:00:00
DateTime.endOfDay(ref) # 2024-06-12 23:59:59
DateTime.startOfDay() # today at 00:00:00
DateTime.endOfDay() # today at 23:59:59

Weeks start on Monday and end on Sunday:

DateTime.startOfWeek(ref) # Monday 00:00:00
DateTime.endOfWeek(ref) # Sunday 23:59:59
DateTime.startOfMonth(ref) # 2024-06-01 00:00:00
DateTime.endOfMonth(ref) # 2024-06-30 23:59:59
DateTime.startOfYear(ref) # 2024-01-01 00:00:00
DateTime.endOfYear(ref) # 2024-12-31 23:59:59

Arithmetic methods accept negative values to move backward in time.

ref = DateTime.datetime(2024, 1, 1, 12, 0, 0, tz="UTC")
DateTime.addDays(ref, 5) # 2024-01-06
DateTime.addDays(ref, -3) # 2023-12-29
DateTime.addHours(ref, 3) # 15:00
DateTime.addHours(ref, 14) # next day 02:00
DateTime.addMinutes(ref, 30) # 12:30
DateTime.addMinutes(ref, 90) # 13:30

Returns the absolute day difference between two datetimes:

a = DateTime.datetime(2024, 1, 1, tz="UTC")
b = DateTime.datetime(2024, 1, 11, tz="UTC")
DateTime.diffInDays(a, b) # 10

Returns the absolute hour difference:

a = DateTime.datetime(2024, 1, 1, 12, 0, 0, tz="UTC")
b = DateTime.addHours(a, 6)
DateTime.diffInHours(a, b) # 6

Returns True for Saturday or Sunday. Without arguments, checks the current date:

saturday = DateTime.datetime(2024, 6, 8, tz="UTC")
monday = DateTime.datetime(2024, 6, 10, tz="UTC")
DateTime.isWeekend(saturday) # True
DateTime.isWeekend(monday) # False
DateTime.isWeekend() # depends on the current day
DateTime.isToday(DateTime.now()) # True
DateTime.isToday(DateTime.yesterday()) # False
future = DateTime.addDays(DateTime.now(), 100)
past = DateTime.addDays(DateTime.now(), -100)
DateTime.isFuture(future) # True
DateTime.isPast(past) # True

MethodReturnsDescription
getZoneinfo()ZoneInfoReturns a ZoneInfo for the current timezone
now(tz?)DateTimeCurrent date and time
today(tz?)DateTimeToday at midnight
tomorrow(tz?)DateTimeTomorrow at midnight
yesterday(tz?)DateTimeYesterday at midnight
datetime(year, ...)DateTimeConstructs a specific datetime
parse(string, tz?)DateTimeParses a date string
fromTimestamp(ts, tz?)DateTimeConverts a Unix timestamp
fromDatetime(dt, tz?)DateTimeConverts a stdlib or framework datetime
convertToLocal(dt)DateTimeConverts any date input to the configured timezone
formatLocal(dt?, fmt?)strFormats a datetime as a string
startOfDay(dt?)DateTimeSnaps to 00:00:00 of the day
endOfDay(dt?)DateTimeSnaps to 23:59:59 of the day
startOfWeek(dt?)DateTimeMonday at 00:00:00
endOfWeek(dt?)DateTimeSunday at 23:59:59
startOfMonth(dt?)DateTimeFirst day at 00:00:00
endOfMonth(dt?)DateTimeLast day at 23:59:59
startOfYear(dt?)DateTimeJanuary 1st at 00:00:00
endOfYear(dt?)DateTimeDecember 31st at 23:59:59
addDays(dt, n)DateTimeAdds n days
addHours(dt, n)DateTimeAdds n hours
addMinutes(dt, n)DateTimeAdds n minutes
diffInDays(dt1, dt2)intAbsolute day difference
diffInHours(dt1, dt2)intAbsolute hour difference
isWeekend(dt?)boolTrue if Saturday or Sunday
isToday(dt)boolTrue if the date is today
isFuture(dt)boolTrue if after the current moment
isPast(dt)boolTrue if before the current moment

Working with the Underlying DateTime Instance

Section titled “Working with the Underlying DateTime Instance”

Every method that returns a DateTime actually returns an enriched datetime object. You can call any method from the underlying library directly on the returned instance, giving you access to the full range of operations beyond what this class exposes:

# Obtain an instance through any DateTime method
dt = DateTime.now()
# Access extended properties
dt.day_of_week # 0 (Monday) to 6 (Sunday)
dt.day_of_year # 1–366
dt.week_of_year # 1–53
dt.days_in_month # 28–31
dt.timezone_name # 'America/Bogota'
# Use extended arithmetic
dt.add(months=2, weeks=1)
dt.subtract(years=1)
# Difference helpers
dt.diff(DateTime.yesterday()).in_hours() # hours between two instants
# Human-readable difference
dt.diff_for_humans() # e.g. "2 hours ago"
# ISO 8601 output
dt.to_iso8601_string() # '2024-06-15T10:30:45-05:00'
dt.to_date_string() # '2024-06-15'
dt.to_time_string() # '10:30:45'