/[suikacvs]/messaging/manakai/lib/Message/Field/Date.pm
Suika

Contents of /messaging/manakai/lib/Message/Field/Date.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.19 - (hide annotations) (download)
Wed Nov 13 08:08:51 2002 UTC (22 years ago) by wakaba
Branch: MAIN
CVS Tags: msg-0-1
Branch point for: stable
Changes since 1.18: +15 -3 lines
2002-08-05  Wakaba <w@suika.fam.cx>

	* Util.pm:
	- (sprintxf): Use Message::Util::Wide::unquote_if_quoted_string
	instead of Message::Util::unquote_if_quoted_string.
	- (Message::Util::Wide): New package.
	- (%Message::Util::Wide::REG): New hash.
	- (Message::Util::unquote_if_quoted_string): New function.
	- NOTE: "Wide" package is created to support utf8 string
	of perl 5.7.3 or later.  Utf8 string does not work
	only for [\x00-\xFF] regex of current functions,
	and this regex is used as (?:.|\x0D|\x0A).  (Without
	's' option, "." does not match with newline character.)
	When we can do away problematic code from all
	Message::* modules, we can also do away "Wide" package.

1 wakaba 1.17
2     =head1 NAME
3    
4     Message::Field::Date --- Perl module for various styles of
5     date-time used in Internet messages and so on
6    
7     =cut
8    
9     ## This file is written in UTF-8
10    
11     package Message::Field::Date;
12     use strict;
13     use vars qw(%DEFAULT %FMT2STR @ISA %MONTH %REG $VERSION %ZONE);
14 wakaba 1.19 $VERSION=do{my @r=(q$Revision: 1.18 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
15 wakaba 1.17 require Message::Field::Structured;
16     push @ISA, qw(Message::Field::Structured);
17     use Time::Local 'timegm_nocheck';
18     use overload '==' => sub { $_[0]->{date_time} == $_[1] },
19     '<=' => sub { $_[0]->{date_time} <= $_[1] },
20     '>=' => sub { $_[0]->{date_time} >= $_[1] },
21     '0+' => sub { $_[0]->{date_time} },
22     fallback => 1;
23    
24     {
25     %REG = %Message::Util::REG;
26     my $_ALPHA = q{[A-Za-z]};
27     $_ALPHA = q{[A-Za-z\x{0080}-\x{7FFFFFFF}]} if defined $^V && $^V gt v5.7;
28     ## RFC 822/2822 Internet Message Format
29     $REG{M_dt_rfc822} = qr!(?:[A-Za-z]+ ## Day of week
30     [\x09\x20,]*)? ([0-9]+) ## Day
31     [\x09\x20/-]* ($_ALPHA+) ## Month
32     [\x09\x20/-]* ([0-9]+) ## Year
33     [\x09\x20:Tt-]+ ([0-9]+) ## Hour
34     [\x09\x20:]+ ([0-9]+) ## Minute
35     [\x09\x20:]* ([0-9]+)? ## Second
36     ([\x09\x20 0-9A-Za-z+-]+)!x; ## Zone
37     ## RFC 3339 Internet Date/Time Format (A profile of ISO 8601)
38     $REG{M_dt_rfc3339} = qr! ([0-9]{4,}) ## Year
39     [\x09\x20.:/-]+ ([0-9]+) ## Month
40     [\x09\x20.:/-]+ ([0-9]+) ## Day
41     (?:[\x09\x20.:Tt-]+ ([0-9]+) ## Hour
42     [\x09\x20.:]+ ([0-9]+) ## Minute
43     (?:[\x09\x20.:]+ ([0-9]+) ## Second
44     (?:[\x09\x20.:]+ ([0-9]+))?)?)? ## frac.
45     ([\x09\x20 0-9A-Za-z:.+-]*) !x; ## Zone.
46     ## RFC 733 ARPA Internet Message Format
47     $REG{M_dt_rfc733} = qr!(?:[A-Za-z]+ ## Day of week
48     [\x09\x20,]*)? ([0-9]+) ## Day
49     [\x09\x20/-]* ([A-Za-z]+) ## Month
50     [\x09\x20/-]* ([0-9]+) ## Year
51     [\x09\x20:Tt-]+ ([0-9][0-9]) ## Hour
52     [\x09\x20:]* ([0-9][0-9]) ## Minute
53     [\x09\x20:]* ([0-9][0-9])? ## Second
54     ([\x09\x20 0-9A-Za-z+-]+)!x; ## Zone
55     ## RFC 724 ARPA Internet Message Format (slash-date)
56     $REG{M_dt_rfc724} = qr!(?:[A-Za-z]+ ## Day of week
57     [\x09\x20,]*)? ([0-9][0-9]?) ## Month
58     [\x09\x20/]+ ([0-9][0-9]?) ## Day
59     [\x09\x20/]+ ([0-9]{2,}) ## Year
60     [\x09\x20:Tt-]+ ([0-9][0-9]) ## Hour
61     [\x09\x20:]* ([0-9][0-9]) ## Minute
62     [\x09\x20:]* ([0-9][0-9])? ## Second
63     ([\x09\x20 0-9A-Za-z+-]+)!x; ## Zone
64     }
65    
66     =head1 CONSTRUCTORS
67    
68     The following methods construct new objects:
69    
70     =over 4
71    
72     =cut
73    
74     ## Initialize of this class -- called by constructors
75     %DEFAULT = (
76     -_MEMBERS => [qw|date_time secfrac|],
77     -_METHODS => [qw|unix_time second_fraction
78     comment_add comment_count comment_item comment_delete|],
79     -date_format => 'string', ## 'unix' / 1*ALPHA
80     #field_param_name
81     #field_name
82     #format
83     #hook_encode_string
84     #hook_decode_string
85     -output_comment => 1,
86     -use_comment => 1,
87     -use_military_zone => +1, ## +1 / -1 / 0
88     #zone => [+1, 0, 0],
89     -zone_default_string => '-0000',
90     );
91    
92     %MONTH = (
93     JAN => 1, JANUARY => 1,
94     FEB => 2, FEBRUARY => 2,
95     MAR => 3, MARCH => 3,
96 wakaba 1.19 APR => 4, APRIL => 4, ARL => 4,
97 wakaba 1.17 MAY => 5,
98     JUN => 6, JUNE => 6,
99     JUL => 7, JULY => 7,
100     AUG => 8, AUGUST => 8,
101     SEP => 9, SEPTEMBER => 9, SEPT => 9,
102     OCT => 10, OCTOBER => 10,
103     NOV => 11, NOVEMBER => 11,
104     DEC => 12, DECEMBER => 12,
105     );
106    
107     {
108     my $_p2f = sub { my ($s, $n) = @_; $s eq 'none'? q(%d): $s eq 'SP'? qq(%${n}d): qq(%0${n}d) };
109     my $_tm = sub { $_[0]->{local}?'tm_local':'tm' };
110     ## &$function ({format's parameters}, {caller's parameters})
111     %FMT2STR = (
112     CC => sub { sprintf &$_p2f ($_[0]->{pad}, 2), ## BUG: Support AD only
113     (($_[1]->{ &$_tm ($_[0]) }->[5] + 1899) / 100) + 1 },
114     YYYY => sub { sprintf &$_p2f ($_[0]->{pad}, 4),
115     $_[1]->{ &$_tm ($_[0]) }->[5] + 1900 },
116     YY => sub { sprintf &$_p2f ($_[0]->{pad}, 2),
117     $_[1]->{ &$_tm ($_[0]) }->[5] % 100 },
118     MM => sub { sprintf &$_p2f ($_[0]->{pad}, 2),
119     $_[1]->{ &$_tm ($_[0]) }->[4] + 1 },
120     Mon => sub { qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
121     [ $_[1]->{ &$_tm ($_[0]) }->[4] ] },
122     Month => sub { qw(January February March April May June
123     July August September October November December)
124     [ $_[1]->{ &$_tm ($_[0]) }->[4] ] },
125     DD => sub { sprintf &$_p2f ($_[0]->{pad}, 2),
126     $_[1]->{ &$_tm ($_[0]) }->[3] },
127     Wdy => sub { qw(Sun Mon Tue Wed Thu Fri Sat)
128     [ $_[1]->{ &$_tm ($_[0]) }->[6] ] },
129     Weekday => sub {
130     (split /:/, $_[0]->{name}
131     || q(Sunday:Monday:Tuesday:Wednesday:Thursday:Friday:Saturday))
132     [ $_[1]->{ &$_tm ($_[0]) }->[6] ] },
133     shun => sub {
134     my @alphabet = split /:/, $_[0]->{alphabet} || 'a:b:c:c';
135     my $day = $_[1]->{ &$_tm ($_[0]) }->[3];
136     $day < 10? $alphabet[0]: ## 1 - 9 joujun
137     $day < 20? $alphabet[1]: ## 10 - 19 chuujun
138     $day < 30? $alphabet[2]: ## 20 - 29 gejun
139     defined $alphabet[3] ?
140     $alphabet[3]: $alphabet[2]; ## 30 - 31 gejun
141     },
142     HH => sub { sprintf &$_p2f ($_[0]->{pad}, 2),
143     $_[1]->{ &$_tm ($_[0]) }->[2] },
144     TT => sub { sprintf &$_p2f ($_[0]->{pad}, 2),
145     $_[1]->{ &$_tm ($_[0]) }->[1] },
146     SS => sub { sprintf &$_p2f ($_[0]->{pad}, 2),
147     $_[1]->{ &$_tm ($_[0]) }->[0] },
148     unix => sub { sprintf &$_p2f ($_[0]->{pad}, 10),
149     $_[1]->{ $_[0]->{local}?'time_local':'time' } },
150     frac => sub { $_[1]->{ $_[0]->{local}?'secfrac_local':'secfrac' } },
151     zsign => sub { $_[1]->{option}->{zone}->[0] > 0 ? '+' : '-' },
152     zHH => sub { sprintf &$_p2f ($_[0]->{pad}, 2), $_[1]->{option}->{zone}->[1] },
153     zTT => sub { sprintf &$_p2f ($_[0]->{pad}, 2), $_[1]->{option}->{zone}->[2] },
154     comment => sub { $_[1]->{comment} },
155     );
156     }
157    
158     %ZONE = ( ## NA = Northern America
159     ADT => [-1, 3, 0], ## (NA)Atlantic Daylight 733
160     CHST => [-1, 10, 0], ## Alaska-Hawaii Standard
161     AST => [-1, 4, 0], ## (NA)Atlantic Standard 733
162     AT => [-1, 2, 0], ## Azores
163     BDT => [-1, 10, 0], ## 733
164     BST => [-1, 11, 0], ## 733
165     #BST => [+1, 1, 0], ## British Summer
166     #BST => [-1, 3, 0], ## Brazil Standard
167     BT => [+1, 3, 0], ## Baghdad
168     CADT => [+1, 10, 30], ## Central Australian Daylight
169     CAST => [+1, 9, 30], ## Central Australian Standard
170     CAT => [-1, 10, 0], ## Central Alaska
171     CCT => [+1, 8, 0], ## China Coast
172     CDT => [-1, 5, 0], ## (NA)Central Daylight 733, 822
173     CET => [+1, 1, 0], ## Central European
174     CEST => [+1, 2, 0], ## Central European Daylight
175     CST => [-1, 6, 0], ## (NA)Central Standard 733, 822
176     EADT => [+1, 11, 0], ## Eastern Australian Daylight
177     EADT => [+1, 10, 0], ## Eastern Australian Standard
178 wakaba 1.19 #EASTERN
179 wakaba 1.17 ECT => [+1, 1, 0], ## Central European (French)
180     EDT => [-1, 4, 0], ## (NA)Eastern Daylight 733, 822
181     EEST => [+1, 3, 0], ## Eastern European Summer
182     EET => [+1, 2, 0], ## Eastern Europe 1947
183     EST => [-1, 5, 0], ## (NA)Eastern Standard 733, 822
184     EWT => [-1, 4, 0], ## U.S. Eastern War Time
185     FST => [+1, 2, 0], ## French Summer
186     FWT => [+1, 1, 0], ## French Winter
187     GDT => [+1, 1, 0], ## 724
188 wakaba 1.19 #GM
189 wakaba 1.17 GMT => [+1, 0, 0], ## Greenwich Mean 733, 822
190     #GST => [-1, 3, 0], ## Greenland Standard
191     GST => [+1, 10, 0], ## Guam Standard
192     HDT => [-1, 9, 0], ## Hawaii Daylight 733
193     HKT => [+1, 8, 0], ## Hong Kong
194     HST => [-1, 10, 0], ## Hawaii Standard 733
195     IDLE => [+1, 12, 0], ## International Date Line East
196     IDLW => [-1, 12, 0], ## International Date Line West
197     IDT => [+1, 3, 0],
198     IST => [+1, 2, 0], ## Israel standard
199     #IST => [+1, 5, 30], ## Indian standard
200     IT => [+1, 3, 30], ## Iran
201     JST => [+1, 9, 0], ## Japan Central Standard
202 wakaba 1.18 #JT => [+1, 9, 0], ## Japan Central Standard
203 wakaba 1.17 JT => [+1, 7, 30], ## Java
204     KDT => [+1, 10, 0], ## Korean Daylight
205     KST => [+1, 9, 0], ## Korean Standard
206     LCL => [-1, 0, 0], ## (unknown zone used by LSMTP)
207     LOCAL => [-1, 0, 0], ## local time zone
208 wakaba 1.19 #LON
209 wakaba 1.17 LT => [-1, 0, 0], ## Luna Time [RFC 1607]
210     MDT => [-1, 6, 0], ## (NA)Mountain Daylight 733, 822
211     MET => [+1, 0, 0], ## Middle European
212     METDST => [+1, 2, 0],
213     MEST => [+1, 2, 0], ## Middle European Summer
214     MEWT => [+1, 0, 0], ## Middle European Winter
215     MEZ => [+1, 0, 0], ## Central European (German)
216     MST => [-1, 7, 0], ## (NA)Mountain Standard 733, 822
217 wakaba 1.19 MOUNTAIN => [-1, 7, 0], ## (maybe) (NA)Mountain Standard 733, 822
218 wakaba 1.17 MT => [-1, 0, 0], ## Mars Time [RFC 1607]
219     NDT => [-1, 2, 30], ## Newfoundland Daylight
220     NFT => [-1, 3, 30], ## Newfoundland Standard
221     NST => [-1, 3, 30], ## Newfoundland Standard 733
222     #NST => [-1, 6, 30], ## North Sumatra
223     NT => [-1, 11, 0], ## Nome
224     NZD => [+1, 13, 0], ## New Zealand Daylight
225     NZT => [+1, 12, 0], ## New Zealand
226     NZDT => [+1, 13, 0], ## New Zealand Daylight
227 wakaba 1.19 NZS => [+1, 12, 0], ## (maybe) New Zealand Standard
228 wakaba 1.17 NZST => [+1, 12, 0], ## New Zealand Standard
229     PDT => [-1, 7, 0], ## (NA)Pacific Daylight 733, 822
230 wakaba 1.19 #PM
231 wakaba 1.17 PST => [-1, 8, 0], ## (NA)Pacific Standard 733, 822
232 wakaba 1.19 #SAMST
233 wakaba 1.17 SET => [+1, 1, 0], ## Seychelles
234     SST => [+1, 2, 0], ## Swedish Summer
235     #SST => [+1, 7, 0], ## South Sumatra
236     SWT => [+1, 1, 0], ## Swedish Winter
237     UKR => [+1, 2, 0], ## Ukraine
238 wakaba 1.19 UNDEFINED => [-1, 0, 0], ## undefined
239 wakaba 1.17 UT => [+1, 0, 0], ## Universal Time 822
240     UTC => [+1, 0, 0], ## Coordinated Universal Time
241     WADT => [+1, 8, 0], ## West Australian Daylight
242     WAT => [-1, 0, 0], ## West Africa
243     WET => [+1, 0, 0], ## Western European
244     WST => [+1, 8, 0], ## West Australian Standard
245     YDT => [-1, 8, 0], ## Yukon Daylight 733
246     YST => [-1, 9, 0], ## Yukon Standard 733
247     Z => [+1, 0, 0], ## 822, ISO 8601
248     ZP4 => [+1, 4, 0], ## Z+4
249     ZP5 => [+1, 5, 0], ## Z+5
250     ZP6 => [+1, 6, 0], ## Z+6
251     );
252    
253     ## -use_military_zone => +1 / -1 / 0
254     ## Whether military zone names are understood or not.
255     ## +1 Admits them and treats as standard value. (eg. "A" = +0100)
256     ## -1 Admits them but treats as negative value. (eg. "A" = -0100)
257     ## 0 They are ignored and zone is set as -0000. (eg. "A" = -0000)
258     ## Because of typo in BNF comment of RFCs 733 and 822,
259     ## quite a few implemention use these values incorrectly.
260     ## As a result, these zone names carry no worthful information.
261     ## RFC 2822 recommends these names be taken as '-0000' (i.e.
262     ## unknown zone).
263    
264     sub _set_military_zone_name ($) {
265     my $self = shift;
266     my $mode = $self->{option}->{use_military_zone};
267     my $i = 0;
268     if ($mode == 0) {
269     for my $letter ('A'..'Y') {$ZONE{$letter} = [-1, 0, 0]} return;
270     }
271     for my $letter ('Z', 'A'..'I', 'K'..'M') {
272     $ZONE{$letter} = [+1*$mode, $i++, 0];
273     } $i = 1;
274     for my $letter ('N'..'Y') {
275     $ZONE{$letter} = [-1*$mode, $i++, 0];
276     }
277     }
278    
279     sub _init ($;%) {
280     my $self = shift;
281     my $DEFAULT = Message::Util::make_clone (\%DEFAULT);
282     my %option = @_;
283     $self->SUPER::_init (%$DEFAULT, %option);
284 wakaba 1.18 $self->{date_time} = 0;
285 wakaba 1.17 $self->{date_time} = $option{unix} if defined $option{unix};
286     $self->{secfrac} = $option{frac} if defined $option{frac};
287     unless (ref $self->{option}->{zone}) {
288     my $zone = $self->{option}->{zone} || ''; #$main::ENV{TZ} || '';
289     ## Since _zone_string_to_array does not provide full support
290     ## of parsing TZ format (eg. daytime), not seeing it might be
291     ## better way.
292     if (length $zone) {
293     $self->{option}->{zone} = [ $self->_zone_string_to_array ($zone) ];
294     } else {
295     my $time = time;
296     my $ltime = timegm_nocheck (localtime ($time));
297     my $o = int ( ($ltime - $time) / 60);
298     my @zone;
299     $zone[2] = $o % 60; $o = int ( $o / 60 );
300     $zone[1] = $o % 24;
301     $zone[0] = $o >= 0 ? +1 : -1;
302     $self->{option}->{zone} = \@zone;
303     }
304     }
305    
306     my $format = $self->{option}->{format};
307     if ($format =~ /mail-rfc2822/) {
308     $self->{option}->{use_military_zone} = 0;
309     }
310    
311     $self->_set_military_zone_name;
312     }
313    
314     =item $date = Message::Field::Date->new ([%options])
315    
316     Constructs a new object. You might pass some options as parameters
317     to the constructor.
318    
319     =cut
320    
321     ## Inherited
322    
323     =item $date = Message::Field::Date->parse ($field-body, [%options])
324    
325     Constructs a new object with given field body. You might pass
326     some options as parameters to the constructor.
327    
328     =cut
329    
330     sub parse ($$;%) {
331     my $class = shift;
332     my $self = bless {}, $class;
333     my $body = shift;
334     $self->_init (@_);
335     ($body, @{$self->{comment}})
336     = $self->Message::Util::delete_comment_to_array ($body)
337     if $self->{option}->{use_comment};
338     $body =~ s/^$REG{WSP}+//; $body =~ s/$REG{WSP}+$//;
339     if ($self->{option}->{date_format} eq 'unix') {
340     $self->{date_time} = int ($body);
341     } elsif (!$body) {
342     return $self;
343     } elsif ($body =~ /^$REG{M_dt_rfc822}$/x) {
344     my ($day, $month, $year, $hour, $minute, $second, $zone)
345     = ($1, uc $2, $3, $4, $5, $6, uc $7);
346     $month = $MONTH{$month} || 1;
347     $year = $self->_obvious_year ($year) if length($year)<4;
348     my ($zone_sign, $zone_hour, $zone_minute) = $self->_zone_string_to_array ($zone);
349     eval '$self->{date_time} = timegm_nocheck
350     ($second, $minute-($zone_sign*$zone_minute), $hour-($zone_sign*$zone_hour),
351     $day, $month-1, $year);';
352     $self->{secfrac} = '';
353     $self->{option}->{zone} = [$zone_sign, $zone_hour, $zone_minute];
354     } elsif ($body =~ /^$REG{M_dt_rfc3339}$/x) {
355     my ($year,$month,$day,$hour,$minute,$second,$secfrac,$zone)
356     = ($1, $2, $3, $4, $5, $6, $7, $8);
357     my ($zone_sign, $zone_hour, $zone_minute) = $self->_zone_string_to_array ($zone);
358     eval '$self->{date_time} = timegm_nocheck
359     ($second, $minute-($zone_sign*$zone_minute), $hour-($zone_sign*$zone_hour),
360     $day, $month-1, $year);';
361     $self->{secfrac} = $secfrac;
362     $self->{option}->{zone} = [$zone_sign, $zone_hour, $zone_minute];
363     } elsif ($body =~ /^$REG{M_dt_rfc733}$/x) {
364     my ($day, $month, $year, $hour, $minute, $second, $zone)
365     = ($1, uc $2, $3, $4, $5, $6, uc $7);
366     $month = $MONTH{$month} || 1;
367     $year = $self->_obvious_year ($year) if length($year)<4;
368     my ($zone_sign, $zone_hour, $zone_minute) = $self->_zone_string_to_array ($zone);
369     eval '$self->{date_time} = timegm_nocheck
370     ($second, $minute-($zone_sign*$zone_minute), $hour-($zone_sign*$zone_hour),
371     $day, $month-1, $year);';
372     $self->{secfrac} = '';
373     $self->{option}->{zone} = [$zone_sign, $zone_hour, $zone_minute];
374     } elsif ($body =~ /^$REG{M_dt_rfc724}$/x) {
375     my ($month, $day, $year, $hour, $minute, $second, $zone)
376     = ($1, $2, $3, $4, $5, $6, uc $7);
377     $year = $self->_obvious_year ($year) if length($year)<4;
378     my ($zone_sign, $zone_hour, $zone_minute) = $self->_zone_string_to_array ($zone);
379     eval '$self->{date_time} = timegm_nocheck
380     ($second, $minute-($zone_sign*$zone_minute), $hour-($zone_sign*$zone_hour),
381     $day, $month-1, $year);';
382     $self->{secfrac} = '';
383     $self->{option}->{zone} = [$zone_sign, $zone_hour, $zone_minute];
384     #} elsif ($body =~ /^[0-9]+$/) {
385     # $self->{date_time} = $&;
386     } else {
387     ## From HTTP::Date (revision 1.40) by Gisle Aas
388     #$body =~ s/^\s+//; # kill leading space
389     $body =~ s/^(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)[a-z]*,?\s*//i; # Useless weekday
390     my ($day, $month, $year, $hour, $minute, $second);
391     my ($secfrac, $zone, $ampm) = ('', $self->{option}->{zone_default_string});
392    
393     # Then we are able to check for most of the formats with this regexp
394     (($day,$month,$year,$hour,$minute,$second,$zone) =
395     $body =~ m"^
396     (\d\d?) # day
397     (?:\s+|[-\/])
398     (\w+) # month
399     (?:\s+|[-\/])
400     (\d+) # year
401     (?:
402     (?:\s+|:) # separator before clock
403     (\d\d?):(\d\d) # hour:min
404     (?::(\d\d))? # optional seconds
405     )? # optional clock
406     \s*
407     ([-+]?\d{2,4}|(?![APap][Mm]\b)[A-Za-z]+)? # timezone
408     $"x)
409    
410     ||
411    
412     # Try the ctime and asctime format
413     (($month, $day, $hour, $minute, $second, $zone, $year) =
414     $body =~ m"^
415     (\w{1,3}) # month
416     \s+
417     (\d\d?) # day
418     \s+
419     (\d\d?):(\d\d) # hour:min
420     (?::(\d\d))? # optional seconds
421     \s+
422     (?:([A-Za-z]+)\s+)? # optional timezone
423     (\d+) # year
424     $"x)
425    
426     ||
427    
428     # Then the Unix 'ls -l' date format
429     (($month, $day, $year, $hour, $minute, $second) =
430     $body =~ m"^
431     (\w{3}) # month
432     \s+
433     (\d\d?) # day
434     \s+
435     (?:
436     (\d\d\d\d) | # year
437     (\d{1,2}):(\d{2}) # hour:min
438     (?::(\d\d))? # optional seconds
439     )
440     $"x)
441    
442     ||
443    
444     # ISO 8601 format '1996-02-29 12:00:00 -0100' and variants
445     (($year, $month, $day, $hour, $minute, $second, $secfrac, $zone) =
446     $body =~ m"^
447     (\d{4}) # year
448     [-\/]?
449     (\d\d?) # numerical month
450     [-\/]?
451     (\d\d?) # day
452     (?:
453     (?:\s+|[-:Tt]) # separator before clock
454     (\d\d?):?(\d\d) # hour:min
455     (?:
456     :?
457     (\d\d)
458     (?:\.?(\d+))? ## optional second frac.
459     )? # optional seconds
460     )? # optional clock
461     \s*
462     ([-+]?\d\d?:?(:?\d\d)?
463     |Z|z)? # timezone (Z is 'zero meridian', i.e. GMT)
464    
465     $"x)
466    
467     ||
468    
469     # ISO 8601 like format '96-02-29 2:0:0 -0100' and variants
470     (($year, $month, $day, $hour, $minute, $second, $secfrac, $zone) =
471     $body =~ m"^
472     (\d+) # year
473     [-/]
474     (\d\d?) # numerical month
475     [-/]
476     (\d\d?) # day
477     (?:
478     (?:\s+|[-:Tt]) # separator before clock
479     (\d\d?):(\d+) # hour:min
480     (?:
481     :
482     (\d+)
483     (?:\.(\d+)) ## optional second frac.
484     )? # optional seconds
485     )? # optional clock
486     \s*
487     ([-+]?\d+(:\d+)?
488     |Z|z)? # timezone (Z is 'zero meridian', i.e. GMT)
489    
490     $"x)
491    
492     ||
493    
494     # Windows 'dir' 11-12-96 03:52PM
495     (($month, $day, $year, $hour, $minute, $ampm) =
496     $body =~ m"^
497     (\d{2}) # numerical month
498     -
499     (\d{2}) # day
500     -
501     (\d{2}) # year
502     \s+
503     (\d\d?):(\d\d)([APap][Mm]) # hour:min AM or PM
504     $"x)
505    
506     #||
507     #return; # unrecognized format
508     ;
509    
510     $day ||= 1;
511     # Translate month name to number
512     $month ||= 1;
513     $month = $MONTH{uc $month}
514     || int ($month);
515    
516     # If the year is missing, we assume first date before the current,
517     # because of the formats we support such dates are mostly present
518     # on "ls -l" listings.
519     unless (defined $year) {
520     my $cur_mon;
521     ($cur_mon, $year) = (localtime)[4, 5];
522     $year += 1900; $cur_mon++;
523     $year-- if $month > $cur_mon;
524     } elsif (length($year) < 3) {
525     $year = $self->_obvious_year ($year);
526     }
527    
528     # Make sure clock elements are defined
529     $hour = 0 unless defined($hour);
530     $minute = 0 unless defined($minute);
531     $second = 0 unless defined($second);
532    
533     # Compensate for AM/PM
534     if ($ampm) {
535     $ampm = uc $ampm;
536     $hour = 0 if $hour == 12 && $ampm eq 'AM';
537     $hour += 12 if $ampm eq 'PM' && $hour != 12;
538     }
539    
540     my ($zone_sign, $zone_hour, $zone_minute) = $self->_zone_string_to_array ($zone);
541     eval '$self->{date_time} = timegm_nocheck
542     ($second, $minute-($zone_sign*$zone_minute), $hour-($zone_sign*$zone_hour),
543     $day, $month-1, $year);';
544     $self->{secfrac} = $secfrac;
545     $self->{option}->{zone} = [$zone_sign, $zone_hour, $zone_minute];
546     }
547     $self;
548     }
549    
550     sub zone ($;$) {
551     my $self = shift;
552     my $newzone = shift;
553     if (ref $newzone) {
554     $self->{option}->{zone} = $newzone;
555     } elsif (length $newzone) {
556     $self->{option}->{zone} = [$self->_zone_string_to_array ($newzone)];
557     }
558     $self->{option}->{zone};
559     }
560    
561     ## Find "obvious" year
562     sub _obvious_year ($$) {
563     my $self = shift;
564     my $year = shift;
565     if ($self->{option}->{format} =~ /mail|news/) {
566     ## RFC 2822
567     if ( 0 <=$year && $year < 50) {$year += 2000}
568     elsif (50 < $year && $year < 1000) {$year += 1900}
569     } else {
570     ## RFC 2616
571     my $cur_yr = (localtime)[5] + 1900;
572     my $m = $cur_yr % 100;
573     my $tmp = $year;
574     $year += $cur_yr - $m;
575     $m -= $tmp;
576     $year += ($m > 0) ? 100 : -100 if abs($m) > 50;
577     }
578     $year;
579     }
580    
581     =back
582    
583     =head1 METHODS
584    
585     =over 4
586    
587    
588     =head2 $self->unix_time ([$new_time])
589    
590     Returns or set the unix-time (seconds from the Epoch).
591    
592     =cut
593    
594     sub unix_time ($;$) {
595     my $self = shift;
596     my $new_time = shift;
597     if (defined $new_time) {
598     $self->{date_time} = $new_time + 0;
599     }
600     $self->{date_time};
601     }
602    
603     sub set_datetime ($@) {
604     my $self = shift;
605     my ($year,$month,$day,$hour,$minute,$second,%misc) = @_;
606     my ($zone_sign, $zone_hour, $zone_minute)
607     = $self->_zone_string_to_array ($misc{zone});
608     eval '$self->{date_time} = timegm_nocheck
609     ($second, $minute-($zone_sign*$zone_minute), $hour-($zone_sign*$zone_hour),
610     $day, $month-1, $year);';
611     $self->{secfrac} = $misc{secfrac};
612     $self->{option}->{zone} = [$zone_sign, $zone_hour, $zone_minute];
613     }
614    
615     =head2 $self->second_fraction ([$new_fraction])
616    
617     Returns or set the decimal fraction of a second.
618     Value is a string containing of only [0-9]
619     or empty string.
620    
621     Note that this implemention is temporary and in the near future
622     it can be changed.
623    
624     =cut
625    
626     sub second_fraction ($;$) {
627     my $self = shift;
628     my $new_fraction = shift;
629     if (defined $new_fraction) {
630     $self->{secfrac} = $new_fraction unless $new_fraction =~ /[^0-9]/;
631     }
632     $self->{secfrac};
633     }
634    
635     =item $field-body = $date->stringify ()
636    
637     Returns C<field-body> as a string.
638    
639     =cut
640    
641     sub stringify ($;%) {
642     my $self = shift;
643     my %o = @_;
644     my %option = %{$self->{option}};
645 wakaba 1.18 $option{format_parameters} ||= {};
646 wakaba 1.17 for (grep {/^-/} keys %o) {$option{substr ($_, 1)} = $o{$_}}
647     unless ($option{format_template}) {
648     if ($option{format} =~ /mail-rfc2822|news-usefor|mail-rfc822\+rfc1123|news-son-of-rfc1036|mime/) {
649     $option{format_template} = '%Wdy(local);, %DD(local); %Mon(local); %YYYY(local); %HH(local);:%TT(local);:%SS(local); %zsign;%zHH;%zTT;%comment(prefix=>" ");';
650     } elsif ($option{format} =~ /http/) {
651     $option{format_template} = '%Wdy;, %DD; %Mon; %YYYY; %HH;:%TT;:%SS; GMT';
652     } elsif ($option{format} =~ /mail-rfc822|news-rfc1036/) {
653     $option{format_template} = '%Wdy(local);, %DD(local); %Mon(local); %YY(local); (%YYYY(local);) %HH(local);:%TT(local);:%SS(local); %zsign;%zHH;%zTT;%comment(prefix=>" ");';
654     } elsif ($option{format} =~ /news-rfc850/) {
655     $option{format_template} = '%Weekday;, %DD;-%Mon;-%YY; %HH;:%TT;:%SS; GMT';
656     } elsif ($option{format} =~ /asctime/) {
657     $option{format_template} = '%Wdy; %Mon; %DD(pad=>SP); %HH;:%MM;:%SS; %YYYY;';
658     #} elsif ($option{format} =~ /date\(1\)/) {
659     # $option{format_template} = '%Wdy; %Mon; %DD(pad=>SP); %HH;:%MM;:%SS; GMT %YYYY;';
660     } elsif ($option{format} =~ /un[i*]x/) { ## ;-)
661     $option{format_template} = '%unix;';
662     } else { ## RFC 3339 (IETF's ISO 8601)
663     $option{format_template} = '%YYYY(local);-%MM(local);-%DD(local);T%HH(local);:%TT(local);:%SS(local);%frac(prefix=>.);%zsign;%zHH;:%zTT;%comment(prefix=>" ");';
664     }
665     }
666     my $zone = $option{zone};
667     $zone = [ $self->_zone_string_to_array ($zone) ] if not ref $zone && length $zone;
668     $zone = [+0, 0, 0] unless ref $zone;
669     my $l_time = $self->{date_time} + $zone->[0] * ($zone->[1] * 60 + $zone->[2]) * 60;
670     my $c = '';
671     $c = $self->_comment_stringify
672     if $option{output_comment} && @{ $self->{comment} } > 0;
673 wakaba 1.18 &Message::Util::sprintxf ($option{format_template}, {
674 wakaba 1.17 comment => $c,
675 wakaba 1.18 fmt2str => ($option{format_macros} || \%FMT2STR),
676 wakaba 1.17 option => \%option,
677     time => $self->{date_time},
678     time_local => $l_time,
679     tm => [ gmtime $self->{date_time} ],
680     tm_local => [ gmtime $l_time ],
681     secfrac => $self->{secfrac},
682     secfrac_local => $self->{secfrac}, ## Not supported yet
683 wakaba 1.18 %{ $option{format_parameters} },
684 wakaba 1.17 });
685     }
686     *as_string = \&stringify;
687     *as_plain_string = \&stringify;
688     ## You should use stringify instead of as_rfc2822_time
689     sub as_rfc2822_time ($@) {
690     shift->stringify (-format => 'mail-rfc2822', @_);
691     }
692    
693     sub _zone_string_to_array ($$;$) {
694     my $self = shift;
695     my $zone = shift;
696     return (+1, 0, 0) unless defined $zone;
697     my $format = shift;
698     my @azone = [+1, 0, 0];
699     $zone =~ tr/\x09\x20//d;
700     if ($zone =~ /^[^0-9+-]+(?:([+-]?)([0-9]{1,2}))(?::([0-9]{1,2}))?/) {
701     ## $ENV{TZ} format, but is not fully supported
702     my ($s, $h, $m) = ($1, $2, $3);
703     $s ||= '+'; $s =~ tr/+-/-+/;
704     @azone = ("${s}1", 0+$h, 0+$m);
705 wakaba 1.19 } elsif ($zone =~ /^GMT([+-])([0-9][0-9]?)([0-9][0-9])?/i) {
706     @azone = ("${1}1", 0+$2, 0+$3);
707 wakaba 1.17 } elsif ($zone =~ /([+-])([0-9][0-9])([0-9][0-9])/) {
708     @azone = ("${1}1", $2, $3);
709 wakaba 1.19 } elsif ($zone =~ /([+-])([0-9])([0-9][0-9])/) {
710     @azone = ("${1}1", $2, $3);
711 wakaba 1.17 } elsif ($zone =~ /([+-]?)([0-9]+)(?:[:.-]([0-9]+))?/) {
712     @azone = ("${1}1", $2, 0+$3);
713     } else { $zone =~ tr/-//d;
714     if (ref $ZONE{$zone}) {@azone = @{$ZONE{$zone}}}
715     elsif ($zone) {@azone = (-1, 0, 0)}
716     }
717     # }
718     @azone;
719     }
720    
721     =item $option-value = $date->option ($option-name)
722    
723     Gets option value.
724    
725     =item $date->option ($option-name, $option-value, ...)
726    
727     Set option value(s). You can pass multiple option name-value pair
728     as parameter when setting.
729    
730     =item $clone = $date->clone ()
731    
732     Returns a copy of the object.
733    
734     =cut
735    
736     ## option, clone, method_available: Inherited
737    
738     =head1 EXAMPLE
739    
740     use Message::Field::Date;
741    
742     print Message::Field::Date->new (unix => time,
743     -zone => '+0900'),"\n"; ## Thu, 16 May 2002 17:53:44 +0900
744     print Message::Field::Date->new (unix => time,
745     -format_template => ## Century: 21, Year: 02, Month: 05
746     'Century: %CC;, Year: %YY;, Month: %MM;'),"\n";
747    
748     my $field_body = '04 Feb 2002 00:12:33 CST';
749     my $field = Message::Field::Date->parse ($field_body);
750    
751     print "RFC 2822:\t", $field->stringify (-format => 'mail-rfc2822'), "\n";
752     print "HTTP preferred:\t", $field->stringify (-format => 'http-1.1'), "\n";
753     print "ISO 8601:\t", $field->stringify (-format => 'mail-cpim'), "\n";
754     ## RFC 2822: Mon, 04 Feb 2002 00:12:33 -0600
755     ## HTTP preferred: Mon, 04 Feb 2002 06:12:33 GMT
756     ## ISO 8601: 2002-02-04T00:12:33-06:00
757    
758     =head1 LICENSE
759    
760     Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
761    
762     This program is free software; you can redistribute it and/or modify
763     it under the terms of the GNU General Public License as published by
764     the Free Software Foundation; either version 2 of the License, or
765     (at your option) any later version.
766    
767     This program is distributed in the hope that it will be useful,
768     but WITHOUT ANY WARRANTY; without even the implied warranty of
769     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
770     GNU General Public License for more details.
771    
772     You should have received a copy of the GNU General Public License
773     along with this program; see the file COPYING. If not, write to
774     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
775     Boston, MA 02111-1307, USA.
776    
777     =head1 CHANGE
778    
779     See F<ChangeLog>.
780 wakaba 1.19 $Date: 2002/08/29 12:14:37 $
781 wakaba 1.17
782     =cut
783    
784     1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24