D Programming Language 2.0

- Comments
- D
- Search
- Home

Last update Sun Apr 19 16:54:11 2009

# std.date

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)

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 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 toString(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.toString(lNow);

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.

uint[] benchmark(fun...)(uint times, uint[] 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);

Copyright © 1999-2009 by Digital Mars, All Rights Reserved | Page generated by Ddoc.