import torrus 1.0.9
[freeside.git] / FS / FS / Misc / DateTime.pm
1 package FS::Misc::DateTime;
2
3 use base qw( Exporter );
4 use vars qw( @EXPORT_OK );
5 use Carp;
6 use Date::Parse;
7 use DateTime::Format::Natural;
8 use FS::Conf;
9
10 @EXPORT_OK = qw( parse_datetime );
11
12 =head1 NAME
13
14 FS::Misc::DateTime - Date and time subroutines
15
16 =head1 SYNOPSIS
17
18 use FS::Misc::DateTime qw( parse_datetime );
19
20 =head1 SUBROUTINES
21
22 =over 4
23
24 =item parse_datetime STRING
25
26 Parses a date (and possibly time) from the supplied string and returns
27 the date as an integer UNIX timestamp.
28
29 =cut
30
31 sub parse_datetime {
32   my $string = shift;
33   return '' unless $string =~ /\S/;
34
35   my $conf = new FS::Conf;
36   my $format = $conf->config('date_format') || '%m/%d/%Y';
37
38   if ( $format eq '%d/%m/%Y' ) { #  =~ /\%d.*\%m/ ) {
39     #$format =~ s/\%//g;
40     my $parser = DateTime::Format::Natural->new( 'time_zone' => 'local',
41                                                  #'format'=>'d/m/y',#lc($format)
42                                                );
43     $dt = $parser->parse_datetime($string);
44     if ( $parser->success ) {
45       return $dt->epoch;
46     } else {
47       #carp "WARNING: can't parse date: ". $parser->error;
48       #return '';
49       #huh, very common, we still need the "partially" (fully enough for our purposes) parsed date.
50       $dt->epoch;
51     }
52   } else {
53     return str2time($string);
54   }
55   
56 }
57
58 =back
59
60 =head1 BUGS
61
62 =cut
63
64 1;