SysTime
import std.datetime.timezone : LocalTime; SysTime today = Clock.currTime(); assert(today.timezone is LocalTime());
SysTime
from a ISO time string import std.datetime.date : DateTime; import std.datetime.timezone : UTC; auto st = SysTime.fromISOExtString("2018-01-01T10:30:00Z"); writeln(st); // SysTime(DateTime(2018, 1, 1, 10, 30, 0), UTC())
import core.time : hours; import std.datetime.date : DateTime; import std.datetime.timezone : SimpleTimeZone; auto ny = SysTime( DateTime(2018, 1, 1, 10, 30, 0), new immutable SimpleTimeZone(-5.hours, "America/New_York") ); // ISO standard time strings writeln(ny.toISOString()); // "20180101T103000-05:00" writeln(ny.toISOExtString()); // "2018-01-01T10:30:00-05:00"
Effectively a namespace to make it clear that the methods it contains are getting the time from the system clock. It cannot be instantiated.
SysTime
import std.datetime.timezone : LocalTime; SysTime today = Clock.currTime(); assert(today.timezone is LocalTime());
Returns the current time in the given time zone.
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. |
std.datetime.date.DateTimeException
if it fails to get the time.Returns the number of hnsecs since midnight, January 1st, 1 A.D. for the current time.
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. |
std.datetime.date.DateTimeException
if it fails to get the time.SysTime
is the type used to get the current time from the system or doing anything that involves time zones. Unlike std.datetime.date.DateTime
, the time zone is an integral part of SysTime
(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, unlike std.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, use SysTime
.
Clock.currTime
will return the current time as a SysTime
. To convert a SysTime
to a std.datetime.date.Date
or std.datetime.date.DateTime
, simply cast it. To convert a std.datetime.date.Date
or std.datetime.date.DateTime
to a SysTime
, use SysTime
's constructor, and pass in the ntended time zone with it (or don't pass in a std.datetime.timezone.TimeZone
, and the local time zone will be used). Be aware, however, that converting from a std.datetime.date.DateTime
to a SysTime
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 as SysTime
s. Aside from DST though, there shouldn't be any conversion problems.
For using time zones other than local time or UTC, use std.datetime.timezone.PosixTimeZone
on Posix systems (or on Windows, if providing the TZ Database files), and use std.datetime.timezone.WindowsTimeZone
on Windows systems. The time in SysTime
is kept internally in hnsecs from midnight, January 1st, 1 A.D. UTC. Conversion error cannot happen when changing the time zone of a SysTime
. std.datetime.timezone.LocalTime
is the std.datetime.timezone.TimeZone
class which represents the local time, and UTC
is the std.datetime.timezone.TimeZone
class which represents UTC. SysTime
uses std.datetime.timezone.LocalTime
if no std.datetime.timezone.TimeZone
is provided. For more details on time zones, see the documentation for std.datetime.timezone.TimeZone
, std.datetime.timezone.PosixTimeZone
, and std.datetime.timezone.WindowsTimeZone
.
SysTime
's range is from approximately 29,000 B.C. to approximately 29,000 A.D.
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"
DateTime dateTime
| The std.datetime.date.DateTime to use to set this SysTime 's internal std time. As std.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 this SysTime . If null, std.datetime.timezone.LocalTime will be used. The given std.datetime.date.DateTime is assumed to be in the given time zone. |
DateTime dateTime
| The std.datetime.date.DateTime to use to set this SysTime 's internal std time. As std.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 this SysTime . If null, std.datetime.timezone.LocalTime will be used. The given std.datetime.date.DateTime is assumed to be in the given time zone. |
std.datetime.date.DateTimeException
if fracSecs
is negative or if it's greater than or equal to one second.Date date
| The std.datetime.date.Date to use to set this SysTime 's internal std time. As std.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 this SysTime . If null, std.datetime.timezone.LocalTime will be used. The given std.datetime.date.Date is assumed to be in the given time zone. |
long stdTime
| The number of hnsecs since midnight, January 1st, 1 A.D. UTC. |
TimeZone tz
| The std.datetime.timezone.TimeZone to use for this SysTime . If null, std.datetime.timezone.LocalTime will be used. |
const(SysTime) rhs
| The SysTime to assign to this one. |
this
of this SysTime
.Checks for equality between this SysTime
and the given SysTime
.
Note that the time zone is ignored. Only the internal std times (which are in UTC) are compared.
Compares this SysTime
with the given SysTime
.
Time zone is irrelevant when comparing SysTime
s.
this < rhs | < 0 |
this == rhs | 0 |
this > rhs | > 0 |
SysTime
.Year of the Gregorian Calendar. Positive numbers are A.D. Non-positive are B.C.
Year of the Gregorian Calendar. Positive numbers are A.D. Non-positive are B.C.
int year
| The year to set this SysTime 's year to. |
std.datetime.date.DateTimeException
if the new year is not a leap year and the resulting date would be on February 29th.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
Year B.C. of the Gregorian Calendar counting year 0 as 1 B.C.
std.datetime.date.DateTimeException
if isAD
is true.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
Year B.C. of the Gregorian Calendar counting year 0 as 1 B.C.
int year
| The year B.C. to set this SysTime 's year to. |
std.datetime.date.DateTimeException
if a non-positive value is given.Month of a Gregorian Year.
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
Month of a Gregorian Year.
Month month
| The month to set this SysTime 's month to. |
std.datetime.date.DateTimeException
if the given month is not a valid month.Day of a Gregorian Month.
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
Day of a Gregorian Month.
int day
| The day of the month to set this SysTime 's day to. |
std.datetime.date.DateTimeException
if the given day is not a valid day of the current month.Hours past midnight.
Hours past midnight.
int hour
| The hours to set this SysTime 's hour to. |
std.datetime.date.DateTimeException
if the given hour are not a valid hour of the day.Minutes past the current hour.
Minutes past the current hour.
int minute
| The minute to set this SysTime 's minute to. |
std.datetime.date.DateTimeException
if the given minute are not a valid minute of an hour.Seconds past the current minute.
Seconds past the current minute.
int second
| The second to set this SysTime 's second to. |
std.datetime.date.DateTimeException
if the given second are not a valid second of a minute.Fractional seconds past the second (i.e. the portion of a SysTime
which is less than a second).
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)
Fractional seconds past the second (i.e. the portion of a SysTime
which is less than a second).
Duration fracSecs
| The duration to set this SysTime 's fractional seconds to. |
std.datetime.date.DateTimeException
if the given duration is negative or if it's greater than or equal to one second.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)
The total hnsecs from midnight, January 1st, 1 A.D. UTC. This is the internal representation of SysTime
.
The total hnsecs from midnight, January 1st, 1 A.D. UTC. This is the internal representation of SysTime
.
long stdTime
| The number of hnsecs since January 1st, 1 A.D. UTC. |
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 this SysTime
's time zone before returning.
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 this SysTime
's time zone before returning.
TimeZone timezone
| The std.datetime.timezone.TimeZone to set this SysTime 's time zone to. |
Returns whether DST is in effect for this SysTime
.
Returns what the offset from UTC is for this SysTime
. It includes the DST offset in effect at that time (if any).
Returns a SysTime
with the same std time as this one, but with std.datetime.timezone.LocalTime
as its time zone.
Returns a SysTime
with the same std time as this one, but with UTC
as its time zone.
Returns a SysTime
with the same std time as this one, but with given time zone as its time zone.
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 (so int.max
if it goes over and int.min
if it goes under). However, no attempt is made to deal with integer overflow if the return type is long.
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. |
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(); }
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.
long unixTime
| Seconds from midnight, January 1st, 1970 in UTC. |
TimeZone tz
| The time zone for the SysTime that's returned. |
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)
Returns a timeval
which represents this SysTime
.
Note that like all conversions in std.datetime, this is a truncating conversion.
If timeval.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 for tv_sec
. (so int.max
if it goes over and int.min
if it goes under).
Returns a timespec
which represents this SysTime
.
This function is Posix-Only.
Returns a tm
which represents this SysTime
.
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).
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. |
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 same SysTime
. 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.
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. |
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))
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 same SysTime
.
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.
units | The units to add. |
long value
| The number of units to add to this SysTime . |
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))
Gives the result of adding or subtracting a core.time.Duration
from this SysTime
.
The legal types of arithmetic for SysTime
using this operator are
SysTime | + | Duration | --> | SysTime |
SysTime | - | Duration | --> | SysTime |
Duration duration
| The core.time.Duration to add to or subtract from this SysTime . |
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)));
Gives the result of adding or subtracting a core.time.Duration
from this SysTime
, as well as assigning the result to this SysTime
.
The legal types of arithmetic for SysTime
using this operator are
SysTime | + | Duration | --> | SysTime |
SysTime | - | Duration | --> | SysTime |
Duration duration
| The core.time.Duration to add to or subtract from this SysTime . |
Gives the difference between two SysTime
s.
The legal types of arithmetic for SysTime
using this operator are
SysTime | - | SysTime | --> | duration |
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 the SysTime
s themselves and use the core.time.Duration
that results. Because converting between months and smaller units requires a specific date (which core.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.
SysTime rhs
| The SysTime to subtract from this one. |
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);
Whether this SysTime
is in a leap year.
Day of the week this SysTime
is on.
Day of the year this SysTime
is on.
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
Day of the year.
int day
| The day of the year to set which day of the year this SysTime is on. |
The Xth day of the Gregorian Calendar that this SysTime
is on.
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
The Xth day of the Gregorian Calendar that this SysTime
is on. Setting this property does not affect the time portion of SysTime
.
int days
| The day of the Gregorian Calendar to set this SysTime to. |
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))
The ISO 8601 week of the year that this SysTime
is in.
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
SysTime
for the last day in the month that this Date is in. The time portion of endOfMonth is always 23:59:59.9999999.
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)));
The last day in the month that this SysTime
is in.
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
Whether the current year is a date in A.D.
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);
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.
The modified Julian day for any time on this date (since, the modified Julian day changes at midnight).
Returns a std.datetime.date.Date
equivalent to this SysTime
.
Returns a std.datetime.date.DateTime
equivalent to this SysTime
.
Returns a std.datetime.date.TimeOfDay
equivalent to this SysTime
.
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 this SysTime
's time zone is std.datetime.timezone.LocalTime
, then TZ is empty. If its time zone is UTC
, 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 as toISOExtString
and generated +HH:MM or -HH:MM for the time zone when it was not std.datetime.timezone.LocalTime
or std.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.
W writer
| A char accepting output range
|
string
when not using an output range; void
otherwise.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");
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 this SysTime
's time zone is std.datetime.timezone.LocalTime
, then TZ is empty. If its time zone is UTC
, 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.
W writer
| A char accepting output range
|
string
when not using an output range; void
otherwise.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");
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 this SysTime
's time zone is std.datetime.timezone.LocalTime
, then TZ is empty. If its time zone is UTC
, 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.
W writer
| A char accepting output range
|
string
when not using an output range; void
otherwise.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");
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 a SysTime
to a string when using functions such as to!string
, format
, or writeln
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 use toISOString
, 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 that SysTime
has no fromString
function, whereas it does have fromISOString
, fromISOExtString
, and fromSimpleString
.
The format returned by toString may or may not change in the future.
W writer
| A char accepting output range
|
string
when not using an output range; void
otherwise.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, while toISOString
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, then std.datetime.timezone.LocalTime
is used. If the time zone is "Z", then UTC
is used. Otherwise, a std.datetime.timezone.SimpleTimeZone
which corresponds to the given offset from UTC is used. To get the returned SysTime
to be a particular time zone, pass in that time zone and the SysTime
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 as toISOExtString
and generated +HH:MM or -HH:MM for the time zone when it was not std.datetime.timezone.LocalTime
or std.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.
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). |
std.datetime.date.DateTimeException
if the given string is not in the ISO format or if the resulting SysTime
would not be valid.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, while toISOExtString
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, then std.datetime.timezone.LocalTime
is used. If the time zone is "Z", then UTC
is used. Otherwise, a std.datetime.timezone.SimpleTimeZone
which corresponds to the given offset from UTC is used. To get the returned SysTime
to be a particular time zone, pass in that time zone and the SysTime
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.
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). |
std.datetime.date.DateTimeException
if the given string is not in the ISO format or if the resulting SysTime
would not be valid.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, while toSimpleString
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, then std.datetime.timezone.LocalTime
is used. If the time zone is "Z", then UTC
is used. Otherwise, a std.datetime.timezone.SimpleTimeZone
which corresponds to the given offset from UTC is used. To get the returned SysTime
to be a particular time zone, pass in that time zone and the SysTime
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.
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). |
std.datetime.date.DateTimeException
if the given string is not in the ISO format or if the resulting SysTime
would not be valid.Returns the SysTime
farthest in the past which is representable by SysTime
.
The SysTime
which is returned is in UTC.
Returns the SysTime
farthest in the future which is representable by SysTime
.
The SysTime
which is returned is in UTC.
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 what SysTime
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 for core.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.
long unixTime
| The unix time to convert. |
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()));
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 what SysTime
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 for core.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 (so int.max
if it goes over and int.min
if it goes under). However, no attempt is made to deal with integer overflow if the return type is long.
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. |
// 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
This function is Windows-Only.
Converts a SYSTEMTIME
struct to a SysTime
.
SYSTEMTIME* st
| The SYSTEMTIME struct to convert. |
TimeZone tz
| The time zone that the time in the SYSTEMTIME struct is assumed to be (if the SYSTEMTIME was supplied by a Windows system call, the SYSTEMTIME will either be in local time or UTC, depending on the call). |
std.datetime.date.DateTimeException
if the given SYSTEMTIME
will not fit in a SysTime
, which is highly unlikely to happen given that SysTime.max
is in 29,228 A.D. and the maximum SYSTEMTIME
is in 30,827 A.D.This function is Windows-Only.
Converts a SysTime
to a SYSTEMTIME
struct.
The SYSTEMTIME
which is returned will be set using the given SysTime
's time zone, so to get the SYSTEMTIME
in UTC, set the SysTime
's time zone to UTC.
SysTime sysTime
| The SysTime to convert. |
std.datetime.date.DateTimeException
if the given SysTime
will not fit in a SYSTEMTIME
. This will only happen if the SysTime
's date is prior to 1601 A.D.This function is Windows-Only.
Converts a FILETIME
struct to the number of hnsecs since midnight, January 1st, 1 A.D.
FILETIME* ft
| The FILETIME struct to convert. |
std.datetime.date.DateTimeException
if the given FILETIME
cannot be represented as the return value.This function is Windows-Only.
Converts a FILETIME
struct to a SysTime
.
FILETIME* ft
| The FILETIME struct to convert. |
TimeZone tz
| The time zone that the SysTime will be in (FILETIME s are in UTC). |
std.datetime.date.DateTimeException
if the given FILETIME
will not fit in a SysTime
.This function is Windows-Only.
Converts a number of hnsecs since midnight, January 1st, 1 A.D. to a FILETIME
struct.
long stdTime
| The number of hnsecs since midnight, January 1st, 1 A.D. UTC. |
std.datetime.date.DateTimeException
if the given value will not fit in a FILETIME
.This function is Windows-Only.
Converts a SysTime
to a FILETIME
struct.
FILETIME
s are always in UTC.
SysTime sysTime
| The SysTime to convert. |
std.datetime.date.DateTimeException
if the given SysTime
will not fit in a FILETIME
.Type representing the DOS file date/time format.
Converts from DOS file date/time to SysTime
.
DosFileTime dft
| The DOS file time to convert. |
TimeZone tz
| The time zone which the DOS file time is assumed to be in. |
std.datetime.date.DateTimeException
if the DosFileTime
is invalid.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))
Converts from SysTime
to DOS file date/time.
SysTime sysTime
| The SysTime to convert. |
std.datetime.date.DateTimeException
if the given SysTime
cannot be converted to a DosFileTime
.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
The given array of char
or random-access range of char
or ubyte
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 corresponding SysTime
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), a std.datetime.timezone.SimpleTimeZone
with a utc offset of 0
is used rather than std.datetime.timezone.UTC
, whereas "+0000"
uses std.datetime.timezone.UTC
.
Note that because SysTime
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.
std.datetime.date.DateTimeException
if the given string doesn't follow the grammar for a date-time field or if the resulting SysTime
is invalid.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–2019 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/std_datetime_systime.html