| 1 | package Time; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Time::HiRes (); | ||
| 5 | use POSIX (); | ||
| 6 | use base 'Object'; | ||
| 7 | |||
| 8 | sub new { | ||
| 9 | my $pkg = shift; | ||
| 10 | return bless {@_}, ref($pkg) || $pkg; | ||
| 11 | } | ||
| 12 | |||
| 13 | sub now { | ||
| 14 | my @t = localtime; | ||
| 15 | shift->new( | ||
| 16 | year => $t[5] + 1900, | ||
| 17 | month => $t[4] + 1, | ||
| 18 | day => $t[3], | ||
| 19 | hour => $t[2], | ||
| 20 | minute => $t[1], | ||
| 21 | second => $t[0], | ||
| 22 | ); | ||
| 23 | } | ||
| 24 | |||
| 25 | =head2 year | ||
| 26 | The C<year> accessor returns the 4-digit year for the date. | ||
| 27 | =cut | ||
| 28 | |||
| 29 | sub year { | ||
| 30 | defined $_[0]->{year} ? $_[0]->{year} : 1970; | ||
| 31 | } | ||
| 32 | |||
| 33 | =head2 month | ||
| 34 | The C<month> accessor returns the 1-12 month of the year for the date. | ||
| 35 | =cut | ||
| 36 | |||
| 37 | sub month { | ||
| 38 | $_[0]->{month} || 1; | ||
| 39 | } | ||
| 40 | |||
| 41 | =head2 day | ||
| 42 | The C<day> accessor returns the 1-31 day of the month for the date. | ||
| 43 | =cut | ||
| 44 | |||
| 45 | sub day { | ||
| 46 | $_[0]->{day} || 1; | ||
| 47 | } | ||
| 48 | |||
| 49 | =head2 hour | ||
| 50 | The C<hour> accessor returns the hour component of the time as | ||
| 51 | an integer from zero to twenty-three (0-23) in line with 24-hour | ||
| 52 | time. | ||
| 53 | =cut | ||
| 54 | |||
| 55 | sub hour { | ||
| 56 | $_[0]->{hour} || 0; | ||
| 57 | } | ||
| 58 | |||
| 59 | =head2 minute | ||
| 60 | The C<minute> accessor returns the minute component of the time | ||
| 61 | as an integer from zero to fifty-nine (0-59). | ||
| 62 | =cut | ||
| 63 | |||
| 64 | sub minute { | ||
| 65 | $_[0]->{minute} || 0; | ||
| 66 | } | ||
| 67 | |||
| 68 | =head2 second | ||
| 69 | The C<second> accessor returns the second component of the time | ||
| 70 | as an integer from zero to fifty-nine (0-59). | ||
| 71 | =cut | ||
| 72 | |||
| 73 | sub second { | ||
| 74 | $_[0]->{second} || 0; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub microsecond { | ||
| 78 | $_[0]->{second} || +(Time::HiRes::gettimeofday)[1]; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub strftime { | ||
| 82 | my $self = shift; | ||
| 83 | my $format = shift; | ||
| 84 | my @need_t; | ||
| 85 | if ( ref( $_[0] ) eq 'Time' ) { | ||
| 86 | @need_t = localtime( shift->to_epoch ); | ||
| 87 | } | ||
| 88 | else { | ||
| 89 | @need_t = @_; | ||
| 90 | } | ||
| 91 | POSIX::strftime( $format, @need_t ); | ||
| 92 | } | ||
| 93 | |||
| 94 | sub to_epoch { | ||
| 95 | my $self = shift; | ||
| 96 | POSIX::mktime( | ||
| 97 | $self->second, $self->minute, $self->hour, $self->day, | ||
| 98 | $self->month - 1, | ||
| 99 | $self->year - 1900 | ||
| 100 | ); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub from_epoch { | ||
| 104 | my $self = shift; | ||
| 105 | my @t = localtime(shift); | ||
| 106 | $self->new( | ||
| 107 | year => $t[5] + 1900, | ||
| 108 | month => $t[4] + 1, | ||
| 109 | day => $t[3], | ||
| 110 | hour => $t[2], | ||
| 111 | minute => $t[1], | ||
| 112 | second => $t[0], | ||
| 113 | ); | ||
| 114 | } | ||
| 115 | |||
| 116 | sub to_float { | ||
| 117 | return Time::HiRes::time; | ||
| 118 | } | ||
| 119 | 1; |