validating (and caching) timezone.

This commit is contained in:
Matthew Wilson 2006-05-19 02:07:52 +00:00
parent 1a6e27710b
commit 4078d4d417

View file

@ -105,7 +105,7 @@ sub addToDate {
my $years = shift || 0; my $years = shift || 0;
my $months = shift || 0; my $months = shift || 0;
my $days = shift || 0; my $days = shift || 0;
my $time_zone = $self->session->user->profileField('timeZone') || 'America/Chicago'; my $time_zone = $self->getTimeZone;
my $date = DateTime->from_epoch(epoch=>$epoch, time_zone=>$time_zone); my $date = DateTime->from_epoch(epoch=>$epoch, time_zone=>$time_zone);
$date->add(years=>$years, months=>$months, days=>$days); $date->add(years=>$years, months=>$months, days=>$days);
return $date->epoch; return $date->epoch;
@ -156,7 +156,7 @@ sub addToDateTime {
my $hours = shift || 0; my $hours = shift || 0;
my $mins = shift || 0; my $mins = shift || 0;
my $secs = shift || 0; my $secs = shift || 0;
my $time_zone = $self->session->user->profileField('timeZone') || 'America/Chicago'; my $time_zone = $self->getTimeZone;
my $date = DateTime->from_epoch(epoch=>$epoch, time_zone=>$time_zone); my $date = DateTime->from_epoch(epoch=>$epoch, time_zone=>$time_zone);
$date->add(years=>$years, months=>$months, days=>$days, hours=>$hours, minutes=>$mins, seconds=>$secs); $date->add(years=>$years, months=>$months, days=>$days, hours=>$hours, minutes=>$mins, seconds=>$secs);
return $date->epoch; return $date->epoch;
@ -193,7 +193,7 @@ sub addToTime {
my $hours = shift || 0; my $hours = shift || 0;
my $mins = shift || 0; my $mins = shift || 0;
my $secs = shift || 0; my $secs = shift || 0;
my $time_zone = $self->session->user->profileField('timeZone') || 'America/Chicago'; my $time_zone = $self->getTimeZone;
my $date = DateTime->from_epoch(epoch=>$epoch, time_zone=>$time_zone); my $date = DateTime->from_epoch(epoch=>$epoch, time_zone=>$time_zone);
$date->add(hours=>$hours, minutes=>$mins, seconds=>$secs); $date->add(hours=>$hours, minutes=>$mins, seconds=>$secs);
return $date->epoch; return $date->epoch;
@ -214,7 +214,7 @@ The number of seconds since January 1, 1970.
sub dayStartEnd { sub dayStartEnd {
my $self = shift; my $self = shift;
my $epoch = shift; my $epoch = shift;
my $time_zone = $self->session->user->profileField('timeZone') || 'America/Chicago'; my $time_zone = $self->getTimeZone;
my $dt = DateTime->from_epoch(epoch=>$epoch, time_zone=>$time_zone); my $dt = DateTime->from_epoch(epoch=>$epoch, time_zone=>$time_zone);
my $end = $dt->clone; my $end = $dt->clone;
$dt->set_hour(0); $dt->set_hour(0);
@ -416,7 +416,7 @@ sub epochToSet {
my $epoch = shift; my $epoch = shift;
my $withTime = shift; my $withTime = shift;
return undef unless $epoch; return undef unless $epoch;
my $time_zone = $self->session->user->profileField('timeZone') || 'America/Chicago'; my $time_zone = $self->getTimeZone;
my $dt = DateTime->from_epoch(epoch=>$epoch, time_zone=>$time_zone); my $dt = DateTime->from_epoch(epoch=>$epoch, time_zone=>$time_zone);
if ($withTime) { if ($withTime) {
return $dt->strftime('%Y-%m-%d %H:%M:%S'); return $dt->strftime('%Y-%m-%d %H:%M:%S');
@ -472,7 +472,7 @@ An epoch date.
sub getDayOfWeek { sub getDayOfWeek {
my $self = shift; my $self = shift;
my $epoch = shift; my $epoch = shift;
my $time_zone = $self->session->user->profileField('timeZone') || 'America/Chicago'; my $time_zone = $self->getTimeZone;
my $dt = DateTime->from_epoch(epoch=>$epoch, time_zone=>$time_zone); my $dt = DateTime->from_epoch(epoch=>$epoch, time_zone=>$time_zone);
return $dt->day_of_week; return $dt->day_of_week;
} }
@ -492,7 +492,7 @@ An epoch date.
sub getDaysInMonth { sub getDaysInMonth {
my $self = shift; my $self = shift;
my $epoch = shift; my $epoch = shift;
my $time_zone = $self->session->user->profileField('timeZone') || 'America/Chicago'; my $time_zone = $self->getTimeZone;
my $dt = DateTime->from_epoch(epoch=>$epoch, time_zone=>$time_zone); my $dt = DateTime->from_epoch(epoch=>$epoch, time_zone=>$time_zone);
my $last = DateTime->last_day_of_month(year=>$dt->year, month=>$dt->month); my $last = DateTime->last_day_of_month(year=>$dt->year, month=>$dt->month);
return $last->day; return $last->day;
@ -519,7 +519,7 @@ sub getDaysInInterval {
my $self = shift; my $self = shift;
my $start = shift; my $start = shift;
my $end = shift; my $end = shift;
my $time_zone = $self->session->user->profileField('timeZone') || 'America/Chicago'; my $time_zone = $self->getTimeZone;
$start = DateTime->from_epoch(epoch=>$start, time_zone=>$time_zone); $start = DateTime->from_epoch(epoch=>$start, time_zone=>$time_zone);
$end = DateTime->from_epoch(epoch=>$end, time_zone=>$time_zone); $end = DateTime->from_epoch(epoch=>$end, time_zone=>$time_zone);
return $end->delta_days($start)->delta_days; return $end->delta_days($start)->delta_days;
@ -541,7 +541,7 @@ An epoch date.
sub getFirstDayInMonthPosition { sub getFirstDayInMonthPosition {
my $self = shift; my $self = shift;
my $epoch = shift; my $epoch = shift;
my $time_zone = $self->session->user->profileField('timeZone') || 'America/Chicago'; my $time_zone = $self->getTimeZone;
my $dt = DateTime->from_epoch(epoch=>$epoch, time_zone=>$time_zone); my $dt = DateTime->from_epoch(epoch=>$epoch, time_zone=>$time_zone);
$dt->set_day(1); $dt->set_day(1);
return $dt->day_of_week; return $dt->day_of_week;
@ -606,7 +606,7 @@ The number of seconds since January 1, 1970 00:00:00.
sub getSecondsFromEpoch { sub getSecondsFromEpoch {
my $self = shift; my $self = shift;
my $epoch = shift; my $epoch = shift;
my $time_zone = $self->session->user->profileField('timeZone') || 'America/Chicago'; my $time_zone = $self->getTimeZone;
my $dt = DateTime->from_epoch(epoch=>$epoch, time_zone=>$time_zone); my $dt = DateTime->from_epoch(epoch=>$epoch, time_zone=>$time_zone);
my $start = $dt->clone; my $start = $dt->clone;
$start->set_hour(0); $start->set_hour(0);
@ -638,6 +638,33 @@ sub getTimeZones {
} }
#-------------------------------------------------------------------
=head2 getTimeZone ( )
Returns the timezone for this user, in DateTime::TimeZone format. Checks to make sure we are sending DateTime::TimeZone a valid one!
=cut
sub getTimeZone {
my $self = shift;
return $self->session->user->{_timeZone} if $self->session->user->{_timeZone};
my @zones = @{DateTime::TimeZone::all_names()};
my $zone = $self->session->user->profileField('timeZone');
$zone =~ s/ /\_/g;
if ($zone) {
foreach (@zones) {
if ($_ eq $zone) {
$self->session->user->{_timeZone} = $zone;
return $zone;
}
}
}
$self->session->user->{_timeZone} = 'America/Chicago';
return $self->session->user->{_timeZone};
}
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 humanToEpoch ( date ) =head2 humanToEpoch ( date )
@ -652,7 +679,7 @@ The human date string. YYYY-MM-DD HH:MM:SS
sub humanToEpoch { sub humanToEpoch {
my $self = shift; my $self = shift;
my $time_zone = $self->session->user->profileField('timeZone') || 'America/Chicago'; my $time_zone = $self->getTimeZone;
my ($dateString,$timeString) = split(/ /,shift); my ($dateString,$timeString) = split(/ /,shift);
my @date = split(/-/,$dateString); my @date = split(/-/,$dateString);
my @time = split(/:/,$timeString); my @time = split(/:/,$timeString);
@ -781,7 +808,7 @@ The number of seconds since January 1, 1970.
sub monthStartEnd { sub monthStartEnd {
my $self = shift; my $self = shift;
my $epoch = shift; my $epoch = shift;
my $time_zone = $self->session->user->profileField('timeZone') || 'America/Chicago'; my $time_zone = $self->getTimeZone;
my $dt = DateTime->from_epoch(epoch=>$epoch, time_zone=>$time_zone); my $dt = DateTime->from_epoch(epoch=>$epoch, time_zone=>$time_zone);
my $end = DateTime->last_day_of_month(year=>$dt->year, month=>$dt->month); my $end = DateTime->last_day_of_month(year=>$dt->year, month=>$dt->month);
$dt->set_day(1); $dt->set_day(1);
@ -918,7 +945,7 @@ sub setToEpoch {
return $self->time() return $self->time()
} }
# in epochToSet we apply the user's time zone, so now we have to remove it. # in epochToSet we apply the user's time zone, so now we have to remove it.
my $time_zone = $self->session->user->profileField('timeZone') || 'America/Chicago'; my $time_zone = $self->getTimeZone;
$dt->set_time_zone($time_zone); $dt->set_time_zone($time_zone);
return $dt->epoch; return $dt->epoch;
} }