This module contains high-level date and time manipulation utilities, extending the core std:time module with object-oriented representations.
Include using
var datetime = use("lib:datetime");
now
now()
Returns a DateTime instance representing the current local date and time.
- Parameters: None
- Returns: (DateTime) A
DateTimeobject initialized with the current system time.
var datetime = use("lib:datetime");
var current = datetime.now();
echo current; // e.g. "2023-10-27 14:30:00"
from_timestamp
from_timestamp(timestamp)
Constructs a DateTime object from a numeric Unix timestamp. Throws a ValueError if the timestamp is negative.
- Parameters:
timestamp(Number): The numeric Unix epoch timestamp (seconds since 1970-01-01).
- Returns: (DateTime) A new
DateTimeinstance.
var datetime = use("lib:datetime");
var dt = datetime.from_timestamp(1698417000.0);
echo dt.year; // 2023
TimeDelta (Class)
Represents a duration or difference between two dates.
Constructor
TimeDelta(days = 0, hours = 0, minutes = 0, seconds = 0)
Constructs a TimeDelta instance.
var datetime = use("lib:datetime");
var delta = datetime.TimeDelta(days = 7, hours = 2);
echo delta.total_seconds(); // 612000
total_seconds
total_seconds()
Returns the total duration converted into seconds.
- Returns: (Number) Total duration in seconds.
DateTime (Class)
The primary class representing a calendar date and time. Throws ValueError if component values (month, day, hour, minute, second) are out of valid bounds.
Constructor
DateTime(year, month = 1, day = 1, hour = 0, minute = 0, second = 0)
Constructs a new DateTime instance.
- Parameters:
year(Number): The calendar year.month(Number, optional): Month (1-12). Defaults to1.day(Number, optional): Day of month (1-31). Defaults to1.hour(Number, optional): Hour (0-23). Defaults to0.minute(Number, optional): Minute (0-59). Defaults to0.second(Number, optional): Second (0-59). Defaults to0.
var datetime = use("lib:datetime");
var start_of_year = datetime.DateTime(2024);
echo start_of_year; // "2024-01-01 00:00:00"
is_leap_year
is_leap_year()
Checks if the DateTime instance falls within a leap year.
- Returns: (Bool)
trueif a leap year, otherwisefalse.
var datetime = use("lib:datetime");
var dt = datetime.DateTime(2024, 2, 29);
echo dt.is_leap_year(); // true
add
__add__(timedelta)
Overloads the binary + operator to return a new DateTime instance shifted by a TimeDelta duration. Throws TypeError if operand is not a TimeDelta.
- Parameters:
timedelta(TimeDelta): ATimeDeltainstance specifying the shift.
- Returns: (DateTime) A recalculated
DateTimeinstance.
var datetime = use("lib:datetime");
var now = datetime.now();
var delta = datetime.TimeDelta(days = 7);
var next_week = now + delta; // Overloaded + operator
echo next_week;
str
__str__()
Returns the standard string representation formatted as "%Y-%m-%d %H:%M:%S".
- Returns: (String) The formatted string.
var datetime = use("lib:datetime");
var dt = datetime.DateTime(2024, month = 5, day = 15, hour = 9);
echo dt; // "2024-05-15 09:00:00"