D Programming Language 2.0

Last update Wed Apr 11 21:24:27 2012


Deprecated. It will be removed in March 2012. Please use std.datetime instead.

Dates are represented in several formats. The date implementation revolves around a central type, d_time, from which other formats are converted to and from. Dates are calculated using the Gregorian calendar.

Gregorian calendar (Wikipedia)

Boost License 1.0.

Walter Bright


deprecated alias d_time;
d_time is a signed arithmetic type giving the time elapsed since January 1, 1970. Negative values are for dates preceding 1970. The time unit used is Ticks. Ticks are milliseconds or smaller intervals.

The usual arithmetic operations can be performed on d_time, such as adding, subtracting, etc. Elapsed time in Ticks can be computed by subtracting a starting d_time from an ending d_time.

deprecated d_time d_time_nan;
A value for d_time that does not represent a valid time.

struct Date;
Time broken down into its components.

int year;
use int.min as "nan" year value

int month;

int day;

int hour;

int minute;

int second;

int ms;

int weekday;
0: not specified, 1..7: Sunday..Saturday

int tzcorrection;
-1200..1200 correction in hours

void parse(string s);
Parse date out of string s[] and store it in this Date instance.

Will be at least 1000

deprecated void toISO8601YearWeek(d_time t, out int year, out int week);
Compute year and week [1..53] from t. The ISO 8601 week 1 is the first week of the year that includes January 4. Monday is the first day of the week.

ISO 8601 (Wikipedia)

deprecated int hourFromTime(d_time time);
Calculates the hour from time.

d_time time The time to compute the hour from.

The calculated hour, 0..23.

deprecated int minFromTime(d_time time);
Calculates the minute from time.

d_time time The time to compute the minute from.

The calculated minute, 0..59.

deprecated int secFromTime(d_time time);
Calculates the second from time.

d_time time The time to compute the second from.

The calculated second, 0..59.

deprecated int msFromTime(d_time time);
Calculates the milisecond from time.

d_time time The time to compute the milisecond from.

The calculated milisecond, 0..999.

deprecated pure uint daysInYear(uint year);
Calculates the number of days that exists in a year.

Leap years have 366 days, while other years have 365.

uint year The year to compute the number of days from.

The number of days in the year, 365 or 366.

deprecated pure int dayFromYear(int year);
Calculates the number of days elapsed since 1 January 1970 until 1 January of the given year.

int year The year to compute the number of days from.

The number of days elapsed.

 writeln(dayFromYear(1970)); // writes '0'
 writeln(dayFromYear(1971)); // writes '365'
 writeln(dayFromYear(1972)); // writes '730'

deprecated pure int yearFromTime(d_time t);
Calculates the year from the d_time t.

deprecated pure bool inLeapYear(d_time t);
Determines if d_time t is a leap year.

A leap year is every 4 years except years ending in 00 that are not divsible by 400.

!=0 if it is a leap year.


deprecated int monthFromTime(d_time t);
Calculates the month from the d_time t.

Integer in the range 0..11, where 0 represents January and 11 represents December.

deprecated int dateFromTime(d_time t);
Compute which day in a month a d_time t is.

Integer in the range 1..31

deprecated int weekDay(d_time t);
Compute which day of the week a d_time t is.

Integer in the range 0..6, where 0 represents Sunday and 6 represents Saturday.

deprecated d_time UTCtoLocalTime(d_time t);
Convert from UTC to local time.

deprecated d_time localTimetoUTC(d_time t);
Convert from local time to UTC.

deprecated int dateFromNthWeekdayOfMonth(int year, int month, int weekday, int n);
Determine the date in the month, 1..31, of the nth weekday.

int year year
int month month, 1..12
int weekday day of week 0..6 representing Sunday..Saturday
int n nth occurrence of that weekday in the month, 1..5, where 5 also means "the last occurrence in the month"

the date in the month, 1..31, of the nth weekday

deprecated int daysInMonth(int year, int month);
Determine the number of days in a month, 1..31.

int month 1..12

deprecated string UTCtoString(d_time time);
Converts UTC time into a text string of the form: "Www Mmm dd hh:mm:ss GMT+-TZ yyyy". For example, "Tue Apr 02 02:04:57 GMT-0800 1996". If time is invalid, i.e. is d_time_nan, the string "Invalid date" is returned.

  d_time lNow;
  char[] lNowString;

  // Grab the date and time relative to UTC
  lNow = std.date.getUTCtime();
  // Convert this into the local date and time for display.
  lNowString = std.date.UTCtoString(lNow);

deprecated alias toString;
Alias for UTCtoString (deprecated).

deprecated string toUTCString(d_time t);
Converts t into a text string of the form: "Www, dd Mmm yyyy hh:mm:ss UTC". If t is invalid, "Invalid date" is returned.

deprecated string toDateString(d_time time);
Converts the date portion of time into a text string of the form: "Www Mmm dd yyyy", for example, "Tue Apr 02 1996". If time is invalid, "Invalid date" is returned.

deprecated string toTimeString(d_time time);
Converts the time portion of t into a text string of the form: "hh:mm:ss GMT+-TZ", for example, "02:04:57 GMT-0800". If t is invalid, "Invalid date" is returned. The input must be in UTC, and the output is in local time.

deprecated d_time parse(string s);
Parses s as a textual date string, and returns it as a d_time. If the string is not a valid date, d_time_nan is returned.

deprecated d_time getUTCtime();
Get current UTC time.

deprecated alias DosFileTime;
Type representing the DOS file date/time format.

deprecated d_time toDtime(DosFileTime time);
Convert from DOS file date/time to d_time.

deprecated DosFileTime toDosFileTime(d_time t);
Convert from d_time to DOS file date/time.

ulong[] benchmark(fun...)(uint times, ulong[] result = null);
Benchmarks code for speed assessment and comparison.

fun aliases of callable objects (e.g. function names). Each should take no arguments.
times The number of times each function is to be executed.
result The optional store for the return value. If null is passed in, new store is allocated appropriately.

An array of n uints. Element at slot i contains the number of milliseconds spent in calling the ith function times times.

int a;
void f0() { }
void f1() { auto b = a; }
void f2() { auto b = to!(string)(a); }
auto r = benchmark!(f0, f1, f2)(10_000_000);