钱宇/Qian Yu > EasyDateTime > EasyDateTime

Download:
EasyDateTime-1.0.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
Report a bug
Module Version: 1.0.0   Source  

NAME ^

EasyDateTime - A date and time object

SYNOPSIS ^

    use EasyDateTime;

    # Constructors
    $dt = EasyDateTime->new({year=>2000,month=>1,day=>1,hour=>0,min=>0,sec=>0,timezone=>8});
    $dt = EasyDateTime->new('2004-08-28 08:06:00'); #date can be seperate by . / or -
    $dt = EasyDateTime->new('2004-08-28T08:06:00',8);
    $dt = EasyDateTime->new('2004/08/28 08:06:00',8); #the second param is timezone
    $dt = EasyDateTime->new('2004/8/28'); #the time will be filled 00:00:00
    $dt = EasyDateTime->new('8:6:00 '); #the date will be filled 2000-01-01
    
    # getter and setter
    $dt->year();
    $dt->year(2004);
    $dt->month();
    $dt->month(3);
    $dt->day();
    $dt->day(24);
    $dt->hour();
    $dt->hour(8);
    $dt->min();
    $dt->min(20);
    $dt->sec();
    $dt->sec(30);
    $dt->timezone();
    $dt->timezone(8);
    
    # clone & lock
    $dt2=$dt->clone(); #create new instance copy from this instance
    $dt->lock(); #set $dt read only
    
    # now localtimezone validate
    $dt=EasyDateTime->now(); #use the time of now to create a instance
    $dt=EasyDateTime->now(8); #use timezone 8 and time of now to create a instance
    print $dt->localtimezone(); #print the localtimezone of server that run this script;
    EasyDateTime->validate('2004-08-28T08:06:00'); #check whether this string is a valid datetime_str
    EasyDateTime->validate({year=>2000,month=>1,day=>1});
    
    # other func
    $dt->to_end_of_month();

    # format output
    $dt->str(); #'2004-08-28 08:06:00'
    $dt->str('%yyyy%MM'); #'200408'

    # operator overload
    $dt2=$dt+3600; #$dt2=$dt + 3600 seconds
    $dt2=$dt+{day=>1};
    $dt2=$dt+{month=>1};
     #attention sometime this func will fail when for example $dt = '2004-10-31'
     #if you want to goto [next] end of month use to_end_of_month instead
    $dt2=$dt-3000;
    $sec_count=$dt2-$dt; #return interval of $dt2 and $dt by seconeds
    $dt2=$dt-{day=>1};
        
    $dt+={day=>1};
    $dt+=3600;
    $dt-={day=>1};
    $dt-=3600;
        
    print $dt2>$dt1; #overload operator <=> and cmp, and '2004-01-01 00:00:00' > '2003-01-01 00:00:00'   

DESCRIPTION ^

    EasyDateTime is a class for the representation of date/time combinations.
    It's simple to use,and function is enough for daily use.
    Only one file ,and don't denpend on external module,and system independent.

EXAMPLES ^

This example how to get the start of month

        $dt->set{day=>1,hour=>0,min=>0,sec=>0}; #2004-08-01 00:00:00

This example how to get the end of month

   $dt->to_end_of_month(); #2004-08-31 00:00:00

This example how to get the next of month

   $dt->to_end_of_month(1); #2004-09-30 00:00:00

This example how to explore every day of month

   $dt_start=EasyDateTime->new('2004-08-01 00:00:00');
   $dt_end=$dt_start->clone();
   $dt_end->to_end_of_month();
   for($dt=$dt_start->clone();$dt<$dt_end;$dt+={day=>1}){
   ... ...
   }; 

This example how to explore every month of year

   $dt_start=EasyDateTime->new('2004-01-01 00:00:00');
   $dt_end=$dt_start->clone();
   $dt_end+={year=>1};
   for($dt=$dt_start->clone();$dt<$dt_end;$dt+={month=>1}){
   ... ...
   };

This example how to explore every last day of month of year

   $dt_start=EasyDateTime->new('2004-01-01 00:00:00');
   $dt_end=$dt_start->clone();
   $dt_start->to_end_of_month();
   $dt_end+={year=>1};
   for($dt=$dt_start->clone();$dt<$dt_end;$dt->to_end_of_month(1)){
   ... ...
   };

This example how to lock a EasyDateTime instance

   $dt->lock();
   $dt+={day=>1};#this will cause die for u cannot changed the value of a locked instance

Bad example

   for($dt=$dt_start;$dt>$dt_end;$dt+={day=>1}){
   ... ...
   }
   
   because $dt and $dt_start is reference the same instance,
   when u changed the value of $dt, $dt_start is also changed
   so u use assignment without clone is DANGEROUS!!!!
   and in case other won't change ur $dt, u can use lock() to set $dt read only 

CONSTRUCTOR ^

METHODS ^

PARAMETER ^

RH_DATETIME

        RH_DATETIME is a reference of hash like 
            {year=>2000,month=>1,day=>1,hour=>0,min=>0,sec=>0,timezone=>8}
        it represent a datetime ,if some item is not set, will use default value instead
                default value of year is 2000
                default value of month is 1
                default value of day is 1
                default value of hour is 0
                default value of min is 0
                default value of sec is 0
                default value of timezone is the localtimezone of machine

RH_TIMEINTERVAL

    RH_DATETIME is a reference of hash like 
            {year=>0,month=>0,day=>0,hour=>0,min=>0,sec=>0}
        it represent the interval between two datetime if some item is not set, will use default value instead
                default value of year is 0
                default value of month is 0
                default value of day is 0
                default value of hour is 0
                default value of min is 0
                default value of sec is 0

DATETIME_STR

FORMAT_STR

YEAR

MONTH

DAY

HOUR

MINUTE

SECOND

TIMEZONE

FULL DATETIME

OTHER

LIMITATION ^

the datetime is limited in the year 1900 to 2037

AUTHOR ^

Qian Yu <foolfish@cpan.org>

COPYRIGHT ^

Copyright (c) 2004-2004 Qian Yu. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the LICENSE file included with this module.