std.datetime.systime
- License:
- Boost License 1.0.
- Authors:
- Jonathan M Davis
- Source
- std/datetime/systime.d
- class Clock;
-
Effectively a namespace to make it clear that the methods it contains are getting the time from the system clock. It cannot be instantiated.
- Examples:
- Get the current time as a
SysTime
import std.datetime.timezone : LocalTime; SysTime today = Clock.currTime(); assert(today.timezone is LocalTime());
- @safe SysTime currTime(ClockType clockType = ClockType.normal)(immutable TimeZone tz = LocalTime());
-
Returns the current time in the given time zone.
- Parameters:
clockType The core.time.ClockType
indicates which system clock to use to get the current time. Very few programs need to use anything other than the default.TimeZone tz
The time zone for the SysTime that's returned.
- Throws:
-
std.datetime.date.DateTimeException
if it fails to get the time.
- @property @trusted long currStdTime(ClockType clockType = ClockType.normal)();
-
Returns the number of hnsecs since midnight, January 1st, 1 A.D. for the current time.
- Parameters:
clockType The core.time.ClockType
indicates which system clock to use to get the current time. Very few programs need to use anything other than the default.
- Throws:
-
std.datetime.date.DateTimeException
if it fails to get the time.
- struct SysTime;
-
SysTime
is the type used to get the current time from the system or doing anything that involves time zones. Unlikestd.datetime.date.DateTime
, the time zone is an integral part ofSysTime
(though for local time applications, time zones can be ignored and it will work, since it defaults to using the local time zone). It holds its internal time in std time (hnsecs since midnight, January 1st, 1 A.D. UTC), so it interfaces well with the system time. However, that means that, unlikestd.datetime.date.DateTime
, it is not optimized for calendar-based operations, and getting individual units from it such as years or days is going to involve conversions and be less efficient.For calendar-based operations that don't care about time zones, then
std.datetime.date.DateTime
would be the type to use. For system time, useSysTime
.
Clock.currTime
will return the current time as aSysTime
. To convert aSysTime
to astd.datetime.date.Date
orstd.datetime.date.DateTime
, simply cast it. To convert astd.datetime.date.Date
orstd.datetime.date.DateTime
to aSysTime
, useSysTime
's constructor, and pass in the intended time zone with it (or don't pass in astd.datetime.timezone.TimeZone
, and the local time zone will be used). Be aware, however, that converting from astd.datetime.date.DateTime
to aSysTime
will not necessarily be 100% accurate due to DST (one hour of the year doesn't exist and another occurs twice). To not risk any conversion errors, keep times asSysTime
s. Aside from DST though, there shouldn't be any conversion problems.
For using time zones other than local time or UTC, usestd.datetime.timezone.PosixTimeZone
on Posix systems (or on Windows, if providing the TZ Database files), and usestd.datetime.timezone.WindowsTimeZone
on Windows systems. The time inSysTime
is kept internally in hnsecs from midnight, January 1st, 1 A.D. UTC. Conversion error cannot happen when changing the time zone of aSysTime
.std.datetime.timezone.LocalTime
is thestd.datetime.timezone.TimeZone
class which represents the local time, andUTC
is thestd.datetime.timezone.TimeZone
class which represents UTC.SysTime
usesstd.datetime.timezone.LocalTime
if nostd.datetime.timezone.TimeZone
is provided. For more details on time zones, see the documentation forstd.datetime.timezone.TimeZone
,std.datetime.timezone.PosixTimeZone
, andstd.datetime.timezone.WindowsTimeZone
.
SysTime
's range is from approximately 29,000 B.C. to approximately 29,000 A.D.- Examples:
-
import core.time : days, hours, seconds; import std.datetime.date : DateTime; import std.datetime.timezone : SimpleTimeZone, UTC; // make a specific point in time in the UTC timezone auto st = SysTime(DateTime(2018, 1, 1, 10, 30, 0), UTC()); // make a specific point in time in the New York timezone auto ny = SysTime( DateTime(2018, 1, 1, 10, 30, 0), new immutable SimpleTimeZone(-5.hours, "America/New_York") ); // ISO standard time strings writeln(st.toISOString()); // "20180101T103000Z" writeln(st.toISOExtString()); // "2018-01-01T10:30:00Z" // add two days and 30 seconds st += 2.days + 30.seconds; writeln(st.toISOExtString()); // "2018-01-03T10:30:30Z"
- nothrow @safe this(DateTime dateTime, immutable TimeZone tz = null);
-
- Parameters:
DateTime dateTime
The std.datetime.date.DateTime
to use to set thisSysTime
's internal std time. Asstd.datetime.date.DateTime
has no concept of time zone, tz is used as its time zone.TimeZone tz
The std.datetime.timezone.TimeZone
to use for thisSysTime
. If null,std.datetime.timezone.LocalTime
will be used. The givenstd.datetime.date.DateTime
is assumed to be in the given time zone.
- @safe this(DateTime dateTime, Duration fracSecs, immutable TimeZone tz = null);
-
- Parameters:
DateTime dateTime
The std.datetime.date.DateTime
to use to set thisSysTime
's internal std time. Asstd.datetime.date.DateTime
has no concept of time zone, tz is used as its time zone.Duration fracSecs
The fractional seconds portion of the time. TimeZone tz
The std.datetime.timezone.TimeZone
to use for thisSysTime
. If null,std.datetime.timezone.LocalTime
will be used. The givenstd.datetime.date.DateTime
is assumed to be in the given time zone.
- Throws:
-
std.datetime.date.DateTimeException
iffracSecs
is negative or if it's greater than or equal to one second.
- nothrow @safe this(Date date, immutable TimeZone tz = null);
-
- Parameters:
Date date
The std.datetime.date.Date
to use to set thisSysTime
's internal std time. Asstd.datetime.date.Date
has no concept of time zone, tz is used as its time zone.TimeZone tz
The std.datetime.timezone.TimeZone
to use for thisSysTime
. If null,std.datetime.timezone.LocalTime
will be used. The givenstd.datetime.date.Date
is assumed to be in the given time zone.
- pure nothrow @safe this(long stdTime, immutable TimeZone tz = null);
-
- Note
- Whereas the other constructors take in the given date/time, assume that it's in the given time zone, and convert it to hnsecs in UTC since midnight, January 1st, 1 A.D. UTC - i.e. std time - this constructor takes a std time, which is specifically already in UTC, so no conversion takes place. Of course, the various getter properties and functions will use the given time zone's conversion function to convert the results to that time zone, but no conversion of the arguments to this constructor takes place.
- Parameters:
long stdTime
The number of hnsecs since midnight, January 1st, 1 A.D. UTC. TimeZone tz
The std.datetime.timezone.TimeZone
to use for thisSysTime
. If null,std.datetime.timezone.LocalTime
will be used.
- pure nothrow ref scope @safe SysTime opAssign()(auto ref const(SysTime) rhs) return;
-
- Parameters:
const(SysTime) rhs
The SysTime
to assign to this one.
- Returns:
- The
this
of thisSysTime
.
- const pure nothrow scope @safe bool opEquals()(auto ref const(SysTime) rhs);
-
Checks for equality between this
SysTime
and the givenSysTime
.Note that the time zone is ignored. Only the internal std times (which are in UTC) are compared.
- const pure nothrow scope @safe int opCmp()(auto ref const(SysTime) rhs);
-
Compares this
SysTime
with the givenSysTime
.Time zone is irrelevant when comparing
SysTime
s.- Returns:
this < rhs < 0 this == rhs 0 this > rhs > 0
- const pure nothrow @nogc scope @safe size_t toHash();
-
- Returns:
- A hash of the
SysTime
.
- const nothrow @property scope @safe short year();
-
Year of the Gregorian Calendar. Positive numbers are A.D. Non-positive are B.C.
- @property scope @safe void year(int year);
-
Year of the Gregorian Calendar. Positive numbers are A.D. Non-positive are B.C.
- Parameters:
int year
The year to set this SysTime
's year to.
- Throws:
-
std.datetime.date.DateTimeException
if the new year is not a leap year and the resulting date would be on February 29th.
- Examples:
-
import std.datetime.date : DateTime; writeln(SysTime(DateTime(1999, 7, 6, 9, 7, 5)).year); // 1999 writeln(SysTime(DateTime(2010, 10, 4, 0, 0, 30)).year); // 2010 writeln(SysTime(DateTime(-7, 4, 5, 7, 45, 2)).year); // -7
- const @property scope @safe ushort yearBC();
-
Year B.C. of the Gregorian Calendar counting year 0 as 1 B.C.
- Throws:
-
std.datetime.date.DateTimeException
ifisAD
is true.
- Examples:
-
import std.datetime.date : DateTime; writeln(SysTime(DateTime(0, 1, 1, 12, 30, 33)).yearBC); // 1 writeln(SysTime(DateTime(-1, 1, 1, 10, 7, 2)).yearBC); // 2 writeln(SysTime(DateTime(-100, 1, 1, 4, 59, 0)).yearBC); // 101
- @property scope @safe void yearBC(int year);
-
Year B.C. of the Gregorian Calendar counting year 0 as 1 B.C.
- Parameters:
int year
The year B.C. to set this SysTime
's year to.
- Throws:
-
std.datetime.date.DateTimeException
if a non-positive value is given.
- const nothrow @property scope @safe Month month();
-
Month of a Gregorian Year.
- Examples:
-
import std.datetime.date : DateTime; writeln(SysTime(DateTime(1999, 7, 6, 9, 7, 5)).month); // 7 writeln(SysTime(DateTime(2010, 10, 4, 0, 0, 30)).month); // 10 writeln(SysTime(DateTime(-7, 4, 5, 7, 45, 2)).month); // 4
- @property scope @safe void month(Month month);
-
Month of a Gregorian Year.
- Parameters:
Month month
The month to set this SysTime
's month to.
- Throws:
-
std.datetime.date.DateTimeException
if the given month is not a valid month.
- const nothrow @property scope @safe ubyte day();
-
Day of a Gregorian Month.
- Examples:
-
import std.datetime.date : DateTime; writeln(SysTime(DateTime(1999, 7, 6, 9, 7, 5)).day); // 6 writeln(SysTime(DateTime(2010, 10, 4, 0, 0, 30)).day); // 4 writeln(SysTime(DateTime(-7, 4, 5, 7, 45, 2)).day); // 5
- @property scope @safe void day(int day);
-
Day of a Gregorian Month.
- Parameters:
int day
The day of the month to set this SysTime
's day to.
- Throws:
-
std.datetime.date.DateTimeException
if the given day is not a valid day of the current month.
- const nothrow @property scope @safe ubyte hour();
-
Hours past midnight.
- @property scope @safe void hour(int hour);
-
Hours past midnight.
- Parameters:
int hour
The hours to set this SysTime
's hour to.
- Throws:
-
std.datetime.date.DateTimeException
if the given hour are not a valid hour of the day.
- const nothrow @property scope @safe ubyte minute();
-
Minutes past the current hour.
- @property scope @safe void minute(int minute);
-
Minutes past the current hour.
- Parameters:
int minute
The minute to set this SysTime
's minute to.
- Throws:
-
std.datetime.date.DateTimeException
if the given minute are not a valid minute of an hour.
- const nothrow @property scope @safe ubyte second();
-
Seconds past the current minute.
- @property scope @safe void second(int second);
-
Seconds past the current minute.
- Parameters:
int second
The second to set this SysTime
's second to.
- Throws:
-
std.datetime.date.DateTimeException
if the given second are not a valid second of a minute.
- const nothrow @property scope @safe Duration fracSecs();
-
Fractional seconds past the second (i.e. the portion of a
SysTime
which is less than a second).- Examples:
-
import core.time : msecs, usecs, hnsecs, nsecs; import std.datetime.date : DateTime; auto dt = DateTime(1982, 4, 1, 20, 59, 22); writeln(SysTime(dt, msecs(213)).fracSecs); // msecs(213) writeln(SysTime(dt, usecs(5202)).fracSecs); // usecs(5202) writeln(SysTime(dt, hnsecs(1234567)).fracSecs); // hnsecs(1234567) // SysTime and Duration both have a precision of hnsecs (100 ns), // so nsecs are going to be truncated. writeln(SysTime(dt, nsecs(123456789)).fracSecs); // nsecs(123456700)
- @property scope @safe void fracSecs(Duration fracSecs);
-
Fractional seconds past the second (i.e. the portion of a
SysTime
which is less than a second).- Parameters:
Duration fracSecs
The duration to set this SysTime
's fractional seconds to.
- Throws:
-
std.datetime.date.DateTimeException
if the given duration is negative or if it's greater than or equal to one second.
- Examples:
-
import core.time : Duration, msecs, hnsecs, nsecs; import std.datetime.date : DateTime; auto st = SysTime(DateTime(1982, 4, 1, 20, 59, 22)); writeln(st.fracSecs); // Duration.zero st.fracSecs = msecs(213); writeln(st.fracSecs); // msecs(213) st.fracSecs = hnsecs(1234567); writeln(st.fracSecs); // hnsecs(1234567) // SysTime has a precision of hnsecs (100 ns), so nsecs are // going to be truncated. st.fracSecs = nsecs(123456789); writeln(st.fracSecs); // hnsecs(1234567)
- const pure nothrow @nogc @property scope @safe long stdTime();
-
The total hnsecs from midnight, January 1st, 1 A.D. UTC. This is the internal representation of
SysTime
. - pure nothrow @property scope @safe void stdTime(long stdTime);
-
The total hnsecs from midnight, January 1st, 1 A.D. UTC. This is the internal representation of
SysTime
.- Parameters:
long stdTime
The number of hnsecs since January 1st, 1 A.D. UTC.
- const pure nothrow @property scope @safe immutable(TimeZone) timezone();
-
The current time zone of this
SysTime
. Its internal time is always kept in UTC, so there are no conversion issues between time zones due to DST. Functions which return all or part of the time - such as hours - adjust the time to thisSysTime
's time zone before returning. - pure nothrow @property scope @safe void timezone(immutable TimeZone timezone);
-
The current time zone of this
SysTime
. It's internal time is always kept in UTC, so there are no conversion issues between time zones due to DST. Functions which return all or part of the time - such as hours - adjust the time to thisSysTime
's time zone before returning.- Parameters:
TimeZone timezone
The std.datetime.timezone.TimeZone
to set thisSysTime
's time zone to.
- const nothrow @property scope @safe bool dstInEffect();
-
Returns whether DST is in effect for this
SysTime
. - const nothrow @property scope @safe Duration utcOffset();
-
Returns what the offset from UTC is for this
SysTime
. It includes the DST offset in effect at that time (if any). - const pure nothrow scope @safe SysTime toLocalTime();
-
Returns a
SysTime
with the same std time as this one, but withstd.datetime.timezone.LocalTime
as its time zone. - const pure nothrow scope @safe SysTime toUTC();
-
Returns a
SysTime
with the same std time as this one, but withUTC
as its time zone. - const pure nothrow scope @safe SysTime toOtherTZ(immutable TimeZone tz);
-
Returns a
SysTime
with the same std time as this one, but with given time zone as its time zone. - const pure nothrow scope @safe T toUnixTime(T = time_t)()
Constraints: if (is(T == int) || is(T == long)); -
Converts this
SysTime
to unix time (i.e. seconds from midnight, January 1st, 1970 in UTC).The C standard does not specify the representation of time_t, so it is implementation defined. On POSIX systems, unix time is equivalent to time_t, but that's not necessarily true on other systems (e.g. it is not true for the Digital Mars C runtime). So, be careful when using unix time with C functions on non-POSIX systems.
By default, the return type is time_t (which is normally an alias for int on 32-bit systems and long on 64-bit systems), but if a different size is required than either int or long can be passed as a template argument to get the desired size.
If the return type is int, and the result can't fit in an int, then the closest value that can be held in 32 bits will be used (soint.max
if it goes over andint.min
if it goes under). However, no attempt is made to deal with integer overflow if the return type is long.- Parameters:
T The return type (int or long). It defaults to time_t, which is normally 32 bits on a 32-bit system and 64 bits on a 64-bit system.
- Returns:
- A signed integer representing the unix time which is equivalent to this SysTime.
- Examples:
-
import core.time : hours; import std.datetime.date : DateTime; import std.datetime.timezone : SimpleTimeZone, UTC; writeln(SysTime(DateTime(1970, 1, 1), UTC()).toUnixTime()); // 0 auto pst = new immutable SimpleTimeZone(hours(-8)); writeln(SysTime(DateTime(1970, 1, 1), pst).toUnixTime()); // 28800 auto utc = SysTime(DateTime(2007, 12, 22, 8, 14, 45), UTC()); writeln(utc.toUnixTime()); // 1_198_311_285 auto ca = SysTime(DateTime(2007, 12, 22, 8, 14, 45), pst); writeln(ca.toUnixTime()); // 1_198_340_085 static void testScope(scope ref SysTime st) @safe { auto result = st.toUnixTime(); }
- static pure nothrow @safe SysTime fromUnixTime(long unixTime, immutable TimeZone tz = LocalTime());
-
Converts from unix time (i.e. seconds from midnight, January 1st, 1970 in UTC) to a
SysTime
.The C standard does not specify the representation of time_t, so it is implementation defined. On POSIX systems, unix time is equivalent to time_t, but that's not necessarily true on other systems (e.g. it is not true for the Digital Mars C runtime). So, be careful when using unix time with C functions on non-POSIX systems.
- Parameters:
long unixTime
Seconds from midnight, January 1st, 1970 in UTC. TimeZone tz
The time zone for the SysTime that's returned.
- Examples:
-
import core.time : hours; import std.datetime.date : DateTime; import std.datetime.timezone : SimpleTimeZone, UTC; assert(SysTime.fromUnixTime(0) == SysTime(DateTime(1970, 1, 1), UTC())); auto pst = new immutable SimpleTimeZone(hours(-8)); assert(SysTime.fromUnixTime(28800) == SysTime(DateTime(1970, 1, 1), pst)); auto st1 = SysTime.fromUnixTime(1_198_311_285, UTC()); writeln(st1); // SysTime(DateTime(2007, 12, 22, 8, 14, 45), UTC()) assert(st1.timezone is UTC()); writeln(st1); // SysTime(DateTime(2007, 12, 22, 0, 14, 45), pst) auto st2 = SysTime.fromUnixTime(1_198_311_285, pst); writeln(st2); // SysTime(DateTime(2007, 12, 22, 8, 14, 45), UTC()) assert(st2.timezone is pst); writeln(st2); // SysTime(DateTime(2007, 12, 22, 0, 14, 45), pst)
- const pure nothrow scope @safe timeval toTimeVal();
-
Returns a
timeval
which represents thisSysTime
.Note that like all conversions in std.datetime, this is a truncating conversion.
Iftimeval.tv_sec
is int, and the result can't fit in an int, then the closest value that can be held in 32 bits will be used fortv_sec
. (soint.max
if it goes over andint.min
if it goes under). - const pure nothrow scope @safe timespec toTimeSpec();
-
Returns a
timespec
which represents thisSysTime
.This function is Posix-Only.
- const nothrow scope @safe tm toTM();
-
Returns a
tm
which represents thisSysTime
. - nothrow ref scope @safe SysTime add(string units)(long value, AllowDayOverflow allowOverflow = AllowDayOverflow.yes)
Constraints: if (units == "years" || units == "months"); -
Adds the given number of years or months to this
SysTime
. A negative number will subtract.Note that if day overflow is allowed, and the date with the adjusted year/month overflows the number of days in the new month, then the month will be incremented by one, and the day set to the number of days overflowed. (e.g. if the day were 31 and the new month were June, then the month would be incremented to July, and the new day would be 1). If day overflow is not allowed, then the day will be set to the last valid day in the month (e.g. June 31st would become June 30th).
- Parameters:
units The type of units to add ("years" or "months"). long value
The number of months or years to add to this SysTime
.AllowDayOverflow allowOverflow
Whether the days should be allowed to overflow, causing the month to increment.
- nothrow ref scope @safe SysTime roll(string units)(long value, AllowDayOverflow allowOverflow = AllowDayOverflow.yes)
Constraints: if (units == "years"); -
Adds the given number of years or months to this
SysTime
. A negative number will subtract.The difference between rolling and adding is that rolling does not affect larger units. Rolling a
SysTime
12 months gets the exact sameSysTime
. However, the days can still be affected due to the differing number of days in each month.
Because there are no units larger than years, there is no difference between adding and rolling years.- Parameters:
units The type of units to add ("years" or "months"). long value
The number of months or years to add to this SysTime
.AllowDayOverflow allowOverflow
Whether the days should be allowed to overflow, causing the month to increment.
- Examples:
-
import std.datetime.date : AllowDayOverflow, DateTime; auto st1 = SysTime(DateTime(2010, 1, 1, 12, 33, 33)); st1.roll!"months"(1); writeln(st1); // SysTime(DateTime(2010, 2, 1, 12, 33, 33)) auto st2 = SysTime(DateTime(2010, 1, 1, 12, 33, 33)); st2.roll!"months"(-1); writeln(st2); // SysTime(DateTime(2010, 12, 1, 12, 33, 33)) auto st3 = SysTime(DateTime(1999, 1, 29, 12, 33, 33)); st3.roll!"months"(1); writeln(st3); // SysTime(DateTime(1999, 3, 1, 12, 33, 33)) auto st4 = SysTime(DateTime(1999, 1, 29, 12, 33, 33)); st4.roll!"months"(1, AllowDayOverflow.no); writeln(st4); // SysTime(DateTime(1999, 2, 28, 12, 33, 33)) auto st5 = SysTime(DateTime(2000, 2, 29, 12, 30, 33)); st5.roll!"years"(1); writeln(st5); // SysTime(DateTime(2001, 3, 1, 12, 30, 33)) auto st6 = SysTime(DateTime(2000, 2, 29, 12, 30, 33)); st6.roll!"years"(1, AllowDayOverflow.no); writeln(st6); // SysTime(DateTime(2001, 2, 28, 12, 30, 33))
- nothrow ref scope @safe SysTime roll(string units)(long value)
Constraints: if (units == "days"); -
Adds the given number of units to this
SysTime
. A negative number will subtract.The difference between rolling and adding is that rolling does not affect larger units. For instance, rolling a
SysTime
one year's worth of days gets the exact sameSysTime
.
Accepted units are"days"
,"minutes"
,"hours"
,"minutes"
,"seconds"
,"msecs"
,"usecs"
, and"hnsecs"
.
Note that when rolling msecs, usecs or hnsecs, they all add up to a second. So, for example, rolling 1000 msecs is exactly the same as rolling 100,000 usecs.- Parameters:
units The units to add. long value
The number of units to add to this SysTime
.
- Examples:
-
import core.time : msecs, hnsecs; import std.datetime.date : DateTime; auto st1 = SysTime(DateTime(2010, 1, 1, 11, 23, 12)); st1.roll!"days"(1); writeln(st1); // SysTime(DateTime(2010, 1, 2, 11, 23, 12)) st1.roll!"days"(365); writeln(st1); // SysTime(DateTime(2010, 1, 26, 11, 23, 12)) st1.roll!"days"(-32); writeln(st1); // SysTime(DateTime(2010, 1, 25, 11, 23, 12)) auto st2 = SysTime(DateTime(2010, 7, 4, 12, 0, 0)); st2.roll!"hours"(1); writeln(st2); // SysTime(DateTime(2010, 7, 4, 13, 0, 0)) auto st3 = SysTime(DateTime(2010, 2, 12, 12, 0, 0)); st3.roll!"hours"(-1); writeln(st3); // SysTime(DateTime(2010, 2, 12, 11, 0, 0)) auto st4 = SysTime(DateTime(2009, 12, 31, 0, 0, 0)); st4.roll!"minutes"(1); writeln(st4); // SysTime(DateTime(2009, 12, 31, 0, 1, 0)) auto st5 = SysTime(DateTime(2010, 1, 1, 0, 0, 0)); st5.roll!"minutes"(-1); writeln(st5); // SysTime(DateTime(2010, 1, 1, 0, 59, 0)) auto st6 = SysTime(DateTime(2009, 12, 31, 0, 0, 0)); st6.roll!"seconds"(1); writeln(st6); // SysTime(DateTime(2009, 12, 31, 0, 0, 1)) auto st7 = SysTime(DateTime(2010, 1, 1, 0, 0, 0)); st7.roll!"seconds"(-1); writeln(st7); // SysTime(DateTime(2010, 1, 1, 0, 0, 59)) auto dt = DateTime(2010, 1, 1, 0, 0, 0); auto st8 = SysTime(dt); st8.roll!"msecs"(1); writeln(st8); // SysTime(dt, msecs(1)) auto st9 = SysTime(dt); st9.roll!"msecs"(-1); writeln(st9); // SysTime(dt, msecs(999)) auto st10 = SysTime(dt); st10.roll!"hnsecs"(1); writeln(st10); // SysTime(dt, hnsecs(1)) auto st11 = SysTime(dt); st11.roll!"hnsecs"(-1); writeln(st11); // SysTime(dt, hnsecs(9_999_999))
- const pure nothrow scope @safe SysTime opBinary(string op)(Duration duration)
Constraints: if (op == "+" || op == "-"); -
Gives the result of adding or subtracting a
core.time.Duration
from thisSysTime
.The legal types of arithmetic for
SysTime
using this operator are
SysTime + Duration --> SysTime SysTime - Duration --> SysTime - Parameters:
Duration duration
The core.time.Duration
to add to or subtract from thisSysTime
.
- Examples:
-
import core.time : hours, seconds; import std.datetime.date : DateTime; assert(SysTime(DateTime(2015, 12, 31, 23, 59, 59)) + seconds(1) == SysTime(DateTime(2016, 1, 1, 0, 0, 0))); assert(SysTime(DateTime(2015, 12, 31, 23, 59, 59)) + hours(1) == SysTime(DateTime(2016, 1, 1, 0, 59, 59))); assert(SysTime(DateTime(2016, 1, 1, 0, 0, 0)) - seconds(1) == SysTime(DateTime(2015, 12, 31, 23, 59, 59))); assert(SysTime(DateTime(2016, 1, 1, 0, 59, 59)) - hours(1) == SysTime(DateTime(2015, 12, 31, 23, 59, 59)));
- pure nothrow ref scope @safe SysTime opOpAssign(string op)(Duration duration)
Constraints: if (op == "+" || op == "-"); -
Gives the result of adding or subtracting a
core.time.Duration
from thisSysTime
, as well as assigning the result to thisSysTime
.The legal types of arithmetic for
SysTime
using this operator are
SysTime + Duration --> SysTime SysTime - Duration --> SysTime - Parameters:
Duration duration
The core.time.Duration
to add to or subtract from thisSysTime
.
- const pure nothrow scope @safe Duration opBinary(string op)(SysTime rhs)
Constraints: if (op == "-"); -
Gives the difference between two
SysTime
s.The legal types of arithmetic for
SysTime
using this operator are
SysTime - SysTime --> duration - const nothrow scope @safe int diffMonths(scope SysTime rhs);
-
Returns the difference between the two
SysTime
s in months.To get the difference in years, subtract the year property of two
SysTime
s. To get the difference in days or weeks, subtract theSysTime
s themselves and use thecore.time.Duration
that results. Because converting between months and smaller units requires a specific date (whichcore.time.Duration
s don't have), getting the difference in months requires some math using both the year and month properties, so this is a convenience function for getting the difference in months.
Note that the number of days in the months or how far into the month either date is is irrelevant. It is the difference in the month property combined with the difference in years * 12. So, for instance, December 31st and January 1st are one month apart just as December 1st and January 31st are one month apart.- Parameters:
SysTime rhs
The SysTime
to subtract from this one.
- Examples:
-
import core.time; import std.datetime.date : Date; assert(SysTime(Date(1999, 2, 1)).diffMonths( SysTime(Date(1999, 1, 31))) == 1); assert(SysTime(Date(1999, 1, 31)).diffMonths( SysTime(Date(1999, 2, 1))) == -1); assert(SysTime(Date(1999, 3, 1)).diffMonths( SysTime(Date(1999, 1, 1))) == 2); assert(SysTime(Date(1999, 1, 1)).diffMonths( SysTime(Date(1999, 3, 31))) == -2);
- const nothrow @property scope @safe bool isLeapYear();
-
Whether this
SysTime
is in a leap year. - const nothrow @property scope @safe DayOfWeek dayOfWeek();
-
Day of the week this
SysTime
is on. - const nothrow @property scope @safe ushort dayOfYear();
-
Day of the year this
SysTime
is on.- Examples:
-
import core.time; import std.datetime.date : DateTime; writeln(SysTime(DateTime(1999, 1, 1, 12, 22, 7)).dayOfYear); // 1 writeln(SysTime(DateTime(1999, 12, 31, 7, 2, 59)).dayOfYear); // 365 writeln(SysTime(DateTime(2000, 12, 31, 21, 20, 0)).dayOfYear); // 366
- @property scope @safe void dayOfYear(int day);
-
Day of the year.
- Parameters:
int day
The day of the year to set which day of the year this SysTime
is on.
- const nothrow @property scope @safe int dayOfGregorianCal();
-
The Xth day of the Gregorian Calendar that this
SysTime
is on.- Examples:
-
import core.time; import std.datetime.date : DateTime; writeln(SysTime(DateTime(1, 1, 1, 0, 0, 0)).dayOfGregorianCal); // 1 writeln(SysTime(DateTime(1, 12, 31, 23, 59, 59)).dayOfGregorianCal); // 365 writeln(SysTime(DateTime(2, 1, 1, 2, 2, 2)).dayOfGregorianCal); // 366 writeln(SysTime(DateTime(0, 12, 31, 7, 7, 7)).dayOfGregorianCal); // 0 writeln(SysTime(DateTime(0, 1, 1, 19, 30, 0)).dayOfGregorianCal); // -365 writeln(SysTime(DateTime(-1, 12, 31, 4, 7, 0)).dayOfGregorianCal); // -366 writeln(SysTime(DateTime(2000, 1, 1, 9, 30, 20)).dayOfGregorianCal); // 730_120 writeln(SysTime(DateTime(2010, 12, 31, 15, 45, 50)).dayOfGregorianCal); // 734_137
- nothrow @property scope @safe void dayOfGregorianCal(int days);
-
The Xth day of the Gregorian Calendar that this
SysTime
is on. Setting this property does not affect the time portion ofSysTime
.- Parameters:
int days
The day of the Gregorian Calendar to set this SysTime
to.
- Examples:
-
import core.time; import std.datetime.date : DateTime; auto st = SysTime(DateTime(0, 1, 1, 12, 0, 0)); st.dayOfGregorianCal = 1; writeln(st); // SysTime(DateTime(1, 1, 1, 12, 0, 0)) st.dayOfGregorianCal = 365; writeln(st); // SysTime(DateTime(1, 12, 31, 12, 0, 0)) st.dayOfGregorianCal = 366; writeln(st); // SysTime(DateTime(2, 1, 1, 12, 0, 0)) st.dayOfGregorianCal = 0; writeln(st); // SysTime(DateTime(0, 12, 31, 12, 0, 0)) st.dayOfGregorianCal = -365; writeln(st); // SysTime(DateTime(-0, 1, 1, 12, 0, 0)) st.dayOfGregorianCal = -366; writeln(st); // SysTime(DateTime(-1, 12, 31, 12, 0, 0)) st.dayOfGregorianCal = 730_120; writeln(st); // SysTime(DateTime(2000, 1, 1, 12, 0, 0)) st.dayOfGregorianCal = 734_137; writeln(st); // SysTime(DateTime(2010, 12, 31, 12, 0, 0))
- const nothrow @property scope @safe ubyte isoWeek();
-
The ISO 8601 week of the year that this
SysTime
is in.- See Also:
- ISO Week Date.
- Examples:
-
import core.time; import std.datetime.date : Date; auto st = SysTime(Date(1999, 7, 6)); const cst = SysTime(Date(2010, 5, 1)); immutable ist = SysTime(Date(2015, 10, 10)); writeln(st.isoWeek); // 27 writeln(cst.isoWeek); // 17 writeln(ist.isoWeek); // 41
- const nothrow @property scope @safe SysTime endOfMonth();
-
SysTime
for the last day in the month that this Date is in. The time portion of endOfMonth is always 23:59:59.9999999.- Examples:
-
import core.time : msecs, usecs, hnsecs; import std.datetime.date : DateTime; assert(SysTime(DateTime(1999, 1, 6, 0, 0, 0)).endOfMonth == SysTime(DateTime(1999, 1, 31, 23, 59, 59), hnsecs(9_999_999))); assert(SysTime(DateTime(1999, 2, 7, 19, 30, 0), msecs(24)).endOfMonth == SysTime(DateTime(1999, 2, 28, 23, 59, 59), hnsecs(9_999_999))); assert(SysTime(DateTime(2000, 2, 7, 5, 12, 27), usecs(5203)).endOfMonth == SysTime(DateTime(2000, 2, 29, 23, 59, 59), hnsecs(9_999_999))); assert(SysTime(DateTime(2000, 6, 4, 12, 22, 9), hnsecs(12345)).endOfMonth == SysTime(DateTime(2000, 6, 30, 23, 59, 59), hnsecs(9_999_999)));
- const nothrow @property scope @safe ubyte daysInMonth();
-
The last day in the month that this
SysTime
is in.- Examples:
-
import core.time; import std.datetime.date : DateTime; writeln(SysTime(DateTime(1999, 1, 6, 0, 0, 0)).daysInMonth); // 31 writeln(SysTime(DateTime(1999, 2, 7, 19, 30, 0)).daysInMonth); // 28 writeln(SysTime(DateTime(2000, 2, 7, 5, 12, 27)).daysInMonth); // 29 writeln(SysTime(DateTime(2000, 6, 4, 12, 22, 9)).daysInMonth); // 30
- const nothrow @property scope @safe bool isAD();
-
Whether the current year is a date in A.D.
- Examples:
-
import core.time; import std.datetime.date : DateTime; assert(SysTime(DateTime(1, 1, 1, 12, 7, 0)).isAD); assert(SysTime(DateTime(2010, 12, 31, 0, 0, 0)).isAD); assert(!SysTime(DateTime(0, 12, 31, 23, 59, 59)).isAD); assert(!SysTime(DateTime(-2010, 1, 1, 2, 2, 2)).isAD);
- const nothrow @property scope @safe long julianDay();
-
The Julian day for this
SysTime
at the given time. For example, prior to noon, 1996-03-31 would be the Julian day number 2_450_173, so this function returns 2_450_173, while from noon onward, the Julian day number would be 2_450_174, so this function returns 2_450_174. - const nothrow @property scope @safe long modJulianDay();
-
The modified Julian day for any time on this date (since, the modified Julian day changes at midnight).
- const nothrow scope @safe Date opCast(T)()
Constraints: if (is(immutable(T) == immutable(Date))); -
Returns a
std.datetime.date.Date
equivalent to thisSysTime
. - const nothrow scope @safe DateTime opCast(T)()
Constraints: if (is(immutable(T) == immutable(DateTime))); -
Returns a
std.datetime.date.DateTime
equivalent to thisSysTime
. - const nothrow scope @safe TimeOfDay opCast(T)()
Constraints: if (is(immutable(T) == immutable(TimeOfDay))); -
Returns a
std.datetime.date.TimeOfDay
equivalent to thisSysTime
. - const nothrow scope @safe string toISOString();
const scope void toISOString(W)(ref W writer)
Constraints: if (isOutputRange!(W, char)); -
Converts this
SysTime
to a string with the format YYYYMMDDTHHMMSS.FFFFFFFTZ (where F is fractional seconds and TZ is time zone).Note that the number of digits in the fractional seconds varies with the number of fractional seconds. It's a maximum of 7 (which would be hnsecs), but only has as many as are necessary to hold the correct value (so no trailing zeroes), and if there are no fractional seconds, then there is no decimal point.
If thisSysTime
's time zone isstd.datetime.timezone.LocalTime
, then TZ is empty. If its time zone isUTC
, then it is "Z". Otherwise, it is the offset from UTC (e.g. +0100 or -0700). Note that the offset from UTC is not enough to uniquely identify the time zone.
Time zone offsets will be in the form +HHMM or -HHMM.
Warning: Previously, toISOString did the same astoISOExtString
and generated +HH:MM or -HH:MM for the time zone when it was notstd.datetime.timezone.LocalTime
orstd.datetime.timezone.UTC
, which is not in conformance with ISO 8601 for the non-extended string format. This has now been fixed. However, for now, fromISOString will continue to accept the extended format for the time zone so that any code which has been writing out the result of toISOString to read in later will continue to work. The current behavior will be kept until July 2019 at which point, fromISOString will be fixed to be standards compliant.- Parameters:
W writer
A char
accepting output range
- Returns:
- A
string
when not using an output range;void
otherwise.
- Examples:
-
import core.time : msecs, hnsecs; import std.datetime.date : DateTime; assert(SysTime(DateTime(2010, 7, 4, 7, 6, 12)).toISOString() == "20100704T070612"); assert(SysTime(DateTime(1998, 12, 25, 2, 15, 0), msecs(24)).toISOString() == "19981225T021500.024"); assert(SysTime(DateTime(0, 1, 5, 23, 9, 59)).toISOString() == "00000105T230959"); assert(SysTime(DateTime(-4, 1, 5, 0, 0, 2), hnsecs(520_920)).toISOString() == "-00040105T000002.052092");
- const nothrow scope @safe string toISOExtString();
const scope void toISOExtString(W)(ref W writer)
Constraints: if (isOutputRange!(W, char)); -
Converts this
SysTime
to a string with the format YYYY-MM-DDTHH:MM:SS.FFFFFFFTZ (where F is fractional seconds and TZ is the time zone).Note that the number of digits in the fractional seconds varies with the number of fractional seconds. It's a maximum of 7 (which would be hnsecs), but only has as many as are necessary to hold the correct value (so no trailing zeroes), and if there are no fractional seconds, then there is no decimal point.
If thisSysTime
's time zone isstd.datetime.timezone.LocalTime
, then TZ is empty. If its time zone isUTC
, then it is "Z". Otherwise, it is the offset from UTC (e.g. +01:00 or -07:00). Note that the offset from UTC is not enough to uniquely identify the time zone.
Time zone offsets will be in the form +HH:MM or -HH:MM.- Parameters:
W writer
A char
accepting output range
- Returns:
- A
string
when not using an output range;void
otherwise.
- Examples:
-
import core.time : msecs, hnsecs; import std.datetime.date : DateTime; assert(SysTime(DateTime(2010, 7, 4, 7, 6, 12)).toISOExtString() == "2010-07-04T07:06:12"); assert(SysTime(DateTime(1998, 12, 25, 2, 15, 0), msecs(24)).toISOExtString() == "1998-12-25T02:15:00.024"); assert(SysTime(DateTime(0, 1, 5, 23, 9, 59)).toISOExtString() == "0000-01-05T23:09:59"); assert(SysTime(DateTime(-4, 1, 5, 0, 0, 2), hnsecs(520_920)).toISOExtString() == "-0004-01-05T00:00:02.052092");
- const nothrow scope @safe string toSimpleString();
const scope void toSimpleString(W)(ref W writer)
Constraints: if (isOutputRange!(W, char)); -
Converts this
SysTime
to a string with the format YYYY-Mon-DD HH:MM:SS.FFFFFFFTZ (where F is fractional seconds and TZ is the time zone).Note that the number of digits in the fractional seconds varies with the number of fractional seconds. It's a maximum of 7 (which would be hnsecs), but only has as many as are necessary to hold the correct value (so no trailing zeroes), and if there are no fractional seconds, then there is no decimal point.
If thisSysTime
's time zone isstd.datetime.timezone.LocalTime
, then TZ is empty. If its time zone isUTC
, then it is "Z". Otherwise, it is the offset from UTC (e.g. +01:00 or -07:00). Note that the offset from UTC is not enough to uniquely identify the time zone.
Time zone offsets will be in the form +HH:MM or -HH:MM.- Parameters:
W writer
A char
accepting output range
- Returns:
- A
string
when not using an output range;void
otherwise.
- Examples:
-
import core.time : msecs, hnsecs; import std.datetime.date : DateTime; assert(SysTime(DateTime(2010, 7, 4, 7, 6, 12)).toSimpleString() == "2010-Jul-04 07:06:12"); assert(SysTime(DateTime(1998, 12, 25, 2, 15, 0), msecs(24)).toSimpleString() == "1998-Dec-25 02:15:00.024"); assert(SysTime(DateTime(0, 1, 5, 23, 9, 59)).toSimpleString() == "0000-Jan-05 23:09:59"); assert(SysTime(DateTime(-4, 1, 5, 0, 0, 2), hnsecs(520_920)).toSimpleString() == "-0004-Jan-05 00:00:02.052092");
- const nothrow scope @safe string toString();
const scope void toString(W)(ref W writer)
Constraints: if (isOutputRange!(W, char)); -
Converts this
SysTime
to a string.This function exists to make it easy to convert a
SysTime
to a string for code that does not care what the exact format is - just that it presents the information in a clear manner. It also makes it easy to simply convert aSysTime
to a string when using functions such asto!string
,format
, orwriteln
which use toString to convert user-defined types. So, it is unlikely that much code will call toString directly.
The format of the string is purposefully unspecified, and code that cares about the format of the string should usetoISOString
,toISOExtString
,toSimpleString
, or some other custom formatting function that explicitly generates the format that the code needs. The reason is that the code is then clear about what format it's using, making it less error-prone to maintain the code and interact with other software that consumes the generated strings. It's for this same reason thatSysTime
has nofromString
function, whereas it does havefromISOString
,fromISOExtString
, andfromSimpleString
.
The format returned by toString may or may not change in the future.- Parameters:
W writer
A char
accepting output range
- Returns:
- A
string
when not using an output range;void
otherwise.
- @safe SysTime fromISOString(S)(scope const S isoString, immutable TimeZone tz = null)
Constraints: if (isSomeString!S); -
Creates a
SysTime
from a string with the format YYYYMMDDTHHMMSS.FFFFFFFTZ (where F is fractional seconds is the time zone). Whitespace is stripped from the given string.The exact format is exactly as described in
toISOString
except that trailing zeroes are permitted - including having fractional seconds with all zeroes. However, a decimal point with nothing following it is invalid. Also, whiletoISOString
will never generate a string with more than 7 digits in the fractional seconds (because that's the limit with hecto-nanosecond precision), it will allow more than 7 digits in order to read strings from other sources that have higher precision (however, any digits beyond 7 will be truncated).
If there is no time zone in the string, thenstd.datetime.timezone.LocalTime
is used. If the time zone is "Z", thenUTC
is used. Otherwise, astd.datetime.timezone.SimpleTimeZone
which corresponds to the given offset from UTC is used. To get the returnedSysTime
to be a particular time zone, pass in that time zone and theSysTime
to be returned will be converted to that time zone (though it will still be read in as whatever time zone is in its string).
The accepted formats for time zone offsets are +HH, -HH, +HHMM, and -HHMM.
Warning: Previously,toISOString
did the same astoISOExtString
and generated +HH:MM or -HH:MM for the time zone when it was notstd.datetime.timezone.LocalTime
orstd.datetime.timezone.UTC
, which is not in conformance with ISO 8601 for the non-extended string format. This has now been fixed. However, for now, fromISOString will continue to accept the extended format for the time zone so that any code which has been writing out the result of toISOString to read in later will continue to work. The current behavior will be kept until July 2019 at which point, fromISOString will be fixed to be standards compliant.- Parameters:
S isoString
A string formatted in the ISO format for dates and times. TimeZone tz
The time zone to convert the given time to (no conversion occurs if null).
- Throws:
-
std.datetime.date.DateTimeException
if the given string is not in the ISO format or if the resultingSysTime
would not be valid.
- @safe SysTime fromISOExtString(S)(scope const S isoExtString, immutable TimeZone tz = null)
Constraints: if (isSomeString!S); -
Creates a
SysTime
from a string with the format YYYY-MM-DDTHH:MM:SS.FFFFFFFTZ (where F is fractional seconds is the time zone). Whitespace is stripped from the given string.The exact format is exactly as described in
toISOExtString
except that trailing zeroes are permitted - including having fractional seconds with all zeroes. However, a decimal point with nothing following it is invalid. Also, whiletoISOExtString
will never generate a string with more than 7 digits in the fractional seconds (because that's the limit with hecto-nanosecond precision), it will allow more than 7 digits in order to read strings from other sources that have higher precision (however, any digits beyond 7 will be truncated).
If there is no time zone in the string, thenstd.datetime.timezone.LocalTime
is used. If the time zone is "Z", thenUTC
is used. Otherwise, astd.datetime.timezone.SimpleTimeZone
which corresponds to the given offset from UTC is used. To get the returnedSysTime
to be a particular time zone, pass in that time zone and theSysTime
to be returned will be converted to that time zone (though it will still be read in as whatever time zone is in its string).
The accepted formats for time zone offsets are +HH, -HH, +HH:MM, and -HH:MM.- Parameters:
S isoExtString
A string formatted in the ISO Extended format for dates and times. TimeZone tz
The time zone to convert the given time to (no conversion occurs if null).
- Throws:
-
std.datetime.date.DateTimeException
if the given string is not in the ISO format or if the resultingSysTime
would not be valid.
- @safe SysTime fromSimpleString(S)(scope const S simpleString, immutable TimeZone tz = null)
Constraints: if (isSomeString!S); -
Creates a
SysTime
from a string with the format YYYY-MM-DD HH:MM:SS.FFFFFFFTZ (where F is fractional seconds is the time zone). Whitespace is stripped from the given string.The exact format is exactly as described in
toSimpleString
except that trailing zeroes are permitted - including having fractional seconds with all zeroes. However, a decimal point with nothing following it is invalid. Also, whiletoSimpleString
will never generate a string with more than 7 digits in the fractional seconds (because that's the limit with hecto-nanosecond precision), it will allow more than 7 digits in order to read strings from other sources that have higher precision (however, any digits beyond 7 will be truncated).
If there is no time zone in the string, thenstd.datetime.timezone.LocalTime
is used. If the time zone is "Z", thenUTC
is used. Otherwise, astd.datetime.timezone.SimpleTimeZone
which corresponds to the given offset from UTC is used. To get the returnedSysTime
to be a particular time zone, pass in that time zone and theSysTime
to be returned will be converted to that time zone (though it will still be read in as whatever time zone is in its string).
The accepted formats for time zone offsets are +HH, -HH, +HH:MM, and -HH:MM.- Parameters:
S simpleString
A string formatted in the way that toSimpleString
formats dates and times.TimeZone tz
The time zone to convert the given time to (no conversion occurs if null).
- Throws:
-
std.datetime.date.DateTimeException
if the given string is not in the ISO format or if the resultingSysTime
would not be valid.
- static pure nothrow @property @safe SysTime min();
-
Returns the
SysTime
farthest in the past which is representable bySysTime
.The
SysTime
which is returned is in UTC. - static pure nothrow @property @safe SysTime max();
-
Returns the
SysTime
farthest in the future which is representable bySysTime
.The
SysTime
which is returned is in UTC.
- pure nothrow @nogc @safe long unixTimeToStdTime(long unixTime);
-
Converts from unix time (which uses midnight, January 1st, 1970 UTC as its epoch and seconds as its units) to "std time" (which uses midnight, January 1st, 1 A.D. UTC and hnsecs as its units).
The C standard does not specify the representation of time_t, so it is implementation defined. On POSIX systems, unix time is equivalent to time_t, but that's not necessarily true on other systems (e.g. it is not true for the Digital Mars C runtime). So, be careful when using unix time with C functions on non-POSIX systems.
"std time"'s epoch is based on the Proleptic Gregorian Calendar per ISO 8601 and is whatSysTime
uses internally. However, holding the time as an integer in hnsecs since that epoch technically isn't actually part of the standard, much as it's based on it, so the name "std time" isn't particularly good, but there isn't an official name for it. C# uses "ticks" for the same thing, but they aren't actually clock ticks, and the term "ticks" is used for actual clock ticks forcore.time.MonoTime
, so it didn't make sense to use the term ticks here. So, for better or worse, std.datetime uses the term "std time" for this.- Parameters:
long unixTime
The unix time to convert.
- See Also:
- SysTime.fromUnixTime
- Examples:
-
import std.datetime.date : DateTime; import std.datetime.timezone : UTC; // Midnight, January 1st, 1970 writeln(unixTimeToStdTime(0)); // 621_355_968_000_000_000L assert(SysTime(unixTimeToStdTime(0)) == SysTime(DateTime(1970, 1, 1), UTC())); writeln(unixTimeToStdTime(int.max)); // 642_830_804_470_000_000L assert(SysTime(unixTimeToStdTime(int.max)) == SysTime(DateTime(2038, 1, 19, 3, 14, 07), UTC())); writeln(unixTimeToStdTime(-127_127)); // 621_354_696_730_000_000L assert(SysTime(unixTimeToStdTime(-127_127)) == SysTime(DateTime(1969, 12, 30, 12, 41, 13), UTC()));
- pure nothrow @safe T stdTimeToUnixTime(T = time_t)(long stdTime)
Constraints: if (is(T == int) || is(T == long)); -
Converts std time (which uses midnight, January 1st, 1 A.D. UTC as its epoch and hnsecs as its units) to unix time (which uses midnight, January 1st, 1970 UTC as its epoch and seconds as its units).
The C standard does not specify the representation of time_t, so it is implementation defined. On POSIX systems, unix time is equivalent to time_t, but that's not necessarily true on other systems (e.g. it is not true for the Digital Mars C runtime). So, be careful when using unix time with C functions on non-POSIX systems.
"std time"'s epoch is based on the Proleptic Gregorian Calendar per ISO 8601 and is whatSysTime
uses internally. However, holding the time as an integer in hnescs since that epoch technically isn't actually part of the standard, much as it's based on it, so the name "std time" isn't particularly good, but there isn't an official name for it. C# uses "ticks" for the same thing, but they aren't actually clock ticks, and the term "ticks" is used for actual clock ticks forcore.time.MonoTime
, so it didn't make sense to use the term ticks here. So, for better or worse, std.datetime uses the term "std time" for this.
By default, the return type is time_t (which is normally an alias for int on 32-bit systems and long on 64-bit systems), but if a different size is required than either int or long can be passed as a template argument to get the desired size.
If the return type is int, and the result can't fit in an int, then the closest value that can be held in 32 bits will be used (soint.max
if it goes over andint.min
if it goes under). However, no attempt is made to deal with integer overflow if the return type is long.- Parameters:
T The return type (int or long). It defaults to time_t, which is normally 32 bits on a 32-bit system and 64 bits on a 64-bit system. long stdTime
The std time to convert.
- Returns:
- A signed integer representing the unix time which is equivalent to the given std time.
- See Also:
- SysTime.toUnixTime
- Examples:
-
// Midnight, January 1st, 1970 UTC writeln(stdTimeToUnixTime(621_355_968_000_000_000L)); // 0 // 2038-01-19 03:14:07 UTC writeln(stdTimeToUnixTime(642_830_804_470_000_000L)); // int.max
- @safe SysTime SYSTEMTIMEToSysTime(scope const SYSTEMTIME* st, immutable TimeZone tz = LocalTime());
-
This function is Windows-Only.
Converts a
SYSTEMTIME
struct to aSysTime
.- Parameters:
SYSTEMTIME* st
The SYSTEMTIME
struct to convert.TimeZone tz
The time zone that the time in the SYSTEMTIME
struct is assumed to be (if theSYSTEMTIME
was supplied by a Windows system call, theSYSTEMTIME
will either be in local time or UTC, depending on the call).
- Throws:
-
std.datetime.date.DateTimeException
if the givenSYSTEMTIME
will not fit in aSysTime
, which is highly unlikely to happen given thatSysTime.max
is in 29,228 A.D. and the maximumSYSTEMTIME
is in 30,827 A.D.
- @safe SYSTEMTIME SysTimeToSYSTEMTIME(scope SysTime sysTime);
-
This function is Windows-Only.
Converts a
SysTime
to aSYSTEMTIME
struct.
TheSYSTEMTIME
which is returned will be set using the givenSysTime
's time zone, so to get theSYSTEMTIME
in UTC, set theSysTime
's time zone to UTC.- Parameters:
SysTime sysTime
The SysTime
to convert.
- Throws:
-
std.datetime.date.DateTimeException
if the givenSysTime
will not fit in aSYSTEMTIME
. This will only happen if theSysTime
's date is prior to 1601 A.D.
- @safe long FILETIMEToStdTime(scope const FILETIME* ft);
-
This function is Windows-Only.
Converts a
FILETIME
struct to the number of hnsecs since midnight, January 1st, 1 A.D.- Parameters:
FILETIME* ft
The FILETIME
struct to convert.
- Throws:
-
std.datetime.date.DateTimeException
if the givenFILETIME
cannot be represented as the return value.
- @safe SysTime FILETIMEToSysTime(scope const FILETIME* ft, immutable TimeZone tz = LocalTime());
-
This function is Windows-Only.
Converts a
FILETIME
struct to aSysTime
.- Parameters:
FILETIME* ft
The FILETIME
struct to convert.TimeZone tz
The time zone that the SysTime
will be in (FILETIME
s are in UTC).
- Throws:
-
std.datetime.date.DateTimeException
if the givenFILETIME
will not fit in aSysTime
.
- @safe FILETIME stdTimeToFILETIME(long stdTime);
-
This function is Windows-Only.
Converts a number of hnsecs since midnight, January 1st, 1 A.D. to a
FILETIME
struct.- Parameters:
long stdTime
The number of hnsecs since midnight, January 1st, 1 A.D. UTC.
- Throws:
-
std.datetime.date.DateTimeException
if the given value will not fit in aFILETIME
.
- @safe FILETIME SysTimeToFILETIME(scope SysTime sysTime);
-
This function is Windows-Only.
Converts a
SysTime
to aFILETIME
struct.
FILETIME
s are always in UTC.- Parameters:
SysTime sysTime
The SysTime
to convert.
- Throws:
-
std.datetime.date.DateTimeException
if the givenSysTime
will not fit in aFILETIME
.
- alias DosFileTime = uint;
-
Type representing the DOS file date/time format.
- @safe SysTime DosFileTimeToSysTime(DosFileTime dft, immutable TimeZone tz = LocalTime());
-
Converts from DOS file date/time to
SysTime
.- Parameters:
DosFileTime dft
The DOS file time to convert. TimeZone tz
The time zone which the DOS file time is assumed to be in.
- Throws:
-
std.datetime.date.DateTimeException
if theDosFileTime
is invalid.
- Examples:
-
import std.datetime.date : DateTime; // SysTime(DateTime(1980, 1, 1, 0, 0, 0)) writeln(DosFileTimeToSysTime(0b00000000001000010000000000000000)); // SysTime(DateTime(2107, 12, 31, 23, 59, 58)) writeln(DosFileTimeToSysTime(0b11111111100111111011111101111101)); writeln(DosFileTimeToSysTime(0x3E3F8456)); // SysTime(DateTime(2011, 1, 31, 16, 34, 44))
- @safe DosFileTime SysTimeToDosFileTime(scope SysTime sysTime);
-
Converts from
SysTime
to DOS file date/time.- Parameters:
SysTime sysTime
The SysTime
to convert.
- Throws:
-
std.datetime.date.DateTimeException
if the givenSysTime
cannot be converted to aDosFileTime
.
- Examples:
-
import std.datetime.date : DateTime; // 0b00000000001000010000000000000000 writeln(SysTimeToDosFileTime(SysTime(DateTime(1980, 1, 1, 0, 0, 0)))); // 0b11111111100111111011111101111101 writeln(SysTimeToDosFileTime(SysTime(DateTime(2107, 12, 31, 23, 59, 58)))); writeln(SysTimeToDosFileTime(SysTime(DateTime(2011, 1, 31, 16, 34, 44)))); // 0x3E3F8456
- @safe SysTime parseRFC822DateTime()(scope const char[] value);
SysTime parseRFC822DateTime(R)(scope R value)
Constraints: if (isRandomAccessRange!R && hasSlicing!R && hasLength!R && (is(immutable(ElementType!R) == immutable(char)) || is(immutable(ElementType!R) == immutable(ubyte)))); -
The given array of
char
or random-access range ofchar
orubyte
is expected to be in the format specified in RFC 5322 section 3.3 with the grammar rule date-time. It is the date-time format commonly used in internet messages such as e-mail and HTTP. The correspondingSysTime
will be returned.RFC 822 was the original spec (hence the function's name), whereas RFC 5322 is the current spec.
The day of the week is ignored beyond verifying that it's a valid day of the week, as the day of the week can be inferred from the date. It is not checked whether the given day of the week matches the actual day of the week of the given date (though it is technically invalid per the spec if the day of the week doesn't match the actual day of the week of the given date).
If the time zone is"-0000"
(or considered to be equivalent to"-0000"
by section 4.3 of the spec), astd.datetime.timezone.SimpleTimeZone
with a utc offset of0
is used rather thanstd.datetime.timezone.UTC
, whereas"+0000"
usesstd.datetime.timezone.UTC
.
Note that becauseSysTime
does not currently support having a second value of 60 (as is sometimes done for leap seconds), if the date-time value does have a value of 60 for the seconds, it is treated as 59.
The one area in which this function violates RFC 5322 is that it accepts"\n"
in folding whitespace in the place of"\r\n"
, because the HTTP spec requires it.- Throws:
-
std.datetime.date.DateTimeException
if the given string doesn't follow the grammar for a date-time field or if the resultingSysTime
is invalid.
- Examples:
-
import core.time : hours; import std.datetime.date : DateTime, DateTimeException; import std.datetime.timezone : SimpleTimeZone, UTC; import std.exception : assertThrown; auto tz = new immutable SimpleTimeZone(hours(-8)); assert(parseRFC822DateTime("Sat, 6 Jan 1990 12:14:19 -0800") == SysTime(DateTime(1990, 1, 6, 12, 14, 19), tz)); assert(parseRFC822DateTime("9 Jul 2002 13:11 +0000") == SysTime(DateTime(2002, 7, 9, 13, 11, 0), UTC())); auto badStr = "29 Feb 2001 12:17:16 +0200"; assertThrown!DateTimeException(parseRFC822DateTime(badStr));
© 1999–2021 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/std_datetime_systime.html