X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2FMisc%2FDateTime.pm;h=08cf9a9456c3522fc57fe95dfec7262bb7af82c4;hp=a32c15aea5dee4ff0ddcf9cbc4f0fb84d1dd938d;hb=dc213442f13860d5f44ff7ea0df4eeaf1881d64f;hpb=b5c4237a34aef94976bc343c8d9e138664fc3984 diff --git a/FS/FS/Misc/DateTime.pm b/FS/FS/Misc/DateTime.pm index a32c15aea..08cf9a945 100644 --- a/FS/FS/Misc/DateTime.pm +++ b/FS/FS/Misc/DateTime.pm @@ -3,11 +3,13 @@ package FS::Misc::DateTime; use base qw( Exporter ); use vars qw( @EXPORT_OK ); use Carp; +use Time::Local; use Date::Parse; use DateTime::Format::Natural; +use Date::Format; use FS::Conf; -@EXPORT_OK = qw( parse_datetime ); +@EXPORT_OK = qw( parse_datetime day_end iso8601 ); =head1 NAME @@ -31,13 +33,14 @@ the date as an integer UNIX timestamp. sub parse_datetime { my $string = shift; return '' unless $string =~ /\S/; + my $tz = shift || 'local'; my $conf = new FS::Conf; my $format = $conf->config('date_format') || '%m/%d/%Y'; if ( $format eq '%d/%m/%Y' ) { # =~ /\%d.*\%m/ ) { #$format =~ s/\%//g; - my $parser = DateTime::Format::Natural->new( 'time_zone' => 'local', + my $parser = DateTime::Format::Natural->new( 'time_zone' => $tz, #'format'=>'d/m/y',#lc($format) ); $dt = $parser->parse_datetime($string); @@ -47,14 +50,40 @@ sub parse_datetime { #carp "WARNING: can't parse date: ". $parser->error; #return ''; #huh, very common, we still need the "partially" (fully enough for our purposes) parsed date. - $dt->epoch; + return $dt->epoch; } } else { - return str2time($string); + return str2time($string, $tz); } } +=item day_end TIME + +Parses TIME as an integer UNIX timestamp and returns a new timestamp with the +same date but 23:59:59 for the time. + +=cut + +sub day_end { + my $time = shift; + + my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = + localtime($time); + timelocal(59,59,23,$mday,$mon,$year); +} + +=item iso8601 TIME + +Parses time as an integer UNIX timestamp and returns the ISO 8601 formatted +date and time. + +=cut + +sub iso8601 { + time2str('%Y-%m-%dT%T', shift); +} + =back =head1 BUGS