Skip to content

Datetime

Spice
import "std/time/datetime";

DateTime struct

Calendar representation of a point in time in UTC. Fields use natural calendar values: month is 1-12 and day is 1-31.

Methods

print

Spice
public p DateTime.print()

Print the DateTime to stdout in ISO 8601 style: YYYY-MM-DD HH:MM:SS. Note: No trailing newline is emitted.

Functions

of

Spice
public f<DateTime> of(int year, int month, int day, int hour, int minute, int second)

Construct a DateTime from its individual calendar components (UTC).

Parameters

Name Type Description
year int Year (e.g. 2026)
month int Month in range [1, 12]
day int Day of month in range [1, 31]
hour int Hour in range [0, 23]
minute int Minute in range [0, 59]
second int Second in range [0, 59]

Returns: DateTime — Constructed DateTime

ofDate

Spice
public f<DateTime> ofDate(int year, int month, int day)

Construct a DateTime at midnight (00:00:00) of the given date (UTC).

Parameters

Name Type Description
year int Year (e.g. 2026)
month int Month in range [1, 12]
day int Day of month in range [1, 31]

Returns: DateTime — Constructed DateTime

fromUnixSecs

Spice
public f<DateTime> fromUnixSecs(long secs)

Convert seconds since the Unix epoch (1970-01-01T00:00:00Z) into a calendar DateTime (UTC). Uses the well-known civil-from-days algorithm by Howard Hinnant.

Parameters

Name Type Description
secs long Seconds since the Unix epoch

Returns: DateTime — Corresponding DateTime in UTC

toUnixSecs

Spice
public f<long> toUnixSecs(const DateTime& dt)

Convert a calendar DateTime (UTC) into seconds since the Unix epoch. Uses the well-known days-from-civil algorithm by Howard Hinnant.

Parameters

Name Type Description
dt const DateTime& Input DateTime (interpreted as UTC)

Returns: long — Seconds since the Unix epoch

now

Spice
public f<DateTime> now()

Retrieve the current point in time as a DateTime in UTC.

Returns: DateTime — Current DateTime in UTC

daysInMonth

Spice
public f<int> daysInMonth(int year, int month)

Determine the number of days in the given month of the given year.

Parameters

Name Type Description
year int Year (used to account for leap years in February)
month int Month in range [1, 12]

Returns: int — Number of days in the month

dayOfWeek

Spice
public f<int> dayOfWeek(const DateTime& dt)

Determine the day of the week for the given DateTime. The result is in range [0, 6], where 0 = Sunday and 6 = Saturday.

Parameters

Name Type Description
dt const DateTime& Input DateTime

Returns: int — Day of week in range [0, 6]

dayOfYear

Spice
public f<int> dayOfYear(const DateTime& dt)

Determine the day of the year for the given DateTime. The result is in range [1, 366], where January 1st is day 1.

Parameters

Name Type Description
dt const DateTime& Input DateTime

Returns: int — Day of year in range [1, 366]

addSeconds

Spice
public f<DateTime> addSeconds(const DateTime& dt, long seconds)

Return a copy of the DateTime advanced by the given number of seconds. A negative value moves the DateTime backwards in time.

Parameters

Name Type Description
dt const DateTime& Input DateTime
seconds long Number of seconds to add

Returns: DateTime — Resulting DateTime

addDays

Spice
public f<DateTime> addDays(const DateTime& dt, long days)

Return a copy of the DateTime advanced by the given number of days. A negative value moves the DateTime backwards in time.

Parameters

Name Type Description
dt const DateTime& Input DateTime
days long Number of days to add

Returns: DateTime — Resulting DateTime

isBefore

Spice
public f<bool> isBefore(const DateTime& lhs, const DateTime& rhs)

Check whether the first DateTime lies strictly before the second.

Parameters

Name Type Description
lhs const DateTime& First DateTime
rhs const DateTime& Second DateTime

Returns: bool — true if lhs is before rhs

isAfter

Spice
public f<bool> isAfter(const DateTime& lhs, const DateTime& rhs)

Check whether the first DateTime lies strictly after the second.

Parameters

Name Type Description
lhs const DateTime& First DateTime
rhs const DateTime& Second DateTime

Returns: bool — true if lhs is after rhs

equals

Spice
public f<bool> equals(const DateTime& lhs, const DateTime& rhs)

Check whether two DateTimes represent the same point in time.

Parameters

Name Type Description
lhs const DateTime& First DateTime
rhs const DateTime& Second DateTime

Returns: bool — true if both represent the same point in time