datetime Module
The datetime module provides a comprehensive set of functions for date and time operations, including timezone support, timers, and cron-like scheduling.
Functions
Now()
Returns the current time as a datetime.time object.
Returns:
(time, error): The current time object.
Example:
| Now Example (time object) | |
|---|---|
NowUnix()
Backward-compatible helper that returns the current Unix timestamp in seconds.
Returns:
(integer, error): The current Unix timestamp.
Example:
| NowUnix Example | |
|---|---|
Format(timestamp, format)
Formats a Unix timestamp into a string based on a layout.
Parameters:
timestamp: An integer representing the Unix timestamp (module-level convenience)format: A string representing the layout. SupportsRFC3339, or custom Go layout strings.
Returns:
(string, error): The formatted time string.
Example:
| Format Example (module-level) | |
|---|---|
You can also call t.Format(layout) on the time object (see below).
time object methods
datetime.time mirrors Go's time.Time API. Below are the key methods.
Accessors
t.Year() -> intt.Month() -> int(1..12). Usedatetime.MonthString(int)for names.t.Day() -> intt.Weekday() -> int(0..6, Sunday=0). Usedatetime.WeekdayString(int)for names.t.Hour() -> int,t.Minute() -> int,t.Second() -> int,t.Nanosecond() -> intt.YearDay() -> intt.ISOWeek() -> (int year, int week)t.Clock() -> (int h, int m, int s)t.Date() -> (int y, int m, int d)t.IsZero() -> bool
Unix conversions
t.Unix() -> intt.UnixMilli() -> intt.UnixMicro() -> intt.UnixNano() -> int
Time zone
t.Location() -> Locationt.Zone() -> (string name, int offsetSeconds)t.In(loc Location) -> timet.Local() -> timet.UTC() -> time
Arithmetic
t.Add(d Duration) -> timet.Sub(u time) -> Duration(seconds resolution)t.AddDate(years, months, days) -> time
Comparisons
t.After(u time) -> boolt.Before(u time) -> boolt.Equal(u time) -> boolt.Compare(u time) -> int// -1, 0, 1
Rounding
t.Truncate(d Duration) -> timet.Round(d Duration) -> time
Formatting and string forms
t.Format(layout string) -> stringt.String() -> stringt.GoString() -> string
Encoding helpers
t.MarshalText() -> string(RFC3339Nano)t.UnmarshalText(text string) -> timet.MarshalJSON() -> string(JSON string, RFC3339Nano)t.UnmarshalJSON(json string) -> timet.MarshalBinary() -> string(base64)t.UnmarshalBinary(b64 string) -> time
Helpers for names
datetime.MonthString(month int 1..12) -> (string, error)datetime.WeekdayString(weekday int 0..6) -> (string, error)
Constructors and Parsing
Constructors
datetime.FromUnix(sec int) -> (time, error)datetime.FromUnixMilli(ms int) -> (time, error)datetime.FromUnixMicro(us int) -> (time, error)datetime.FromUnixNano(ns int) -> (time, error)datetime.FromParts(y, m, d, h, min, s, nsec[, loc]) -> (time, error)
Parsing
datetime.Parse(layout, value string) -> (time, error)datetime.ParseRFC3339(value string) -> (time, error)datetime.ParseRFC3339Nano(value string) -> (time, error)
Named constants
The datetime module exposes Go-like named constants as zero-arg functions:
Weekdays (Sunday=0..Saturday=6)
datetime.Sunday()datetime.Monday()datetime.Tuesday()datetime.Wednesday()datetime.Thursday()datetime.Friday()datetime.Saturday()
Example:
Months (1..12)
datetime.January()datetime.February()datetime.March()datetime.April()datetime.May()datetime.June()datetime.July()datetime.August()datetime.September()datetime.October()datetime.November()datetime.December()
Example:
Sleep(seconds)
Pauses the execution for a given number of seconds.
Parameters:
seconds: An integer or float representing the number of seconds to sleep.
Example:
| Sleep Example | |
|---|---|
year()
Returns the year of the datetime object.
| Year Example | |
|---|---|
Month(timestamps)
Returns the month of a given Unix timestamp.
Parameters:
timestamp: An integer representing the Unix timestamp.
Returns:
(integer, error): The month (1-12).
Example:
| Month Example | |
|---|---|
Day(timestamp)
Returns the day of the month of a given Unix timestamp.
Parameters:
timestamp: An integer representing the Unix timestamp.
Returns:
(integer, error): The day of the month (1-31).
Example:
| Day Example | |
|---|---|
ConvertTZ(timestamp, fromTZ, toTZ)
Converts a timestamp from one timezone to another.
Parameters:
timestamp: An integer representing the Unix timestamp.fromTZ: The source timezone (e.g., "UTC").toTZ: The target timezone (e.g., "America/New_York").
Returns:
(integer, error): The converted Unix timestamp.
Example:
| ConvertTZ Example | |
|---|---|
NowInTZ(timezone)
Returns the current time in a specific timezone.
Parameters:
timezone: The timezone (e.g., "America/New_York").
Returns:
(integer, error): The current Unix timestamp in the specified timezone.
Example:
| NowInTZ Example | |
|---|---|
LocalTime(locale)
Returns the current time for a given locale.
Parameters:
locale: A two-letter country code (e.g., "US", "GB").
Returns:
(integer, error): The current Unix timestamp in the locale's timezone.
Example:
| LocalTime Example | |
|---|---|
GetSystemTZ()
Returns the system's timezone name.
Returns:
(string, error): The system timezone.
Example:
| GetSystemTZ Example | |
|---|---|
GetTZOffset(timezone)
Returns the timezone offset in seconds from UTC.
Parameters:
timezone: The timezone name.
Returns:
(integer, error): The offset in seconds.
Example:
| GetTZOffset Example | |
|---|---|
Timers and Tickers
NewTimer(id, delay_seconds)
Creates a new timer that will fire after a delay.
Example:
| NewTimer Example | |
|---|---|
CheckTimer(id)
Checks if a timer has fired.
Example:
| CheckTimer Example | |
|---|---|
StopTimer(id)
Stops a timer.
Example:
| StopTimer Example | |
|---|---|
NewTicker(id, interval_seconds)
Creates a new ticker that fires at regular intervals.
Example:
| NewTicker Example | |
|---|---|
StopTicker(id)
Stops a ticker.
Example:
| StopTicker Example | |
|---|---|
Cron Scheduling
IsValidCron(expression)
Validates a cron expression.
Example:
| IsValidCron Example | |
|---|---|
NextCronExecution(expression, timestamp)
Calculates the next execution time of a cron expression.
Example:
| NextCronExecution Example | |
|---|---|
ParseCron(expression)
Parses a cron expression.
Example:
| ParseCron Example | |
|---|---|
ScheduleCron(id, expression)
Schedules a cron job.
Example:
| ScheduleCron Example | |
|---|---|
StopCron(id)
Stops a scheduled cron job.
Example:
| StopCron Example | |
|---|---|
ListCronJobs()
Lists all active cron jobs.
Example:
| ListCronJobs Example | |
|---|---|
EveryMinute(), Hourly(), Daily(), Weekly(), Monthly()
Returns common cron expressions.
Example: