std.date proposal
Fredrik Olsson
peylow at gmail.com
Tue Mar 28 08:39:55 PST 2006
I have created a proposal for a std.date replacement. And announce it
here in hopes of some comments and criticism.
The parse code in converted from the PostgreSQL C code base, and is
quite competent. If SQL99 supports it, then PostgreSQL does. Thanks goes
to the postgres hackers.
The philosophy is that date and time can be represented with four basic
types:
d_date - For a date with day precision.
d_time - For a time with millisecond precision (No date part).
d_timestamp - For a date+time with better than millisecond precision for
dates close to present day.
d_duration - A fixed of relative duration of time. A months and a year,
is relative as no two months are guarantied to have the same
length, while weeks and hours are fixed (No support for leap
seconds :) ).
The module is kept simple and consistent, all types are handled with
just a few functions:
toType() - functions, with many overloads to make a time/date/duration
from a string, different time units, Unix epoch, and more. For
example toTime(hour, minute, second) and toDuration(string).
age() - functions with overloads to get the duration between two dates,
times, or timestamps in fixed or relative units. For example:
age(toDate("2006-02-01"), toDate("2006-03-01");
will give "28 days", while:
age(toDate("2006-02-01"), toDate("2006-03-01", true);
Will give "1 month".
splitType() - functions with overloads, to split type into it's
components. For example:
int year, dayOfYear;
splitDate(toDate("2006-10-28"), year, dayOfYear);
extract() - With overloads, extract a component of a type, or converts
the type to special representations. Example:
extract(toDate("2006-03-28"), DatePart.WEEKDAY);
will give WeekDay.TUESDAY, and:
extract(toDate("2006-03-28"), DatePart.EPOCH);
will give 1143496800.
increment() - With overloads to add both a single date part, and
duration to each type. For example:
increment(toTime("01:02:03"), DatePart.HOUR, 4);
Will give "05:02:03". And:
increment(toDate("2006-02-12"), toDuration("3 months"));
will give "2006-05-12".
truncate() - With overloads for each type, truncates a type to a given
unit. For example:
truncate(toDate("2006-08-12"), DatePart.QUARTER);
will give "2006-06-01", and
truncate(toDate("2006-03-28"), DatePart.WEEK);
will give "2006-03-27".
toString() - With overloads for each type, converts to strings
optionally with a formatting string (Not fully implemented).
A full documentation is available at:
http://peylow.no-ip.org/~peylow/date.html
The source is available at:
http://www.dsource.org/projects/dlisp/browser/branches?rev=51
Comments om implementation, documents and so forth are requested. I have
no plans to ad any more functions as I like to keep simple things
simple. But I do plan to add more "DatePart"'s, for example JULIANDAY
for all the astronomers, and also extract date parts from other
calendars, such as Arabic etc.
I have intentionaly not included conversion of weekdays and month into
english names. As I believe that is up to the GUI, and locale parts of
an applicition, not the low level lib.
regards
Fredrik Olsson
More information about the Digitalmars-d-announce
mailing list