| 1 | 113 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 113 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package Jifty::DateTime; | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | Jifty::DateTime - a DateTime subclass that knows about Jifty users | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | use Jifty::DateTime; | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # Get the current date and time | ||||||||||||||||||||||||||||
| 15 | my $dt = Jifty::DateTime->now; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # Print out the pretty date (i.e., today, tomorrow, yesterday, or 2007-09-11) | ||||||||||||||||||||||||||||
| 18 | Jifty->web->out( $dt->friendly_date ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # Better date parsing | ||||||||||||||||||||||||||||
| 21 | my $dt_from_human = Jifty::DateTime->new_from_string("next Saturday"); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | Jifty natively stores timestamps in the database in GMT. Dates are | ||||||||||||||||||||||||||||
| 26 | stored without timezone. This class loads and parses dates and sets | ||||||||||||||||||||||||||||
| 27 | them into the proper timezone. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | To use this DateTime class to it's fullest ability, you'll need to add | ||||||||||||||||||||||||||||
| 30 | a C<time_zone> method to your application's user object class. This is | ||||||||||||||||||||||||||||
| 31 | the class returned by L<Jifty::CurrentUser/user_object>. It must | ||||||||||||||||||||||||||||
| 32 | return a value valid for using as an argument to L<DateTime>'s | ||||||||||||||||||||||||||||
| 33 | C<set_time_zone()> method. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =cut | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 113 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | # we spent about 30% of the time in validate during 'require | ||||||||||||||||||||||||||||
| 39 | # DateTime::Locale' which isn't necessary at all | ||||||||||||||||||||||||||||
| 40 | 113 | require Params::Validate; | |||||||||||||||||||||||||||
| 41 | 113 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | local *Params::Validate::validate = sub { pop @_, return @_ }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 43 | 113 | require DateTime::Locale; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 113 | ▶ | use base qw(Jifty::Object DateTime); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 113 | ▶ | use Jifty::DBI::Schema; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | Jifty::DBI::Schema->register_types( | ||||||||||||||||||||||||||||
| 50 | 0 | ▶ | timestamp => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 51 | 0 | encode_on_select is 1, | |||||||||||||||||||||||||||
| 52 | type is 'timestamp', | ||||||||||||||||||||||||||||
| 53 | filters are qw( Jifty::Filter::DateTime Jifty::DBI::Filter::DateTime ), | ||||||||||||||||||||||||||||
| 54 | }, | ||||||||||||||||||||||||||||
| 55 | 113 | ); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =head2 new ARGS | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | See L<DateTime/new>. If we get what appears to be a date, then we keep this in | ||||||||||||||||||||||||||||
| 60 | the floating datetime. Otherwise, set this object's timezone to the current | ||||||||||||||||||||||||||||
| 61 | user's time zone, if the current user's user object has a method called | ||||||||||||||||||||||||||||
| 62 | C<time_zone>. | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =cut | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 68 | 0 | my %args = ( | |||||||||||||||||||||||||||
| 69 | current_user => undef, | ||||||||||||||||||||||||||||
| 70 | time_zone => undef, | ||||||||||||||||||||||||||||
| 71 | input_time_zone => undef, | ||||||||||||||||||||||||||||
| 72 | output_time_zone => undef, | ||||||||||||||||||||||||||||
| 73 | @_, | ||||||||||||||||||||||||||||
| 74 | ); | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | my ($input_time_zone, $output_time_zone); | |||||||||||||||||||||||||||
| 77 | 0 | ▶ | $input_time_zone = delete($args{input_time_zone}) || $args{time_zone}; | ||||||||||||||||||||||||||
|
Condition0%0/3
delete $args{'input_time_zone'} || $args{'time_zone'}
MC/DC0%0/2
delete $args{'input_time_zone'} || $args{'time_zone'}
delete $args{'input_time_zone'}
$args{'time_zone'}
Truth table0%0/3
A || B
A= delete $args{'input_time_zone'}B= $args{'time_zone'}
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | $output_time_zone = delete($args{output_time_zone}) || $args{time_zone}; | ||||||||||||||||||||||||||
|
Condition0%0/3
delete $args{'output_time_zone'} || $args{'time_zone'}
MC/DC0%0/2
delete $args{'output_time_zone'} || $args{'time_zone'}
delete $args{'output_time_zone'}
$args{'time_zone'}
Truth table0%0/3
A || B
A= delete $args{'output_time_zone'}B= $args{'time_zone'}
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | my $current_user = delete $args{current_user}; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | my $self = $class->SUPER::new(%args, time_zone => $input_time_zone); | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | my $is_date = $self->hms eq '00:00:00' | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->hms eq '00:00:00' && $self->time_zone->name eq 'floating'
MC/DC0%0/2
$self->hms eq '00:00:00' && $self->time_zone->name eq 'floating'
$self->hms eq '00:00:00'
$self->time_zone->name eq 'floating'
Truth table0%0/3
A && B
A= $self->hms eq '00:00:00'B= $self->time_zone->name eq 'floating'
| |||||||||||||||||||||||||||||
| 85 | && $self->time_zone->name eq 'floating'; | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # The output time_zone is the *current user's* time zone. It's okay that | ||||||||||||||||||||||||||||
| 88 | # $current_user can be undef; we'll still find and set the right current | ||||||||||||||||||||||||||||
| 89 | # user then set the time zone. | ||||||||||||||||||||||||||||
| 90 | 0 | $self->current_user($current_user); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ($output_time_zone) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 93 | 0 | $self->set_time_zone($output_time_zone); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | # If we were given a date, then we need to make sure its output time zone | ||||||||||||||||||||||||||||
| 96 | # is Floating and it's set to 00:00:00. | ||||||||||||||||||||||||||||
| 97 | # This sucks when you want a timestamp (not just a datestamp) at midnight | ||||||||||||||||||||||||||||
| 98 | # in the floating time zone but we don't have any better way to make this | ||||||||||||||||||||||||||||
| 99 | # work. | ||||||||||||||||||||||||||||
| 100 | elsif ($is_date) { | ||||||||||||||||||||||||||||
| 101 | 0 | $self->set_time_zone('floating'); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # Without this check we loop infinitely, because set_hour constructs | ||||||||||||||||||||||||||||
| 104 | # a new Jifty::DateTime object. | ||||||||||||||||||||||||||||
| 105 | 0 | ▶ | if ($self->hms ne '00:00:00') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | $self->set_hour(0); | |||||||||||||||||||||||||||
| 107 | 0 | $self->set_minute(0); | |||||||||||||||||||||||||||
| 108 | 0 | $self->set_second(0); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | return $self; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =head2 now ARGS | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | See L<DateTime/now>. If a time_zone argument is passed in, then this wrapper | ||||||||||||||||||||||||||||
| 118 | is effectively a no-op. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | OTHERWISE this will always set this object's timezone to the current user's | ||||||||||||||||||||||||||||
| 121 | timezone. Without this, DateTime's C<now> will set the timezone to UTC always | ||||||||||||||||||||||||||||
| 122 | (by passing C<< time_zone => 'UTC' >> to C<Jifty::DateTime::new>. We want | ||||||||||||||||||||||||||||
| 123 | Jifty::DateTime to always reflect the current | ||||||||||||||||||||||||||||
| 124 | user's timezone (unless otherwise requested, of course). | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =cut | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub now { | ||||||||||||||||||||||||||
|
Subroutine
now: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 130 | 0 | my %args = ( | |||||||||||||||||||||||||||
| 131 | current_user => undef, | ||||||||||||||||||||||||||||
| 132 | #time_zone => undef, # DateTime doesn't like undef time_zone | ||||||||||||||||||||||||||||
| 133 | @_, | ||||||||||||||||||||||||||||
| 134 | ); | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | my $current_user = delete $args{current_user}; | |||||||||||||||||||||||||||
| 137 | 0 | my $self = $class->SUPER::now(%args); | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | $self->current_user($current_user); | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # We set time_zone here since saying | ||||||||||||||||||||||||||||
| 142 | # "Jifty::DateTime->now(time_zone => 'UTC')" is obviously referring the | ||||||||||||||||||||||||||||
| 143 | # output time zone; the input time zone doesn't matter at all. | ||||||||||||||||||||||||||||
| 144 | 0 | ▶ | $self->set_time_zone($args{time_zone}) if $args{time_zone}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | return $self; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head2 from_epoch ARGS | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | See L<DateTime/from_epoch> and L<Jifty::DateTime/now>. This handles the common | ||||||||||||||||||||||||||||
| 152 | mistake of C<from_epoch($epoch)> as well. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | ▶ | sub from_epoch { | ||||||||||||||||||||||||||
|
Subroutine
from_epoch: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 157 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | # from_epoch(100) should dwim | ||||||||||||||||||||||||||||
| 160 | 0 | ▶ | unshift @_, 'epoch' if @_ == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | my %args = ( | |||||||||||||||||||||||||||
| 163 | current_user => undef, | ||||||||||||||||||||||||||||
| 164 | #time_zone => undef, # DateTime doesn't like undef time_zone | ||||||||||||||||||||||||||||
| 165 | @_, | ||||||||||||||||||||||||||||
| 166 | ); | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | my $current_user = delete $args{current_user}; | |||||||||||||||||||||||||||
| 169 | 0 | my $self = $class->SUPER::from_epoch(%args); | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | $self->current_user($current_user); | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | # We set time_zone here since saying | ||||||||||||||||||||||||||||
| 174 | # "Jifty::DateTime->now(time_zone => 'UTC')" is obviously referring the | ||||||||||||||||||||||||||||
| 175 | # output time zone; the input time zone doesn't matter at all. | ||||||||||||||||||||||||||||
| 176 | 0 | ▶ | $self->set_time_zone($args{time_zone}) if $args{time_zone}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | return $self; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =head2 current_user [CURRENTUSER] | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | When setting the current user, update the timezone appropriately. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | If an C<undef> current user is passed, this method will find the correct | ||||||||||||||||||||||||||||
| 186 | current user and set the time zone. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =cut | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | ▶ | sub current_user { | ||||||||||||||||||||||||||
|
Subroutine
current_user: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 191 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 192 | 0 | ▶ | return $self->SUPER::current_user unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | # $date->current_user(undef) will not remove the current user, but it will | ||||||||||||||||||||||||||||
| 195 | # calculate who the current user is for setting the time zone | ||||||||||||||||||||||||||||
| 196 | 0 | ▶ | if (@_ == 1 && !defined($_[0])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ == 1 and not defined $_[0]
MC/DC0%0/2
@_ == 1 and not defined $_[0]
@_ == 1
not defined $_[0]
Truth table0%0/3
A and not(B)
A= @_ == 1B= not defined $_[0]
| |||||||||||||||||||||||||||||
| 197 | 0 | shift; | |||||||||||||||||||||||||||
| 198 | 0 | $self->_get_current_user; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 0 | my $ret = $self->SUPER::current_user(@_); | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 0 | $self->set_current_user_timezone(); | |||||||||||||||||||||||||||
| 204 | 0 | return $ret; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =head2 current_user_has_timezone | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | Return timezone if the current user has one. This is determined by | ||||||||||||||||||||||||||||
| 210 | checking to see if the current user has a user object. If it has a | ||||||||||||||||||||||||||||
| 211 | user object, then it checks to see if that user object has a | ||||||||||||||||||||||||||||
| 212 | C<time_zone> method and uses that to determine the value. | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =cut | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 0 | ▶ | sub current_user_has_timezone { | ||||||||||||||||||||||||||
|
Subroutine
current_user_has_timezone: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 217 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | # make this work as Jifty::DateTime->current_user_has_timezone | ||||||||||||||||||||||||||||
| 220 | 0 | ▶ | my $dt = ref($self) ? $self : $self->now; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 0 | $dt->_get_current_user(); | |||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | # Can't continue if we have no notion of a user_object | ||||||||||||||||||||||||||||
| 225 | 0 | ▶ | $dt->current_user->can('user_object') or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | # Can't continue unless the user object is defined | ||||||||||||||||||||||||||||
| 228 | 0 | ▶ | my $user_obj = $dt->current_user->user_object or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | # Check for a time_zone method and then use it if it exists | ||||||||||||||||||||||||||||
| 231 | 0 | ▶ | my $f = $user_obj->can('time_zone') || $user_obj->can('timezone') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$user_obj->can('time_zone') || $user_obj->can('timezone')
MC/DC0%0/2
$user_obj->can('time_zone') || $user_obj->can('timezone')
$user_obj->can('time_zone')
$user_obj->can('timezone')
Truth table0%0/3
A || B
A= $user_obj->can('time_zone')B= $user_obj->can('timezone')
| |||||||||||||||||||||||||||||
| 232 | or return; | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 0 | return $f->($user_obj); | |||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | =head2 set_current_user_timezone [DEFAULT_TZ] | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =head2 set_current_user_time_zone [DEFAULT_TZ] | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | Set this Jifty::DateTime's timezone to the current user's timezone. If that's | ||||||||||||||||||||||||||||
| 242 | not available, then use the passed in DEFAULT_TZ (or GMT if not passed in). | ||||||||||||||||||||||||||||
| 243 | Returns the Jifty::DateTime object itself. | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | If your subclass changes this method, please override | ||||||||||||||||||||||||||||
| 246 | C<set_current_user_timezone> not C<set_current_user_time_zone>, since the | ||||||||||||||||||||||||||||
| 247 | latter is merely an alias for the former. | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =cut | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 0 | ▶ | sub set_current_user_timezone { | ||||||||||||||||||||||||||
|
Subroutine
set_current_user_timezone: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 252 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 253 | 0 | ▶ | my $default = shift || Jifty->config->framework('Timezone') || 'UTC'; | ||||||||||||||||||||||||||
|
Condition0%0/5
shift() || 'Jifty'->config->framework('Timezone') || 'UTC'
shift() or "Jifty"->config->framework("Timezone")
MC/DC0%0/2
shift() || 'Jifty'->config->framework('Timezone') || 'UTC'
shift()
"Jifty"->config->framework("Timezone")
Truth table0%0/3
A or B
A= shift()B= "Jifty"->config->framework("Timezone")
| |||||||||||||||||||||||||||||
| 254 | 0 | ▶ | my $tz = $self->current_user_has_timezone || $default; | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->current_user_has_timezone || $default
MC/DC0%0/2
$self->current_user_has_timezone || $default
$self->current_user_has_timezone
$default
Truth table0%0/3
A || B
A= $self->current_user_has_timezoneB= $default
| |||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 0 | $self->set_time_zone($tz); | |||||||||||||||||||||||||||
| 257 | 0 | return $self; | |||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 0 | ▶ | sub set_current_user_time_zone { shift->set_current_user_timezone(@_) } | ||||||||||||||||||||||||||
|
Subroutine
set_current_user_time_zone: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | =head2 new_from_string STRING[, ARGS] | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | Take some user defined string like "tomorrow" and turn it into a | ||||||||||||||||||||||||||||
| 265 | C<Jifty::Datetime> object. If a C<time_zone> argument is passed in, that is | ||||||||||||||||||||||||||||
| 266 | used for the B<input> time zone. | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | If the string appears to be a _date_, the B<output> time zone will be floating. | ||||||||||||||||||||||||||||
| 269 | Otherwise, the B<output> time zone will be the current user's time zone. | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | As of this writing, this uses L<Date::Manip> along with some internal | ||||||||||||||||||||||||||||
| 272 | hacks to alter the way L<Date::Manip> normally interprets week day | ||||||||||||||||||||||||||||
| 273 | names. This may change in the future. | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | =cut | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 0 | ▶ | sub new_from_string { | ||||||||||||||||||||||||||
|
Subroutine
new_from_string: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 278 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 279 | 0 | my $string = shift; | |||||||||||||||||||||||||||
| 280 | 0 | ▶ | return unless $string; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 281 | 0 | my %args = ( | |||||||||||||||||||||||||||
| 282 | time_zone => undef, | ||||||||||||||||||||||||||||
| 283 | @_, | ||||||||||||||||||||||||||||
| 284 | ); | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 0 | my $epoch; | |||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | # Hack to use Date::Manip to flexibly scan dates from strings | ||||||||||||||||||||||||||||
| 289 | { | ||||||||||||||||||||||||||||
| 290 | # Date::Manip interprets days of the week (eg, ''Monday'') as | ||||||||||||||||||||||||||||
| 291 | # days within the *current* week. Detect these and prepend | ||||||||||||||||||||||||||||
| 292 | # ``next'' | ||||||||||||||||||||||||||||
| 293 | # XXX TODO: Find a real solution (better date-parsing library?) | ||||||||||||||||||||||||||||
| 294 | 0 | ▶ | if($string =~ /^\s* (?:monday|tuesday|wednesday|thursday|friday|saturday|sunday)$/xi) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 295 | 0 | $string = "next $string"; | |||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | my $offset = $class->get_tz_offset( | ||||||||||||||||||||||||||||
| 299 | 0 | ▶ | $args{time_zone} ? (time_zone => $args{time_zone}) : (), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | ); | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | 0 | require Date::Manip; | |||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | # ForceDate forces the current date to be now in the user's timezone, | ||||||||||||||||||||||||||||
| 305 | # if we don't set it then DM uses the machine's timezone | ||||||||||||||||||||||||||||
| 306 | 0 | Date::Manip::Date_Init("ForceDate=now,$offset"); | |||||||||||||||||||||||||||
| 307 | 0 | $epoch = Date::Manip::UnixDate( $string, "%o" ); | |||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | # Stop here if Date::Manip couldn't figure it out | ||||||||||||||||||||||||||||
| 311 | 0 | ▶ | return undef unless $epoch; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | # Build a DateTime object from the Date::Manip value and setup the TZ | ||||||||||||||||||||||||||||
| 314 | 0 | my $self = $class->from_epoch( epoch => $epoch, time_zone => 'UTC' ); | |||||||||||||||||||||||||||
| 315 | 0 | ▶ | if (my $tz = $self->current_user_has_timezone) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | 0 | ▶ | if ($self->hms(':') ne '00:00:00') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | 0 | $self->set_time_zone($tz); | |||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | else { | ||||||||||||||||||||||||||||
| 320 | 0 | $self->set_time_zone("floating"); | |||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | 0 | return $self; | |||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | =head2 friendly_date | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | Returns the date given by this C<Jifty::DateTime> object. It will display "today" | ||||||||||||||||||||||||||||
| 330 | for today, "tomorrow" for tomorrow, or "yesterday" for yesterday. Any other date | ||||||||||||||||||||||||||||
| 331 | will be displayed in C<ymd> format. | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | We currently shift by "24 hours" to detect yesterday and tomorrow, rather than | ||||||||||||||||||||||||||||
| 334 | "1 day" because of daylight saving issues. "1 day" can result in invalid local | ||||||||||||||||||||||||||||
| 335 | time errors. | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | =cut | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 0 | ▶ | sub friendly_date { | ||||||||||||||||||||||||||
|
Subroutine
friendly_date: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 340 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 341 | 0 | my $ymd = $self->ymd; | |||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | # Use the current user's time zone on the date | ||||||||||||||||||||||||||||
| 344 | 0 | ▶ | my $tz = $self->current_user_has_timezone || $self->time_zone; | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->current_user_has_timezone || $self->time_zone
MC/DC0%0/2
$self->current_user_has_timezone || $self->time_zone
$self->current_user_has_timezone
$self->time_zone
Truth table0%0/3
A || B
A= $self->current_user_has_timezoneB= $self->time_zone
| |||||||||||||||||||||||||||||
| 345 | 0 | my $rel = DateTime->now( time_zone => $tz ); | |||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | # Is it today? | ||||||||||||||||||||||||||||
| 348 | 0 | ▶ | if ($ymd eq $rel->ymd) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 349 | 0 | return "today"; | |||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | # Is it yesterday? | ||||||||||||||||||||||||||||
| 353 | 0 | my $yesterday = $rel->clone->subtract(hours => 24); | |||||||||||||||||||||||||||
| 354 | 0 | ▶ | if ($ymd eq $yesterday->ymd) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 355 | 0 | return "yesterday"; | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | # Is it tomorrow? | ||||||||||||||||||||||||||||
| 359 | 0 | my $tomorrow = $rel->clone->add(hours => 24); | |||||||||||||||||||||||||||
| 360 | 0 | ▶ | if ($ymd eq $tomorrow->ymd) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 361 | 0 | return "tomorrow"; | |||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | # None of the above, just spit out the date | ||||||||||||||||||||||||||||
| 365 | 0 | return $ymd; | |||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | =head2 is_date | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | Returns whether or not this C<Jifty::DateTime> object represents a date | ||||||||||||||||||||||||||||
| 371 | (without a specific time). Dates in Jifty are in the floating time zone and | ||||||||||||||||||||||||||||
| 372 | are set to midnight. | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | =cut | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | 0 | ▶ | sub is_date { | ||||||||||||||||||||||||||
|
Subroutine
is_date: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 377 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | # all dates are in the floating time zone | ||||||||||||||||||||||||||||
| 380 | 0 | ▶ | return 0 unless $self->time_zone->name eq 'floating'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | # all dates are set to midnight | ||||||||||||||||||||||||||||
| 383 | 0 | ▶ | return 0 unless $self->hms eq '00:00:00'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | 0 | return 1; | |||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | =head2 get_tz_offset | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | Returns the offset for a time zone. If there is no current | ||||||||||||||||||||||||||||
| 391 | user, or the current user's time zone is unset, then UTC will be used. | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | The optional datetime argument lets you calculate an offset for some time other | ||||||||||||||||||||||||||||
| 394 | than "right now". | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | =cut | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | 0 | ▶ | sub get_tz_offset { | ||||||||||||||||||||||||||
|
Subroutine
get_tz_offset: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 399 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 400 | 0 | ▶ | my %args = ( | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->current_user_has_timezone || 'UTC'
MC/DC0%0/1
$self->current_user_has_timezone || 'UTC'
$self->current_user_has_timezone
Truth table0%0/2
A
A= $self->current_user_has_timezone
| |||||||||||||||||||||||||||||
| 401 | datetime => DateTime->now, | ||||||||||||||||||||||||||||
| 402 | time_zone => $self->current_user_has_timezone || 'UTC', | ||||||||||||||||||||||||||||
| 403 | @_, | ||||||||||||||||||||||||||||
| 404 | ); | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | 0 | my $dt = $args{datetime}->clone; | |||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | 0 | $dt->set_time_zone($args{time_zone}); | |||||||||||||||||||||||||||
| 409 | 0 | return $dt->strftime("%z"); | |||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | =head2 jifty_serialize_format | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | This returns a DateTime (or string) consistent with Jifty's date format. | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | =cut | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | 0 | ▶ | sub jifty_serialize_format { | ||||||||||||||||||||||||||
|
Subroutine
jifty_serialize_format: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 419 | 0 | my $dt = shift; | |||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | # if it looks like just a date, then return just the date portion | ||||||||||||||||||||||||||||
| 422 | 0 | ▶ | return $dt->ymd if $dt->is_date; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | # otherwise let stringification take care of it | ||||||||||||||||||||||||||||
| 425 | 0 | return $dt; | |||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | =head1 WHY? | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | There are other ways to do some of these things and some of the | ||||||||||||||||||||||||||||
| 431 | decisions here may seem arbitrary, particularly if you read the | ||||||||||||||||||||||||||||
| 432 | code. They are. | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | These things are valuable to applications built by Best Practical | ||||||||||||||||||||||||||||
| 435 | Solutions, so it's here. If you disagree with the policy or need to do | ||||||||||||||||||||||||||||
| 436 | it differently, then you probably need to implement something yourself | ||||||||||||||||||||||||||||
| 437 | using a DateTime::Format::* class or your own code. | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | Parts may be cleaned up and the API cleared up a bit more in the future. | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | L<DateTime>, L<DateTime::TimeZone>, L<Jifty::CurrentUser> | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | Jifty is Copyright 2005-2010 Best Practical Solutions, LLC. | ||||||||||||||||||||||||||||
| 448 | Jifty is distributed under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | =cut | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | 113 | 1; | |||||||||||||||||||||||||||