# Copyright (c) 2006-2010 bivio Software, Inc. All Rights Reserved. # # Visit http://www.bivio.biz for more info. # # This library is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; If not, you may get a copy from: # http://www.opensource.org/licenses/lgpl-license.html # # $Id: TimeZone.pm,v 1.9 2011/11/30 03:31:25 schellj Exp $ package Bivio::Type::TimeZone; use strict; use Bivio::Base 'Type.Enum'; use DateTime (); our($VERSION) = sprintf('%d.%02d', q$Revision: 1.9 $ =~ /\d+/g); my($_DT) = b_use('Type.DateTime'); __PACKAGE__->compile; b_use('IO.Config')->register(my $_CFG = { default => 'UTC', }); sub ROW_TAG_KEY { return 'TIME_ZONE'; } sub compile { my($i) = 2; return shift->SUPER::compile([ UNKNOWN => [0, 'Select Time Zone'], UTC => [1, 'UTC'], map({ my($x) = $_; $x =~ s/\W/_/g; (uc($x) => [$i++, $_]); } grep(!/UTC/, DateTime::TimeZone->all_names)), ]); } sub date_time_from_utc { my($self, $date_time) = @_; return _convert($self, $date_time, $self->UTC, $self); } sub date_time_to_utc { my($self, $date_time) = @_; return _convert($self, $date_time, $self, $self->UTC); } sub as_display_name { return shift->get_long_desc; } sub get_default { return $_CFG->{default}; } sub handle_config { my($proto, $cfg) = @_; $_CFG->{default} = $proto->from_any($cfg->{default}); return; } sub _convert { my($self, $date_time, $source, $target) = @_; my($sec, $min, $hour, $mday, $mon, $year) = $_DT->to_parts($date_time); my($dt) = scalar(DateTime->new( year => $year, month => $mon, day => $mday, hour => $hour, minute => $min, second => $sec, time_zone => $source->get_short_desc, )); $dt->set_time_zone($target->get_short_desc); return $_DT->from_parts_or_die( $dt->second, $dt->minute, $dt->hour, $dt->day, $dt->month, $dt->year); } 1;