Panda::Date - fast Date framework in C.
Panda::Date is almost fully compatible with Class::Date, but has several more features and much greater perfomance. It is written fully in C/C++.
Panda::Date supports dates between -2147483648/01/01 00:00:00 and 2147483647/12/31 23:59:59.
With Panda::Date you can perform some operations even faster than in plain C program using stdlibc functions. See Panda::Time why.
use Panda::Time qw/tzset/; use Panda::Date qw/now date today rdate idate :const/; my $date = Panda::Date->new($epoch); # using server's local timezone $date = Panda::Date->new([$y,$m,$d,$h,$m,$s]); $date = Panda::Date->new({year => $y, month => $m, day => $d, hour => $h, min => $m, sec => $s}); $date = Panda::Date->new("2013-03-05 23:45:56"); $date = now(); # same as Panda::Date->new(time()) but faster $date = today(); # same as Panda::Date->new(time())->truncate but faster # create using function 'date' tzset('Europe/Moscow'); # using 'Europe/Moscow' as server's local timezone $date = date [$year,$month,$day,$hour,$min,$sec]; $date = date { year => $year, month => $month, day => $day, hour => $hour, min => $min, sec => $sec }; $date = date "2001/11/12 07:13:12"; $date = date 123456789; $date = date("2001/11/12 07:13:12", 'America/New_York'); # $date operates in custom time zone # creating relative date object # (normally you don't need to create this object explicitly) $reldate = new Panda::Date::Rel "3Y 1M 3D 6h 2m 4s"; $reldate = new Panda::Date::Rel "6Y"; $reldate = new Panda::Date::Rel $secs; # secs $reldate = new Panda::Date::Rel [$year,$month,$day,$hour,$min,$sec]; $reldate = new Panda::Date::Rel { year => $year, month => $month, day => $day, hour => $hour, min => $min, sec => $sec }; $reldate = rdate "-1M -3D 6h"; $reldate = 3*MONTH; # "3M" $reldate = 2*YEAR + MONTH - 30*DAY; # "2Y 1M -30D" print $reldate/2; # 1Y 5h 14m 32s $reldate = YEAR/2 + HOUR/2; # 6M 30m $date; # prints the date in default output format (ISO/SQL format) $date->epoch; # unix timestamp $date->year; # year, e.g: 2001 $date->_year; # year - 1900, e.g. 101 $date->yr; # 2-digit year 0-99, e.g 1 $date->mon; # month 1..12 $date->month; # same as prev. $date->_mon; # month 0..11 $date->_month; # same as prev. $date->day; # day of month $date->mday; # day of month $date->day_of_month;# same as prev. $date->hour; $date->min; $date->minute; # same as prev. $date->sec; $date->second; # same as prev. $date->wday; # 1 = Sunday $date->day_of_week; # same as prev. $date->_wday; # 0 = Sunday $date->ewday; # 1 = Monday, 7 = Sunday $date->yday; # [1-366] $date->day_of_year; # same as prev. $date->_yday; # [0-365] $date->isdst; # DST? $date->daylight_savings; # same as prev. $date->strftime($format); $date->monname; # name of month, eg: March $date->monthname; # same as prev. $date->wdayname; # Thursday $date->day_of_weekname # same as prev. $date->hms # 01:23:45 $date->ymd # 2000/02/29 $date->mdy # 02/29/2000 $date->dmy # 29/02/2000 $date->meridiam # 01:23 AM $date->ampm # AM/PM $date->string # 2000-02-29 12:21:11 (format can be changed) "$date" # same as prev. $date->iso # 2000-02-29 12:21:11 $date->gmtoff # current offset from UTC (in seconds) $date->tzname # returns the timezone name (EST, EET, etc) $date->tzlocal # true if $date is in local time zone $date->tz # returns timezone info $date->tz('GMT+5') # changes $date's timezone (saving YMDhms) $date->to_tz('UTC') # changes $date's timezone (saving epoch) ($year,$month,$day,$hour,$min,$sec)=$date->array; ($year,$month,$day,$hour,$min,$sec)=@{ $date->aref }; # !! $year: 1900-, $month: 1-12 ($sec,$min,$hour,$day,$mon,$year,$wday,$yday,$isdst)=$date->struct; ($sec,$min,$hour,$day,$mon,$year,$wday,$yday,$isdst)=@{ $date->sref }; # !! $year: 0-, $month: 0-11 %hash=$date->hash; # !! $hash{year}: 1900-, $hash{month}: 1-12 $hash=$date->href; # $hash can be reused as a constructor print $hash->{year}."-".$hash->{month}. ... $hash->{sec} ... ; # constructing new date based on an existing one: $new_date = $date->clone; $new_date = $date->clone({year => 1977, sec => 14, tz => 'Australia/Melbourne'}); # valid keys: year, _year, month, _month, day, hour, min, sec, tz $date->month_begin # First day of the month (date object) $date->month_end # Last day of the month $date->days_in_month # 28..31 # changing date stringify format globally Panda::Date::string_format("%Y%m%d%H%M%S"); print $date # result: 20011222000000 Panda::Date::string_format(undef); print $date # result: 2000-02-29 12:21:11 Panda::Date::string_format("%Y/%m/%d"); print $date # result: 1994/10/13 # error handling $a = date($date_string); if ($a) { # valid date ... } else { # invalid date if ($a->error == E_UNPARSABLE) { ... } print $a->errstr; } # date range check Panda::Date::range_check(0); # this is the default print date("2001-02-31"); # will print 2001-03-03 Panda::Date::range_check(1); print date("2001-02-31"); # will print nothing # getting values of a relative date object int($reldate); # reldate in seconds (assumed 1 month = 2_629_744 secs) "$reldate"; # reldate in "1Y 2M 3D 4h 5m 6s" format $reldate->year; $reldate->mon; $reldate->month; # same as prev. $reldate->day; $reldate->hour; $reldate->min; $reldate->minute; # same as prev. $reldate->sec; # same as $reldate $reldate->second; # same as prev. $reldate->to_sec; # relative date in seconds $reldate->to_min; # relative date in minutes $reldate->to_hour; # relative date in hours $reldate->to_day; # relative date in days $reldate->to_month; # relative date in months $reldate->to_year; # relative date in years # arithmetic with dates: print date([2001,12,11,4,5,6])->truncate; # will print "2001-12-11" $new_date = $date+$reldate; $date2 = $date+'3Y 2D'; # 3 Years and 2 days $date3 = $date+[1,2,3]; # $date plus 1 year, 2 months, 3 days $new_date = $date-$reldate; $date2 = $date-'3Y'; # 3 Yearss $date3 = $date-[1,2,3]; # $date minus 1 year, 2 months, 3 days $intdate = $date1-$date2; $intdate2 = date('2000-11-12')-'2000-11-10'; $intdate3 = $date3-'1977-11-10'; $intdate = date("2013-10-25") - "2012-03-10"; $intdate->from; # lower date in interval (2012-03-10) $intdate->till; # upper date in interval (2013-10-25) $reldate = $intdate->relative; # relative date ("1Y 7M 15D") $intdate->sec; # accurate number of seconds in interval $intdate->month; # accurate number of months in interval $reldate->to_sec; # number of seconds in relative date (inaccurate) $intdate->includes("2013-01-01") # returns -1, 0, or 1 $days_between = (Class::Date->new('2001-11-12')-'2001-07-04')->day; # comparison between absolute dates print $date1 > $date2 ? "I am older" : "I am younger"; # comparison between relative dates print $reldate1 > $reldate2 ? "I am faster" : "I am slower"; # Adding / Subtracting months and years are sometimes tricky: print date("2001-01-29") + '1M' - '1M'; # gives "2001-02-01" print date("2000-02-29") + '1Y' - '1Y'; # gives "2000-03-01" # Named interface ($date2 does not necessary to be a Class::Date object) $date1->string; # same as $date1 in scalar context $date1->subtract($date2); # same as $date1 -= $date2 $date1->subtract_new($date2); # same as $date1 - $date2 $date1->add($date2); # same as $date1 += $date2 $date1->add_new($date2); # same as $date1 + $date2 $date1->compare($date2); # same as $date1 <=> $date2 $reldate1->sec; # same as $reldate1 in numeric or scalar context $reldate1->compare($reldate2);# same as $reldate1 <=> $reldate2 $reldate1->add($reldate2); # same as $reldate1 + $reldate2 $reldate1->neg # used for subtraction
Creates a date object.
If $timezone is present, created object will operate as if tzset($timezone) was called, but without calling tzset().
tzset($timezone)
tzset()
If $timezone is absent (or undef or ""), $date uses local timezone. Further changes of local timezone via tzset() won't affect constructed object.
Treated as 64-bit UNIX timestamp. To define a date below 1970 year, use negative timestamp.
If some args are missing, will use defaults [2000,1,1,0,0,0]
If some args are missing, will use defaults defined in previous section.
A standard ISO(-like) date format. Additional ".fraction" part is ignored. Any number of trailing parameters can be missing. So the actual format is "YYYY-[MM[-DD[ HH[:MM[:SS[.MS]]]]]]" Minimal string is "YYYY-". Fractional part of seconds will be ignored. If some args are missing, will use defaults defined in previous section.
Clones another object.
If $timezone parameter is absent (or undef or ""), newly created date will use $date's timezone. Otherwise constructed date is converted to timezone $timezone preserving YMDhms information.
If there is any error while creating an object, properties error() and errstr() will be set. The object itself will return false in boolean context, empty string in string context and so on.
Same as Panda::Date->new(time()) but runs faster.
Same as Panda::Date->new(time())->truncate but runs faster.
Same as today()->epoch but runs faster.
strftime-compatible format that is used to stringify the date with '.', "", to_string(), string() or as_string(). If it's false (the default) then iso() is used.
If parts of the date are invalid or the whole date is not valid, e.g. 2001-02-31 then:
when range_check is not set (the default), then these date values are automatically converted to a valid date (normalized): 2001-03-03
when range_check is set, then a date "2001-02-31" became invalid date and error() is set to E_RANGE.
Same as Panda::Date->new($arg, [$timezone])
Same as Panda::Date::Rel->new($arg)
Same as Panda::Date::Rel->new($from, $till)
Same as Panda::Date::Int->new($arg1, $arg2)
Set date from argument or another date. This is much faster than creating new object.
UNIX timestamp (64bit)
Year [-2**31, 2**31-1]
Year (year() - 1900)
Last 2 digits of the year. [0-99]
Month [1-12]
Month [0-11]
Day of month [1-31]
[0-23]
[0-59]
[0-60]
Day of week. 1 = Sunday, 2 = Monday, ... , 7 = Saturday. If you pass an argument then another day of the same week will be set.
Day of week. 0 = Sunday, 1 = Monday, ... , 6 = Saturday. If you pass an argument then another day of the same week will be set.
Day of week (Europe-friendly). 1 = Monday, ..., 7 = Sunday. If you pass an argument then another day of the same week will be set.
Day of the year [1-366]. If you pass an argument then another day of the same year will be set.
Day of the year [0-365]. If you pass an argument then another day of the same year will be set.
Is daylight savings time in effect now (true/false).
Works like strftime from C POSIX
Full name of the month in the genitive
Full name of the day in the nominative case.
Same as strftime('%H:%M:%S') but much faster
strftime('%H:%M:%S')
Same as strftime('%Y/%m/%d') but much faster
strftime('%Y/%m/%d')
Same as strftime('%m/%d/%Y') but much faster
strftime('%m/%d/%Y')
Same as strftime('%d/%m/%Y') but much faster
strftime('%d/%m/%Y')
By default returns iso(). String format can be changed via string_format()
iso()
string_format()
Called implicitly in boolean context
if ($date) $date ? EXPR1 : EXPR2 $date && $something
Returns TRUE if date has no errors (i.e. has no parsing or out of range errors, etc), otherwise FALSE
Returns epoch() in numeric context
epoch()
Same as strftime('%Y-%m-%d %H:%M:%S') but much faster
strftime('%Y-%m-%d %H:%M:%S')
Same as strftime('%Y%m%d%H%M%S') but much faster
strftime('%Y%m%d%H%M%S')
Returns string 'AM' or 'PM'
Returns time in "11:35 AM" format (american 12h style)
Returns current timezone offset from UTC in seconds
Returns the name of the object's timezone (Europe/Moscow, America/New_York, etc).
Returns timezone abbreviation (EST, EET, etc) - may change when the date changes isdst/nodst.
Returns TRUE if this object's timezone is set as local.
With no arguments returns information about object's timezone. See "tzget([$zone])" in Panda::Time.
With argument changes the timezone of current object to $newzone preserving YMDhms information (epoch may change) and returns nothing.
Changes the timezone of current object to $newzone in a way that changed date still points to the same time moment (same epoch). YMDhms info may change. Returns nothing.
Returns 6 elements list - $year,$month,$day,$hour,$min,$sec. $year is year() [2013=2013] $month is month() [1-12]
Same as [array()] (array reference)
Returns 9 elements list - $sec,$min,$hour,$day,$mon,$year,$wday,$yday,$isdst $year is _year() [113 = 2013] $month is _month() [0-11] $wday is _wday() [0-6] $yday is _yday() [0-365]
Same as [struct()] (array reference)
Returns key-value list. Keys are 'year', 'month', 'day', 'hour', 'min', 'sec' year, month are human-friendly (2013 year, month [1-12])
Same as {href()} (hash reference)
Returns copy of the date.
If you pass a hash or array ref then date is cloned with changes described in the hash/array. Hash keys: 'year' (YYYY), 'month' [1-12], 'day', 'hour', 'min', 'sec'. Array: [$year (YYYY), $month [1-12], $day, $hour, $min, $sec]
If any values in hash or array are absent (or = undef or = -1) the appropriate field of date is not changed.
Returns the beggining of month. Only day of month is changed, HMS are preserved.
Same as month_begin_new() but changes current object instead of cloning.
month_begin_new()
Returns the end of month. Only day of month is changed, HMS are preserved.
Same as month_end_new() but changes current object instead of cloning.
month_end_new()
Returns the number of days in month
Returns error code occured during creating or cloning object (if any). If no errors returns E_OK.
Returns error string if any, otherwise undef.
Return copy of the current date with HMS set to 0. Same as clone({hour = 0, min => 0, sec => 0})>, but much faster.
clone({hour =
Same as truncate_new() but changes current object instead of cloning. It's extremely faster.
truncate_new()
Compares 2 dates and returns -1, 0 or 1. If second operand is not an object then it's created. If second operand is object but not Panda::Date then it croaks.
Adds a relative date to date object. If second operand is not an object then it's created (Panda::Date::Rel).
Same as add_new() but changes current object instead of creating new one.
add_new()
Subtracts a relative date or another date from the date object. In case of relative date the result is a Panda::Date object. Otherwise the result is Panda::Date::Int. If second operand is not an object then it's created (Panda::Date::Rel or Panda::Date).
Same as subtract() but changes current object instead of creating new one. Operand can only be a Panda::Date::Rel object.
subtract()
No errors
Wrong date string format
Invalid date (or date part) supplied when range_check() is in effect
Constant for rdate("1Y"). These (YEAR...SEC) objects are constants (read-only).
If you try to change these objects they'll croak.
Constant for rdate("1M").
Constant for rdate("1D").
Constant for rdate("1h").
Constant for rdate("1m").
Constant for rdate("1s").
See screenshot http://crazypanda.ru/v/clip2net/a/v/ri93sO22KI.png
In Class::Date it returns _wday()
In Class::Date they return [0-365]. If you need that behaviour, use _yday() method.
In Class::Date they return 13 elements: additionally _year, _month, wday, yday, isdst, epoch, minute
Class::Date's clone() receives list of key-value pairs and supports key aliases like 'mon' etc.
Panda::Date always performs all calculations with DST_ADJUST enabled.
Because it's a DATE format NOT RELATIVE.
Class::Date::Rel stringifies to approximate number of seconds in interval (useless imho)
Class::Date::Rel consists of only months and seconds.
If you have relative date "1Y 2M", year() would return 1, month() - 2, day() - 0, etc. If you need to calculate all the period in, for example, months, use to_month() (would return 14). Such calculations can be inaccurate, for example, rdate("1M")->to_sec
year()
month()
day()
to_month()
Panda::Date::Int is an Interval object and is an absolutely new term.
Storable serialization is fully supported. That means you're able to freeze Panda::Date::* objects and thaw serialized data back without losing any date information.
If you serialize a date object which was created with personal timezone (second arg to constructor), then it will be deserialized exactly in the same timezone.
If a date object is in local timezone, then it will be deserialized in local timezone too (which may differ on differrent servers), but it's guaranteed that those two dates will point to the same time moment (epoch is preserved).
For example:
tzset('Europe/Moscow'); my $date = date("2014-01-01"); my $frozen = freeze $date; tzset('America/New_York'); my $date2 = thaw $frozen; $date == $date2; # true, because $date->epoch == $date2->epoch say $date; # 2014-01-01 00:00:00 say $date2; # 2013-12-31 15:00:00
If you subclass Panda::Date it won't work correct.
You will receive SIGSEGV. If you want to clone a Panda::Date::* object, use it's clone() method.
However, cloning and serializing/deserializing via Storable is fully supported. Don't use it just to clone and object because it's 20x times slower than calling clone().
clone()
Panda::Date operates 40-70x faster than Class::Date, tests were performed on Core i7 3.2Ghz, MacOSX Lion, perl 5.12.4
my $cdate = new Class::Date("2013-06-05 23:45:56"); my $date = new Panda::Date("2013-06-05 23:45:56"); my $crel = Class::Date::Rel->new("1M"); my $rel = rdate("1M"); timethese(-1, { cdate_new_str => sub { new Class::Date("2013-01-25 21:26:43"); }, panda_new_str => sub { new Panda::Date("2013-01-25 21:26:43"); }, cdate_new_epoch => sub { new Class::Date(1000000000); }, panda_new_epoch => sub { new Panda::Date(1000000000); }, panda_new_reuse => sub { state $date = new Panda::Date(0); $date->set(1000000000); }, cdate_now => sub { Class::Date->now; }, panda_now => sub { now(); }, cdate_truncate => sub { $cdate->truncate }, panda_truncate_new => sub { $date->truncate_new }, panda_truncate => sub { $date->truncate }, cdate_today => sub { Class::Date->now->truncate; }, panda_today1 => sub { now()->truncate; }, panda_today2 => sub { today(); }, cdate_stringify => sub { $cdate->string }, panda_stringify => sub { $date->to_string }, cdate_strftime => sub { $cdate->strftime("%H:%M:%S") }, panda_strftime => sub { $date->strftime("%H:%M:%S") }, cdate_clone_simple => sub { $cdate->clone }, panda_clone_simple => sub { $date->clone }, cdate_clone_change => sub { $cdate->clone(year => 2008, month => 12) }, panda_clone_change => sub { $date->clone({year => 2008, month => 12}) }, cdate_rel_new_sec => sub { new Class::Date::Rel 1000 }, pdate_rel_new_sec => sub { new Panda::Date::Rel 1000 }, cdate_rel_new_str => sub { new Class::Date::Rel "1Y 2M 3D 4h 5m 6s" }, panda_rel_new_str => sub { new Panda::Date::Rel "1Y 2M 3D 4h 5m 6s" }, cdate_add => sub { $cdate = $cdate + '1M' }, panda_add_new => sub { $date = $date + '1M' }, panda_add => sub { $date += '1M' }, panda_add2 => sub { $date += MONTH }, panda_add3 => sub { $date->month($date->month+1) }, cdate_compare => sub { $cdate == $cdate }, panda_compare => sub { $date == $date }, }); #RESULTS #cdate_new_epoch: 2 wallclock secs ( 1.11 usr + 0.00 sys = 1.11 CPU) @ 59609.01/s (n=66166) #panda_new_epoch: 1 wallclock secs ( 1.08 usr + 0.01 sys = 1.09 CPU) @ 1485434.86/s (n=1619124) #cdate_new_str: 1 wallclock secs ( 1.09 usr + 0.01 sys = 1.10 CPU) @ 19549.09/s (n=21504) #panda_new_str: 1 wallclock secs ( 1.01 usr + 0.00 sys = 1.01 CPU) @ 1238753.47/s (n=1251141) #panda_new_reuse: 1 wallclock secs ( 1.05 usr + 0.00 sys = 1.05 CPU) @ 5242879.05/s (n=5505023) #cdate_now: 1 wallclock secs ( 1.11 usr + 0.00 sys = 1.11 CPU) @ 55350.45/s (n=61439) #panda_now: 1 wallclock secs ( 1.08 usr + 0.00 sys = 1.08 CPU) @ 1341379.63/s (n=1448690) #cdate_truncate: 1 wallclock secs ( 1.07 usr + 0.00 sys = 1.07 CPU) @ 25120.56/s (n=26879) #panda_truncate: 1 wallclock secs ( 1.02 usr + 0.00 sys = 1.02 CPU) @ 7710116.67/s (n=7864319) #panda_truncate_new: 1 wallclock secs ( 1.00 usr + 0.00 sys = 1.00 CPU) @ 1376255.00/s (n=1376255) #cdate_today: 1 wallclock secs ( 1.08 usr + 0.00 sys = 1.08 CPU) @ 16591.67/s (n=17919) #panda_today1: 1 wallclock secs ( 1.03 usr + 0.00 sys = 1.03 CPU) @ 1027823.30/s (n=1058658) #panda_today2: 1 wallclock secs ( 1.03 usr + 0.01 sys = 1.04 CPU) @ 1323322.12/s (n=1376255) #cdate_stringify: 1 wallclock secs ( 1.09 usr + 0.00 sys = 1.09 CPU) @ 92839.45/s (n=101195) #panda_stringify: 2 wallclock secs ( 1.09 usr + 0.00 sys = 1.09 CPU) @ 5072344.04/s (n=5528855) #cdate_strftime: 1 wallclock secs ( 1.06 usr + 0.00 sys = 1.06 CPU) @ 101433.02/s (n=107519) #panda_strftime: 2 wallclock secs ( 1.06 usr + 0.01 sys = 1.07 CPU) @ 1513200.00/s (n=1619124) #cdate_clone_simple: 1 wallclock secs ( 1.07 usr + 0.00 sys = 1.07 CPU) @ 26796.26/s (n=28672) #panda_clone_simple: 2 wallclock secs ( 1.03 usr + 0.00 sys = 1.03 CPU) @ 1670213.59/s (n=1720320) #cdate_clone_change: 2 wallclock secs ( 1.11 usr + 0.00 sys = 1.11 CPU) @ 25830.63/s (n=28672) #panda_clone_change: 1 wallclock secs ( 1.08 usr + 0.00 sys = 1.08 CPU) @ 637154.63/s (n=688127) #cdate_rel_new_sec: 1 wallclock secs ( 1.08 usr + 0.00 sys = 1.08 CPU) @ 245059.26/s (n=264664) #cdate_rel_new_str: 1 wallclock secs ( 1.05 usr + 0.00 sys = 1.05 CPU) @ 68265.71/s (n=71679) #pdate_rel_new_sec: 2 wallclock secs ( 1.02 usr + 0.00 sys = 1.02 CPU) @ 1420284.31/s (n=1448690) #panda_rel_new_str: 1 wallclock secs ( 1.01 usr + 0.00 sys = 1.01 CPU) @ 1238753.47/s (n=1251141) #cdate_add: 1 wallclock secs ( 1.09 usr + 0.00 sys = 1.09 CPU) @ 17934.86/s (n=19549) #panda_add: 0 wallclock secs ( 1.01 usr + 0.00 sys = 1.01 CPU) @ 4542099.01/s (n=4587520) #panda_add2: 1 wallclock secs ( 1.03 usr + 0.00 sys = 1.03 CPU) @ 4858802.91/s (n=5004567) #panda_add3: 1 wallclock secs ( 1.05 usr + 0.00 sys = 1.05 CPU) @ 2759410.48/s (n=2897381) #panda_add_new: 2 wallclock secs ( 1.06 usr + 0.00 sys = 1.06 CPU) @ 1180321.70/s (n=1251141) #cdate_compare: 1 wallclock secs ( 1.10 usr + 0.00 sys = 1.10 CPU) @ 71087.27/s (n=78196) #panda_compare: 2 wallclock secs ( 1.07 usr + 0.00 sys = 1.07 CPU) @ 3674914.95/s (n=3932159)
Pronin Oleg <syber@cpan.org>, Crazy Panda, CP Decision LTD
You may distribute this code under the same terms as Perl itself.
To install Panda::Date, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Panda::Date
CPAN shell
perl -MCPAN -e shell install Panda::Date
For more information on module installation, please visit the detailed CPAN module installation guide.