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