Dz
Djazair
v1.0.5
🔍

Datetime Module

Import using: use datetime. Provides Unix timestamps, a Date class with formatting/arithmetic/comparison, and execution measurement utilities.

Time Functions

  • datetime.timestamp() - Current Unix timestamp (seconds since epoch, with sub-second precision).
  • datetime.clock() - CPU time consumed by process (seconds).
  • datetime.ticks() - High-resolution monotonic tick count (milliseconds).
  • datetime.sleep(seconds) - Suspend execution for given duration (float, e.g. 0.5 = 500ms).
  • datetime.local(ts?) - Decompose timestamp into local-time Map (year, month, day, hour, minute, second, wday, yday, isdst). Null = current time.
  • datetime.utc(ts?) - Decompose timestamp into UTC Map (same structure as local).
  • datetime.make(year, month, day, hour, minute, second) - Construct timestamp from local components.
  • datetime.makeUtc(year, month, day, hour, minute, second) - Construct timestamp from UTC components.
  • datetime.timezoneOffset() - Local timezone offset from UTC in seconds.
  • datetime.measureTime(func) - Measure wall-clock execution time of zero-argument function. Returns seconds (float).
use datetime
# Current timestamp
print(str(datetime.timestamp()))
# Decompose local time
let parts = datetime.local()
print(parts["year"] + "-" + parts["month"] + "-" + parts["day"])
# Measure execution time
let elapsed = datetime.measureTime(fn()
    datetime.sleep(0.1)
end)
print(str(elapsed) + " seconds")

Date Class

Constructed via factory functions (see below). All arithmetic methods return new Date instances (immutable pattern).

Factory Functions

  • datetime.now() - Current local time as Date.
  • datetime.nowUTC() - Current UTC time as Date.
  • datetime.fromTimestamp(ts) - Date from Unix timestamp (local).
  • datetime.fromUTCTimestamp(ts) - Date from Unix timestamp (UTC).
  • datetime.fromParts(year, month, day, hour?, minute?, second?) - Date from components (local).
  • datetime.fromUTCParts(year, month, day, hour?, minute?, second?) - Date from components (UTC).
use datetime
let d1 = datetime.now()
let d2 = datetime.nowUTC()
let d3 = datetime.fromParts(2025, 6, 15, 10, 30, 0)
let d4 = datetime.fromUTCParts(2025, 12, 25)

Property Accessors

  • date.year(), date.month(), date.day(), date.hour(), date.minute(), date.second()
  • date.wday() - Day of week (0=Sunday).
  • date.yday() - Day of year (1-366).
  • date.isdst() - Daylight Saving Time active?
  • date.isUTC() - Is the date in UTC?
  • date.timestamp() - Unix timestamp.
use datetime
let d = datetime.now()
print(str(d.year()))
print(str(d.month()))
print(str(d.day()))
print(str(d.wday()))    # day of week (0=Sun)
print(str(d.yday()))    # day of year
print(str(d.isdst()))   # DST active?
print(str(d.isUTC()))   # is UTC?
print(str(d.timestamp()))

Predicates

  • date.isLeap() - Is the year a leap year?
  • date.daysInMonth() - Number of days in current month.
  • date.isToday() - Is the date today (local calendar date)?
use datetime
let d = datetime.fromParts(2024, 2, 15)
print(str(d.isLeap()))       # True (2024 is leap)
print(str(d.daysInMonth()))  # 29
print(str(d.isToday()))      # probably False

Formatting

  • date.format(pattern) - Format with specifiers: %Y (year), %m (month), %d (day), %H (hour), %M (minute), %S (second), %A (weekday), %a (short weekday), %B (month name), %b (short month), %ArA (Arabic weekday), %ArB (Arabic month).
  • date.toString() - ISO-style "YYYY-MM-DD HH:MM:SS".
use datetime
let d = datetime.fromParts(2025, 6, 15, 10, 30, 0)
print(d.format("%Y-%m-%d %H:%M:%S"))  # 2025-06-15 10:30:00
print(d.toString())                    # 2025-06-15 10:30:00
print(d.format("%A, %B %d, %Y"))      # Sunday, June 15, 2025

Arithmetic (all return new Date)

  • date.addSeconds(secs), date.addMinutes(mins), date.addHours(hrs), date.addDays(days)
  • date.addMonths(months) - Handles varying month lengths and leap years.
  • date.addYears(years) - Clamps day to valid range.
use datetime
let d = datetime.fromParts(2025, 1, 31)
let next = d.addDays(1)
print(str(next.day()))  # 1
let nextMonth = d.addMonths(1)
print(str(nextMonth.month()))  # 2
print(str(nextMonth.day()))    # 28 (clamped)

Difference

  • date.diff(other) - Signed difference in seconds (self - other).
  • date.diffInDays(other), date.diffInHours(other), date.diffInMinutes(other)
use datetime
let d1 = datetime.fromParts(2025, 6, 15)
let d2 = datetime.fromParts(2025, 6, 10)
print(str(d1.diff(d2)))          # 432000 seconds (5 days)
print(str(d2.diffInDays(d1)))    # -5

Comparison

  • date.compare(other) - Returns -1, 0, or 1.
  • date.isBefore(other), date.isAfter(other), date.isEqual(other)
use datetime
let d1 = datetime.fromParts(2025, 6, 15)
let d2 = datetime.fromParts(2025, 6, 20)
print(str(d1.isBefore(d2)))  # True
print(str(d1.isAfter(d2)))   # False
print(str(d1.isEqual(d1)))   # True
print(str(d1.compare(d2)))   # -1
© 2026 Harizi Riyadh. Released under the MIT License.
Official Website for Djazair.