Utilities for handling dates and times in various formats.
We support the following time formats
sql_date yyyy-mm-dd
iso_date yyyy-mm-dd
sql_datetime yyyy-mm-dd hh:mm:ss
time hh:mm AM/PM
time 24hh:mm (24-hour clock)
hms hh:mm:ss (24-hour clock)
sql_timestamp yyyymmddhhmmss OR yyyy-mm-dd hh:mm:ss *
unix_timestamp nnnnnnnnnnn
date mmm dd, yyyy
date_long Month dd, yyyy
datetime wday Mon dd hh:mm yyyy
datetime_long Weekday Month dd hh:mm yyyy
datetime2 hh:mm AM/PM, Month dd, yyyy
dd-mmm-yy dd-Mon-yy
weekday wday
weekday_long Weekday
year yyyy
month Mon
month_long Month
raw [y,m,d,h,m,s]
* depends on $config{timestamp_format}, which normally depends on which which version of MySQL you are running.
Internally, we convert all dates/times to a 6-element time array: y, m, d, h, m, s. The time array is initialized to the current datetime, so if you only set a partial datetime (eg. only the time), the remaining values are unchanged, and the full array still represents a complete datetime.
Make a time object (initialized to current time):
my $t = new ExSite::Time;
Make a time object (initialized to some other time/format):
my $t = new ExSite::Time("2001-12-25 09:00:00","sql_datetime");
Set the time to the current time:
$t->set;
Set the time to some time/format:
$t->set('Feb 1, 1968','date');
$t->set('6:30 pm','time');
$t->set('Fri Aug 26 12:14:43 2005','datetime');
adjust the time by some amount:
$t->add(30,'days');
$t->add(-3,'months');
NB: some time units are not precisely defined, so we must approximate. What we do is define 1 month as 1/12 of a year, and 1 year as 365 days. These may not give expected results. For example, adding 1 month will not leave you at the same time of day, nor on the same day of the following month. (To understand why this is correct, consider what the date should be if are on Jan 31, and we add 1 month.)
Output the time in some format:
print $t->write('date');
print $t->write('unix_timestamp');
print $t->write('weekday');
Compare another time to the currently-set time: ... returns 1 if the compared time is in the future, -1 if it is in the past, and 0 if it is the same as the current time.
$t->compare('20050826091500','sql_timestamp');
Convenience call to compare two times in same format:
$t->compare2('Aug 26, 2005','Aug 28, 2005','datetime');
There are also a number of legacy calls to emulate an older Time package.