This commit was generated by cvs2svn to compensate for changes in r12472,
[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 POSIX;
6 use Carp;
7 use Date::Parse;
8 use DateTime::Format::Natural;
9 use FS::Conf;
10
11 @EXPORT_OK = qw( parse_datetime day_end );
12
13 =head1 NAME
14
15 FS::Misc::DateTime - Date and time subroutines
16
17 =head1 SYNOPSIS
18
19 use FS::Misc::DateTime qw( parse_datetime );
20
21 =head1 SUBROUTINES
22
23 =over 4
24
25 =item parse_datetime STRING
26
27 Parses a date (and possibly time) from the supplied string and returns
28 the date as an integer UNIX timestamp.
29
30 =cut
31
32 sub parse_datetime {
33   my $string = shift;
34   return '' unless $string =~ /\S/;
35
36   my $conf = new FS::Conf;
37   my $format = $conf->config('date_format') || '%m/%d/%Y';
38
39   if ( $format eq '%d/%m/%Y' ) { #  =~ /\%d.*\%m/ ) {
40     #$format =~ s/\%//g;
41     my $parser = DateTime::Format::Natural->new( 'time_zone' => 'local',
42                                                  #'format'=>'d/m/y',#lc($format)
43                                                );
44     $dt = $parser->parse_datetime($string);
45     if ( $parser->success ) {
46       return $dt->epoch;
47     } else {
48       #carp "WARNING: can't parse date: ". $parser->error;
49       #return '';
50       #huh, very common, we still need the "partially" (fully enough for our purposes) parsed date.
51       $dt->epoch;
52     }
53   } else {
54     return str2time($string);
55   }
56   
57 }
58
59 =item day_end TIME
60
61 If the next-bill-ignore-time configuration setting is turned off, just 
62 returns the passed-in value.
63
64 If the next-bill-ignore-time configuration setting is turned on, parses TIME
65 as an integer UNIX timestamp and returns a new timestamp with the same date but
66 23:59:59 for the time.
67
68 =cut
69
70 sub day_end {
71     my $time = shift;
72
73     my $conf = new FS::Conf;
74     return $time unless $conf->exists('next-bill-ignore-time');
75
76     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
77         localtime($time);
78     mktime(59,59,23,$mday,$mon,$year,$wday,$yday,$isdst);
79 }
80
81 =back
82
83 =head1 BUGS
84
85 =cut
86
87 1;