| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 1 |  |  | 1 |  | 13698 | use strict; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 22 |  | 
| 2 | 1 |  |  | 1 |  | 3 | use warnings; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 58 |  | 
| 3 |  |  |  |  |  |  | package Time::Tiny; | 
| 4 |  |  |  |  |  |  | # ABSTRACT: A time object, with as little code as possible | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | our $VERSION = '1.07'; | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 1 |  |  | 1 |  | 868 | use overload 'bool' => sub () { 1 }; | 
|  | 1 |  |  |  |  | 864 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 9 | 1 |  |  | 1 |  | 46 | use overload '""'   => 'as_string'; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 2 |  | 
| 10 | 1 |  |  | 1 |  | 60 | use overload 'eq'   => sub { "$_[0]" eq "$_[1]" }; | 
|  | 1 |  |  | 0 |  | 1 |  | 
|  | 1 |  |  |  |  | 4 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 11 | 1 |  |  | 1 |  | 47 | use overload 'ne'   => sub { "$_[0]" ne "$_[1]" }; | 
|  | 1 |  |  | 0 |  | 1 |  | 
|  | 1 |  |  |  |  | 4 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | ##################################################################### | 
| 18 |  |  |  |  |  |  | # Constructor and Accessors | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | #pod =pod | 
| 21 |  |  |  |  |  |  | #pod | 
| 22 |  |  |  |  |  |  | #pod =method new | 
| 23 |  |  |  |  |  |  | #pod | 
| 24 |  |  |  |  |  |  | #pod   # Create a Time::Tiny object for midnight | 
| 25 |  |  |  |  |  |  | #pod   my $midnight = Time::Tiny->new( | 
| 26 |  |  |  |  |  |  | #pod       hour   => 0, | 
| 27 |  |  |  |  |  |  | #pod       minute => 0, | 
| 28 |  |  |  |  |  |  | #pod       second => 0, | 
| 29 |  |  |  |  |  |  | #pod   ); | 
| 30 |  |  |  |  |  |  | #pod | 
| 31 |  |  |  |  |  |  | #pod The C constructor creates a new B object. | 
| 32 |  |  |  |  |  |  | #pod | 
| 33 |  |  |  |  |  |  | #pod It takes three named parameters. C should be the hour of the day (0-23), | 
| 34 |  |  |  |  |  |  | #pod C should be the minute of the hour (0-59), and C should be | 
| 35 |  |  |  |  |  |  | #pod the second of the minute (0-59). | 
| 36 |  |  |  |  |  |  | #pod | 
| 37 |  |  |  |  |  |  | #pod These are the only parameters accepted. | 
| 38 |  |  |  |  |  |  | #pod | 
| 39 |  |  |  |  |  |  | #pod Returns a new B object. | 
| 40 |  |  |  |  |  |  | #pod | 
| 41 |  |  |  |  |  |  | #pod =cut | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | sub new { | 
| 44 | 3 |  |  | 3 | 1 | 12 | my $class = shift; | 
| 45 | 3 |  |  |  |  | 18 | bless { @_ }, $class; | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | #pod =pod | 
| 49 |  |  |  |  |  |  | #pod | 
| 50 |  |  |  |  |  |  | #pod =method now | 
| 51 |  |  |  |  |  |  | #pod | 
| 52 |  |  |  |  |  |  | #pod   my $current_time = Time::Tiny->now; | 
| 53 |  |  |  |  |  |  | #pod | 
| 54 |  |  |  |  |  |  | #pod The C method creates a new date object for the current time. | 
| 55 |  |  |  |  |  |  | #pod | 
| 56 |  |  |  |  |  |  | #pod The time created will be based on localtime, despite the fact that | 
| 57 |  |  |  |  |  |  | #pod the time is created in the floating time zone. | 
| 58 |  |  |  |  |  |  | #pod | 
| 59 |  |  |  |  |  |  | #pod This means that the time created by C is somewhat lossy, but | 
| 60 |  |  |  |  |  |  | #pod since the primary purpose of B is for small transient | 
| 61 |  |  |  |  |  |  | #pod time objects, and B for use in calculations and comparisons, | 
| 62 |  |  |  |  |  |  | #pod this is considered acceptable for now. | 
| 63 |  |  |  |  |  |  | #pod | 
| 64 |  |  |  |  |  |  | #pod Returns a new B object. | 
| 65 |  |  |  |  |  |  | #pod | 
| 66 |  |  |  |  |  |  | #pod =cut | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | sub now { | 
| 69 | 1 |  |  | 1 | 1 | 79 | my @t = localtime time; | 
| 70 | 1 |  |  |  |  | 5 | $_[0]->new( | 
| 71 |  |  |  |  |  |  | hour   => $t[2], | 
| 72 |  |  |  |  |  |  | minute => $t[1], | 
| 73 |  |  |  |  |  |  | second => $t[0], | 
| 74 |  |  |  |  |  |  | ); | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | #pod =pod | 
| 78 |  |  |  |  |  |  | #pod | 
| 79 |  |  |  |  |  |  | #pod =method hour | 
| 80 |  |  |  |  |  |  | #pod | 
| 81 |  |  |  |  |  |  | #pod The C accessor returns the hour component of the time as | 
| 82 |  |  |  |  |  |  | #pod an integer from zero to twenty-three (0-23) in line with 24-hour | 
| 83 |  |  |  |  |  |  | #pod time. | 
| 84 |  |  |  |  |  |  | #pod | 
| 85 |  |  |  |  |  |  | #pod =cut | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | sub hour { | 
| 88 | 6 | 50 |  | 6 | 1 | 647 | $_[0]->{hour} || 0; | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | #pod =pod | 
| 92 |  |  |  |  |  |  | #pod | 
| 93 |  |  |  |  |  |  | #pod =method minute | 
| 94 |  |  |  |  |  |  | #pod | 
| 95 |  |  |  |  |  |  | #pod The C accessor returns the minute component of the time | 
| 96 |  |  |  |  |  |  | #pod as an integer from zero to fifty-nine (0-59). | 
| 97 |  |  |  |  |  |  | #pod | 
| 98 |  |  |  |  |  |  | #pod =cut | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | sub minute { | 
| 101 | 6 | 50 |  | 6 | 1 | 17 | $_[0]->{minute} || 0; | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | #pod =pod | 
| 105 |  |  |  |  |  |  | #pod | 
| 106 |  |  |  |  |  |  | #pod =method second | 
| 107 |  |  |  |  |  |  | #pod | 
| 108 |  |  |  |  |  |  | #pod The C accessor returns the second component of the time | 
| 109 |  |  |  |  |  |  | #pod as an integer from zero to fifty-nine (0-59). | 
| 110 |  |  |  |  |  |  | #pod | 
| 111 |  |  |  |  |  |  | #pod =cut | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | sub second { | 
| 114 | 6 | 50 |  | 6 | 1 | 39 | $_[0]->{second} || 0; | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | ##################################################################### | 
| 122 |  |  |  |  |  |  | # Type Conversion | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | #pod =pod | 
| 125 |  |  |  |  |  |  | #pod | 
| 126 |  |  |  |  |  |  | #pod =method from_string | 
| 127 |  |  |  |  |  |  | #pod | 
| 128 |  |  |  |  |  |  | #pod The C method creates a new B object from a string. | 
| 129 |  |  |  |  |  |  | #pod | 
| 130 |  |  |  |  |  |  | #pod The string is expected to be an "hh:mm:ss" type ISO 8601 time string. | 
| 131 |  |  |  |  |  |  | #pod | 
| 132 |  |  |  |  |  |  | #pod   my $almost_midnight = Time::Tiny->from_string( '23:59:59' ); | 
| 133 |  |  |  |  |  |  | #pod | 
| 134 |  |  |  |  |  |  | #pod Returns a new B object, or throws an exception on error. | 
| 135 |  |  |  |  |  |  | #pod | 
| 136 |  |  |  |  |  |  | #pod =cut | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | sub from_string { | 
| 139 | 1 |  |  | 1 | 1 | 2 | my $string = $_[1]; | 
| 140 | 1 | 50 | 33 |  |  | 7 | unless ( defined $string and ! ref $string ) { | 
| 141 | 0 |  |  |  |  | 0 | require Carp; | 
| 142 | 0 |  |  |  |  | 0 | Carp::croak("Did not provide a string to from_string"); | 
| 143 |  |  |  |  |  |  | } | 
| 144 | 1 | 50 |  |  |  | 5 | unless ( $string =~ /^(\d\d):(\d\d):(\d\d)$/ ) { | 
| 145 | 0 |  |  |  |  | 0 | require Carp; | 
| 146 | 0 |  |  |  |  | 0 | Carp::croak("Invalid time format (does not match ISO 8601 hh:mm:ss)"); | 
| 147 |  |  |  |  |  |  | } | 
| 148 | 1 |  |  |  |  | 5 | $_[0]->new( | 
| 149 |  |  |  |  |  |  | hour   => $1 + 0, | 
| 150 |  |  |  |  |  |  | minute => $2 + 0, | 
| 151 |  |  |  |  |  |  | second => $3 + 0, | 
| 152 |  |  |  |  |  |  | ); | 
| 153 |  |  |  |  |  |  | } | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | #pod =pod | 
| 156 |  |  |  |  |  |  | #pod | 
| 157 |  |  |  |  |  |  | #pod =method as_string | 
| 158 |  |  |  |  |  |  | #pod | 
| 159 |  |  |  |  |  |  | #pod The C method converts the time object to an ISO 8601 | 
| 160 |  |  |  |  |  |  | #pod time string, with separators (see example in C). | 
| 161 |  |  |  |  |  |  | #pod | 
| 162 |  |  |  |  |  |  | #pod Returns a string. | 
| 163 |  |  |  |  |  |  | #pod | 
| 164 |  |  |  |  |  |  | #pod =cut | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | sub as_string { | 
| 167 | 5 |  |  | 5 | 1 | 1193 | sprintf( "%02u:%02u:%02u", | 
| 168 |  |  |  |  |  |  | $_[0]->hour, | 
| 169 |  |  |  |  |  |  | $_[0]->minute, | 
| 170 |  |  |  |  |  |  | $_[0]->second, | 
| 171 |  |  |  |  |  |  | ); | 
| 172 |  |  |  |  |  |  | } | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | #pod =pod | 
| 175 |  |  |  |  |  |  | #pod | 
| 176 |  |  |  |  |  |  | #pod =method DateTime | 
| 177 |  |  |  |  |  |  | #pod | 
| 178 |  |  |  |  |  |  | #pod The C method is used to create a L object | 
| 179 |  |  |  |  |  |  | #pod that is equivalent to the B object, for use in | 
| 180 |  |  |  |  |  |  | #pod conversions and calculations. | 
| 181 |  |  |  |  |  |  | #pod | 
| 182 |  |  |  |  |  |  | #pod As mentioned earlier, the object will be set to the 'C' locate, | 
| 183 |  |  |  |  |  |  | #pod and the 'floating' time zone. | 
| 184 |  |  |  |  |  |  | #pod | 
| 185 |  |  |  |  |  |  | #pod If installed, the L module will be loaded automatically. | 
| 186 |  |  |  |  |  |  | #pod | 
| 187 |  |  |  |  |  |  | #pod Returns a L object, or throws an exception if L | 
| 188 |  |  |  |  |  |  | #pod is not installed on the current host. | 
| 189 |  |  |  |  |  |  | #pod | 
| 190 |  |  |  |  |  |  | #pod =cut | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  | sub DateTime { | 
| 193 | 0 |  |  | 0 | 1 |  | require DateTime; | 
| 194 | 0 |  |  |  |  |  | my $self = shift; | 
| 195 | 0 |  |  |  |  |  | DateTime->new( | 
| 196 |  |  |  |  |  |  | year      => 1970, | 
| 197 |  |  |  |  |  |  | month     => 1, | 
| 198 |  |  |  |  |  |  | day       => 1, | 
| 199 |  |  |  |  |  |  | hour      => $self->hour, | 
| 200 |  |  |  |  |  |  | minute    => $self->minute, | 
| 201 |  |  |  |  |  |  | second    => $self->second, | 
| 202 |  |  |  |  |  |  | locale    => 'C', | 
| 203 |  |  |  |  |  |  | time_zone => 'floating', | 
| 204 |  |  |  |  |  |  | @_, | 
| 205 |  |  |  |  |  |  | ); | 
| 206 |  |  |  |  |  |  | } | 
| 207 |  |  |  |  |  |  |  | 
| 208 |  |  |  |  |  |  | 1; | 
| 209 |  |  |  |  |  |  |  | 
| 210 |  |  |  |  |  |  | __END__ |