DateTime
DateTime
Section titled “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.
Import
Section titled “Import”from orionis.support.time.datetime import DateTimeInternal Methods
Section titled “Internal Methods”The class exposes three prefixed methods that are reserved for the framework’s internal bootstrap process:
| Method | Effect |
|---|---|
_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 |
Configuration
Section titled “Configuration”getZoneinfo
Section titled “getZoneinfo”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')Creating Instants
Section titled “Creating Instants”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 tzDateTime.now(tz="Asia/Singapore") # current moment in SingaporeReturns today’s date at midnight (00:00:00):
DateTime.today()DateTime.today(tz="Australia/Sydney")tomorrow / yesterday
Section titled “tomorrow / yesterday”Return the date one day ahead or behind, respectively:
DateTime.tomorrow()DateTime.yesterday(tz="Pacific/Auckland")datetime
Section titled “datetime”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 MadridParsing and Conversion
Section titled “Parsing and Conversion”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 TokyofromTimestamp
Section titled “fromTimestamp”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")fromDatetime
Section titled “fromDatetime”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 timezonedt = DateTime.fromDatetime(datetime(2024, 3, 15, 10, 30))
# Aware datetime — converted to the configured timezoneaware = datetime(2024, 3, 15, 10, 30, tzinfo=timezone.utc)dt = DateTime.fromDatetime(aware)
# With explicit target timezonedt = DateTime.fromDatetime(aware, tz="Asia/Seoul")convertToLocal
Section titled “convertToLocal”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))Formatting
Section titled “Formatting”formatLocal
Section titled “formatLocal”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 patternBoundaries
Section titled “Boundaries”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:00DateTime.endOfDay(ref) # 2024-06-12 23:59:59
DateTime.startOfDay() # today at 00:00:00DateTime.endOfDay() # today at 23:59:59Weeks start on Monday and end on Sunday:
DateTime.startOfWeek(ref) # Monday 00:00:00DateTime.endOfWeek(ref) # Sunday 23:59:59DateTime.startOfMonth(ref) # 2024-06-01 00:00:00DateTime.endOfMonth(ref) # 2024-06-30 23:59:59DateTime.startOfYear(ref) # 2024-01-01 00:00:00DateTime.endOfYear(ref) # 2024-12-31 23:59:59Arithmetic
Section titled “Arithmetic”Arithmetic methods accept negative values to move backward in time.
addDays
Section titled “addDays”ref = DateTime.datetime(2024, 1, 1, 12, 0, 0, tz="UTC")
DateTime.addDays(ref, 5) # 2024-01-06DateTime.addDays(ref, -3) # 2023-12-29addHours
Section titled “addHours”DateTime.addHours(ref, 3) # 15:00DateTime.addHours(ref, 14) # next day 02:00addMinutes
Section titled “addMinutes”DateTime.addMinutes(ref, 30) # 12:30DateTime.addMinutes(ref, 90) # 13:30diffInDays
Section titled “diffInDays”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) # 10diffInHours
Section titled “diffInHours”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) # 6Predicates
Section titled “Predicates”isWeekend
Section titled “isWeekend”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) # TrueDateTime.isWeekend(monday) # FalseDateTime.isWeekend() # depends on the current dayisToday
Section titled “isToday”DateTime.isToday(DateTime.now()) # TrueDateTime.isToday(DateTime.yesterday()) # FalseisFuture / isPast
Section titled “isFuture / isPast”future = DateTime.addDays(DateTime.now(), 100)past = DateTime.addDays(DateTime.now(), -100)
DateTime.isFuture(future) # TrueDateTime.isPast(past) # TrueMethod Reference
Section titled “Method Reference”| Method | Returns | Description |
|---|---|---|
getZoneinfo() | ZoneInfo | Returns a ZoneInfo for the current timezone |
now(tz?) | DateTime | Current date and time |
today(tz?) | DateTime | Today at midnight |
tomorrow(tz?) | DateTime | Tomorrow at midnight |
yesterday(tz?) | DateTime | Yesterday at midnight |
datetime(year, ...) | DateTime | Constructs a specific datetime |
parse(string, tz?) | DateTime | Parses a date string |
fromTimestamp(ts, tz?) | DateTime | Converts a Unix timestamp |
fromDatetime(dt, tz?) | DateTime | Converts a stdlib or framework datetime |
convertToLocal(dt) | DateTime | Converts any date input to the configured timezone |
formatLocal(dt?, fmt?) | str | Formats a datetime as a string |
startOfDay(dt?) | DateTime | Snaps to 00:00:00 of the day |
endOfDay(dt?) | DateTime | Snaps to 23:59:59 of the day |
startOfWeek(dt?) | DateTime | Monday at 00:00:00 |
endOfWeek(dt?) | DateTime | Sunday at 23:59:59 |
startOfMonth(dt?) | DateTime | First day at 00:00:00 |
endOfMonth(dt?) | DateTime | Last day at 23:59:59 |
startOfYear(dt?) | DateTime | January 1st at 00:00:00 |
endOfYear(dt?) | DateTime | December 31st at 23:59:59 |
addDays(dt, n) | DateTime | Adds n days |
addHours(dt, n) | DateTime | Adds n hours |
addMinutes(dt, n) | DateTime | Adds n minutes |
diffInDays(dt1, dt2) | int | Absolute day difference |
diffInHours(dt1, dt2) | int | Absolute hour difference |
isWeekend(dt?) | bool | True if Saturday or Sunday |
isToday(dt) | bool | True if the date is today |
isFuture(dt) | bool | True if after the current moment |
isPast(dt) | bool | True 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 methoddt = DateTime.now()
# Access extended propertiesdt.day_of_week # 0 (Monday) to 6 (Sunday)dt.day_of_year # 1–366dt.week_of_year # 1–53dt.days_in_month # 28–31dt.timezone_name # 'America/Bogota'
# Use extended arithmeticdt.add(months=2, weeks=1)dt.subtract(years=1)
# Difference helpersdt.diff(DateTime.yesterday()).in_hours() # hours between two instants
# Human-readable differencedt.diff_for_humans() # e.g. "2 hours ago"
# ISO 8601 outputdt.to_iso8601_string() # '2024-06-15T10:30:45-05:00'dt.to_date_string() # '2024-06-15'dt.to_time_string() # '10:30:45'