| Jifty documentation | Contained in the Jifty distribution. |
Jifty::DateTime - a DateTime subclass that knows about Jifty users
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");
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 time_zone method to your application's user object class. This is
the class returned by user_object in Jifty::CurrentUser. It must
return a value valid for using as an argument to DateTime's
set_time_zone() method.
See new in DateTime. 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
time_zone.
See now in DateTime. 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 now will set the timezone to UTC always
(by passing time_zone => 'UTC' to Jifty::DateTime::new. We want
Jifty::DateTime to always reflect the current
user's timezone (unless otherwise requested, of course).
See from_epoch in DateTime and now in Jifty::DateTime. This handles the common
mistake of from_epoch($epoch) as well.
When setting the current user, update the timezone appropriately.
If an undef current user is passed, this method will find the correct
current user and set the time zone.
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
time_zone method and uses that to determine the value.
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
set_current_user_timezone not set_current_user_time_zone, since the
latter is merely an alias for the former.
Take some user defined string like "tomorrow" and turn it into a
Jifty::Datetime object. If a time_zone argument is passed in, that is
used for the input time zone.
If the string appears to be a _date_, the output time zone will be floating. Otherwise, the output time zone will be the current user's time zone.
As of this writing, this uses Date::Manip along with some internal hacks to alter the way Date::Manip normally interprets week day names. This may change in the future.
Returns the date given by this Jifty::DateTime object. It will display "today"
for today, "tomorrow" for tomorrow, or "yesterday" for yesterday. Any other date
will be displayed in 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.
Returns whether or not this Jifty::DateTime object represents a date
(without a specific time). Dates in Jifty are in the floating time zone and
are set to midnight.
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".
This returns a DateTime (or string) consistent with Jifty's date format.
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.
Jifty is Copyright 2005-2010 Best Practical Solutions, LLC. Jifty is distributed under the same terms as Perl itself.
| Jifty documentation | Contained in the Jifty distribution. |
use warnings; use strict; package Jifty::DateTime;
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 ), }, );
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; }
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; }
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; }
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; }
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); }
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(@_) }
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; }
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; }
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; }
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"); }
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; }
1;