|
NAMEClass::Date - Class for easy date and time manipulationVERSIONversion 1.1.17SYNOPSISuse Class::Date qw(:errors date localdate gmdate now -DateParse -EnvC); # creating absolute date object (local time) $date = Class::Date->new( [$year,$month,$day,$hour,$min,$sec]); $date = date [$year,$month,$day,$hour,$min,$sec]; # ^- "date" is an exportable function, the same as Class::Date->new $date = date { year => $year, month => $month, day => $day, hour => $hour, min => $min, sec => $sec }; $date = date "2001-11-12 07:13:12"; $date = localdate "2001-12-11"; $date = now; # the same as date(time) $date = date($other_date_object); # cloning ... # creating absolute date object (GMT) $date = Class::Date->new( [$year,$month,$day,$hour,$min,$sec],'GMT'); $date = gmdate "2001-11-12 17:13"; ... # creating absolute date object in any other timezone $date = Class::Date->new( [$year,$month,$day,$hour,$min,$sec],'Iceland' ); $date = date "2001-11-12 17:13", 'Iceland'; $date2 = $date->new([$y2, $m2, $d2, $h2, $m2, $s2]); # ^- timezone is inherited from the $date object # creating relative date object # (normally you don't need to create this object explicitly) $reldate = Class::Date::Rel->new( "3Y 1M 3D 6h 2m 4s" ); $reldate = Class::Date::Rel->new( "6Y" ); $reldate = Class::Date::Rel->new( $secs ); # secs $reldate = Class::Date::Rel->new( [$year,$month,$day,$hour,$min,$sec] ); $reldate = Class::Date::Rel->new( { year => $year, month => $month, day => $day, hour => $hour, min => $min, sec => $sec } ); $reldate = Class::Date::Rel->new( "2001-11-12 07:13:12" ); $reldate = Class::Date::Rel->new( "2001-12-11" ); # getting values of an absolute date object $date; # prints the date in default output format (see below) $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->_wday; # 0 = Sunday $date->day_of_week; # same as prev. $date->yday; $date->day_of_year; # same as prev. $date->isdst; # DST? $date->daylight_savings; # same as prev. $date->epoch; # UNIX time_t $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, look below) "$date" # same as prev. $date->tzoffset # timezone-offset $date->strftime($format) # POSIX strftime (without the huge POSIX.pm) $date->tz # returns the base timezone as you specify, eg: CET $date->tzdst # returns the real timezone with dst information, eg: CEST ($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->href; # $href can be reused as a constructor print $hash->{year}."-".$hash->{month}. ... $hash->{sec} ... ; %hash=$date->hash; # !! $hash{year}: 1900-, $hash{month}: 1-12 $date->month_begin # First day of the month (date object) $date->month_end # Last day of the month $date->days_in_month # 28..31 # constructing new date based on an existing one: $new_date = $date->clone; $new_date = $date->clone( year => 1977, sec => 14 ); # valid keys: year, _year, month, mon, _month, _mon, day, mday, day_of_month, # hour, min, minute, sec, second, tz # constructing a new date, which is the same as the original, but in # another timezone: $new_date = $date->to_tz('Iceland'); # changing date format { local $Class::Date::DATE_FORMAT="%Y%m%d%H%M%S"; print $date # result: 20011222000000 $Class::Date::DATE_FORMAT=undef; print $date # result: Thu Oct 13 04:54:34 1994 $Class::Date::DATE_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_INVALID) { ... } print $a->errstr; } # adjusting DST in calculations (see the doc) $Class::Date::DST_ADJUST = 1; # this is the default $Class::Date::DST_ADJUST = 0; # "month-border adjust" flag $Class::Date::MONTH_BORDER_ADJUST = 0; # this is the default print date("2001-01-31")+'1M'; # will print 2001-03-03 $Class::Date::MONTH_BORDER_ADJUST = 1; print date("2001-01-31")+'1M'; # will print 2001-02-28 # date range check $Class::Date::RANGE_CHECK = 0; # this is the default print date("2001-02-31"); # will print 2001-03-03 $Class::Date::RANGE_CHECK = 1; print date("2001-02-31"); # will print nothing # getting values of a relative date object $reldate; # reldate in seconds (assumed 1 month = 2_629_744 secs) $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->sec_part; # "second" part of the relative date $reldate->mon_part; # "month" part of the relative date # 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 $date4 = $date+'3-1-5' # $date plus 3 years, 1 months, 5 days $new_date = $date-$reldate; $date2 = $date-'3Y'; # 3 Yearss $date3 = $date-[1,2,3]; # $date minus 1 year, 2 months, 3 days $date4 = $date-'3-1-5' # $date minus 3 years, 1 month, 5 days $new_reldate = $date1-$date2; $reldate2 = Class::Date->new('2000-11-12')-'2000-11-10'; $reldate3 = $date3-'1977-11-10'; $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->add($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 # Disabling Class::Date warnings at load time BEGIN { $Class::Date::WARNINGS=0; } use Class::Date; DESCRIPTIONThis module is intended to provide a general-purpose date and datetime type for perl. You have a Class::Date class for absolute date and datetime, and have a Class::Date::Rel class for relative dates.You can use "+", "-", "<" and ">" operators as with native perl data types. Note that this module is fairly ancient and dusty. You might want to take a look at DateTime and its related modules for a more standard, and maintained, Perl date manipulation solution. USAGEIf you want to use a date object, you need to do the following:- create a new object - do some operations (+, -, comparison) - get result back Creating a new date objectYou can create a date object by the "date", "localdate" or "gmdate" function, or by calling the Class::Date constructor."date" and "Class::Date->new" are equivalent, both has two arguments: The date and the timezone. $date1= date [2000,11,12]; $date2= Class::Date->new([2000,06,11,13,11,22],'GMT'); $date2= $date1->new([2000,06,11,13,11,22]); If the timezone information is omitted, then it first check if "new" is called as an object method or a class method. If it is an object method, then it inherits the timezone from the base object, otherwise the default timezone is used ($Class::Date::DEFAULT_TIMEZONE), which is usually set to the local timezone (which is stored in $Class::Date::LOCAL_TIMEZONE). These two variables are set only once to the value, which is returned by the Class::Date::local_timezone() function. You can change these values whenever you want. "localdate $x" is equivalent to "date $x, $Class::Date::LOCAL_TIMEZONE", "gmdate $x" is equivalent to "date $x, $Class::Date::GMT_TIMEZONE". $Class::Date::GMT_TIMEZONE is set to 'GMT' by default. $date1= localdate [2000,11,12]; $date2= gmdate [2000,4,2,3,33,33]; $date = localdate(time); The format of the accepted input date can be:
Operations
Accessing data from a Class::Date and Class::Date::Rel objectYou can use the methods methods described at the top of the document if you want to access parts of the data which is stored in a Class::Date and Class::Date::Rel object.Error handlingIf a date object became invalid, then the object will be reblessed to Class::Date::Invalid. This object is false in boolean environment, so you can test the date validity like this:$a = date($input_date); if ($a) { # valid date ... } else { # invalid date if ($a->error == E_INVALID) { ... } print $a->errstr; } Note even the date is invalid, the expression "defined $a" always returns true, so the following is wrong: $a = date($input_date); if (defined $a) ... # WRONG!!!! You can test the error by getting the $date->error value. You might import the ":errors" tag: use Class::Date qw(:errors); Possible error values are:
You can get the error in string form by calling the "errstr" method. DST_ADJUST$DST_ADJUST is an important configuration option.If it is set to true (default), then the module adjusts the date and time when the operation switches the border of DST. With this setting, you are ignoring the effect of DST. When $DST_ADJUST is set to false, then no adjustment is done, the calculation will be based on the exact time difference. You will see the difference through an example: $Class::Date::DST_ADJUST=1; print date("2000-10-29", "CET") + "1D"; # This will print 2000-10-30 00:00:00 print date("2001-03-24 23:00:00", "CET") + "1D"; # This will be 2001-03-25 23:00:00 print date("2001-03-25", "CET") + "1D"; # This will be 2001-03-26 00:00:00 $Class::Date::DST_ADJUST=0; print date("2000-10-29", "CET") + "1D"; # This will print 2000-10-29 23:00:00 print date("2001-03-24 23:00:00", "CET") + "1D"; # This will be 2001-03-26 00:00:00 MONTHS AND YEARSIf you add or subtract "months" and "years" to a date, you may get wrong dates, e.g when you add one month to 2001-01-31, you expect to get 2001-02-31, but this date is invalid and converted to 2001-03-03. Thats' whydate("2001-01-31") + '1M' - '1M' != "2001-01-31" This problem can occur only with months and years, because others can easily be converted to seconds. MONTH_BORDER_ADJUST$MONTH_BORDER_ADJUST variable is used to switch on or off the month-adjust feature. This is used only when someone adds months or years to a date and then the resulted date became invalid. An example: adding one month to "2001-01-31" will result "2001-02-31", and this is an invalid date.When $MONTH_BORDER_ADJUST is false, this result simply normalized, and becomes "2001-03-03". This is the default behaviour. When $MONTH_BORDER_ADJUST is true, this result becomes "2001-02-28". So when the date overflows, then it returns the last day insted. Both settings keep the time information. TIMEZONE SUPPORTSince 1.0.11, Class::Date handle timezones natively on most platforms (see the BUGS AND LIMITATIONS section for more info).When the module is loaded, then it determines the local base timezone by calling the Class::Date::local_timezone() function, and stores these values into two variables, these are: $Class::Date::LOCAL_TIMEZONE and $Class::Date::DEFAULT_TIMEZONE. The first value is used, when you call the "localdate" function, the second value is used, when you call the "date" function and you don't specify the timezone. There is a $Class::Date::GMT_TIMEZONE function also, which is used by the "gmdate" function, this is set to 'GMT'. You can query the timezone of a date object by calling the $date->tz method. Note this value returns the timezone as you specify, so if you create the object with an unknown timezone, you will get this back. If you want to query the effective timezone, you can call the $date->tzdst method. This method returns only valid timezones, but it is not necessarily the timezone which can be used to create a new object. For example $date->tzdst can return 'CEST', which is not a valid base timezone, because it contains daylight savings information also. On Linux systems, you can see the possible base timezones in the /usr/share/zoneinfo directory. In Class::Date 1.1.6, a new environment variable is introduced: $Class::Date::NOTZ_TIMEZONE. This variable stores the local timezone, which is used, when the TZ environment variable is not set. It is introduced, because there are some systems, which cannot handle the queried timezone well. For example the local timezone is CST, it is returned by the tzname() perl function, but when I set the TZ environment variable to CST, it works like it would be GMT. The workaround is NOTZ_TIMEZONE: if a date object has a timezone, which is the same as NOTZ_TIMEZONE, then the TZ variable will be removed before each calculation. In normal case, it would be the same as setting TZ to $NOTZ_TIMEZONE, but some systems don't like it, so I decided to introduce this variable. The $Class::Date::NOTZ_TIMEZONE variable is set in the initialization of the module by removing the TZ variable from the environment and querying the tzname variable. INTERNALSThis module uses operator overloading very heavily. I've found it quite stable, but I am afraid of it a bit.A Class::Date object is an array reference. A Class::Date::Rel object is an array reference, which contains month and second information. I need to store it as an array ref, because array and month values cannot be converted into seconds, because of our super calendar. You can add code references to the @Class::Date::NEW_FROM_SCALAR and @Class::Date::Rel::NEW_FROM_SCALAR. These arrays are iterated through when a scalar-format date must be parsed. These arrays only have one or two values at initialization. The parameters which the code references got are the same as the "new" method of each class. In this way, you can personalize the date parses as you want. As of 0.90, the Class::Date has been rewritten. A lot of code and design decision has been borrowed from Matt Sergeant's Time::Object, and there will be some incompatibility with the previous public version (0.5). I tried to keep compatibility methods in Class::Date. If you have problems regarding this, please drop me an email with the description of the problem, and I will set the compatibility back. Invalid dates are Class::Date::Invalid objects. Every method call on this object and every operation with this object returns undef or 0. DEVELOPMENT FOCUSThis module tries to be as full-featured as can be. It currently lacks business-day calculation, which is planned to be implemented in the 1.0.x series.I try to keep this module not to depend on other modules and I want this module usable without a C compiler. Currently the module uses the POSIX localtime function very extensively. This makes the date calculation a bit slow, but provides a rich interface, which is not provided by any other module. When I tried to redesign the internals to not depend on localtime, I failed, because there are no other way to determine the daylight savings information. SPEED ISSUESThere are two kind of adjustment in this module, DST_ADJUST and MONTH_BORDER_ADJUST. Both of them makes the "+" and "-" operations slower. If you don't need them, switch them off to achieve faster calculations.In general, if you really need fast date and datetime calculation, don't use this module. As you see in the previous section, the focus of development is not the speed in 1.0. For fast date and datetime calculations, use Date::Calc module instead. THREAD SAFETY and MOD_PERLThis module is NOT thread-safe, since it uses C library functions, which are not thread-safe. Using this module in a multi-threaded environment can cause timezones to be messed up. I did not put any warning about it, you have to make sure that you understand this!Under some circumstances in a mod_perl environment, you require the Env::C module to set the TZ variable properly before calling the time functions. I added the -EnvC import option to automatically load this module if it is not loaded already. Please read the mod_perl documentation about the environment variables and mod_perl to get the idea why it is required sometimes: http://perl.apache.org/docs/2.0/user/troubleshooting/troubleshooting.html#C_Libraries_Don_t_See_C__ENV__Entries_Set_by_Perl_Code You are sure have this problem if the $Class::Date::NOTZ_TIMEZONE variable is set to 'UTC', althought you are sure that your timezone is not that. Try -EnvC in this case, but make sure that you are not using it in a multi-threaded environment! OTHER BUGS AND LIMITATIONS
SUPPORTClass::Date is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.If you have questions, you can send questions directly to me: dlux@dlux.hu WIN32 notesYou can get a binary win32 version of Class::Date from Chris Winters' .ppd repository with the following commands:For people using PPM2: c:\> ppm PPM> set repository oi http://openinteract.sourceforge.net/ppmpackages/ PPM> set save PPM> install Class-Date For people using PPM3: c:\> ppm PPM> repository http://openinteract.sourceforge.net/ppmpackages/ PPM> install Class-Date The first steps in PPM only needs to be done at the first time. Next time you just run the 'install'. COPYRIGHTCopyright (c) 2001 Szabó, Balázs (dLux)All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Portions Copyright (c) Matt Sergeant CREDITS- Matt Sergeant <matt@sergeant.org> (Lots of code are borrowed from the Time::Object module) - Tatsuhiko Miyagawa <miyagawa@cpan.org> (bugfixes) - Stas Bekman <stas@stason.org> (suggestions, bugfix) - Chris Winters <chris@cwinters.com> (win32 .ppd version) - Benoit Beausejour <bbeausej@pobox.com> (Parts of the timezone code is borrowed from his Date::Handler module) SEE ALSOperl(1). Date::Calc(3pm). Time::Object(3pm). Date::Handler(3pm).AUTHORS
COPYRIGHT AND LICENSEThis software is copyright (c) 2018, 2014, 2010, 2003 by Balázs Szabó.This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Visit the GSP FreeBSD Man Page Interface. |