www.digitalmars.com

D Programming Language 2.0

Last update Thu May 12 16:55:05 2011

std.date

Scheduled for deprecation. 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.

References:
Gregorian calendar (Wikipedia)

License:
Boost License 1.0.

Authors:
Walter Bright

Source:
std/date.d

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.

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;
1..12

int day;
1..31

int hour;
0..23

int minute;
0..59

int second;
0..59

int ms;
0..999

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.

ticksPerSecond
Will be at least 1000

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.

References:
ISO 8601 (Wikipedia)

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

Parameters:
d_time time The time to compute the hour from.

Returns:
The calculated hour, 0..23.

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

Parameters:
d_time time The time to compute the minute from.

Returns:
The calculated minute, 0..59.

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

Parameters:
d_time time The time to compute the second from.

Returns:
The calculated second, 0..59.

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

Parameters:
d_time time The time to compute the milisecond from.

Returns:
The calculated milisecond, 0..999.

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

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

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

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

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

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

Returns:
The number of days elapsed.

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

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

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.

Returns:
!=0 if it is a leap year.

References:
Wikipedia

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

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

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

Returns:
Integer in the range 1..31

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

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

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

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

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

Parameters:
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"

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

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

Parameters:
int month 1..12

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.

Example:
  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).

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.

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.

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.

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.

d_time getUTCtime();
Get current UTC time.

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

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

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.

Parameters:
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.

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

Example:
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);