/[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.18 - (hide annotations) (download)
Thu Aug 29 12:14:37 2002 UTC (22 years, 3 months ago) by wakaba
Branch: MAIN
Changes since 1.17: +8 -47 lines
2002-08-29  Wakaba <w@suika.fam.cx>

	* Date.pm:
	- (%MONTH): Japanese month names are removed.  (I wish
	those will be reintroduced in future version of this module.)
	- (stringify): New options: '-format_macros', '-format_parameters'.

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.18 $VERSION=do{my @r=(q$Revision: 1.17 $=~/\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     APR => 4, APRIL => 4,
97     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     ECT => [+1, 1, 0], ## Central European (French)
179     EDT => [-1, 4, 0], ## (NA)Eastern Daylight 733, 822
180     EEST => [+1, 3, 0], ## Eastern European Summer
181     EET => [+1, 2, 0], ## Eastern Europe 1947
182     EST => [-1, 5, 0], ## (NA)Eastern Standard 733, 822
183     EWT => [-1, 4, 0], ## U.S. Eastern War Time
184     FST => [+1, 2, 0], ## French Summer
185     FWT => [+1, 1, 0], ## French Winter
186     GDT => [+1, 1, 0], ## 724
187     GMT => [+1, 0, 0], ## Greenwich Mean 733, 822
188     #GST => [-1, 3, 0], ## Greenland Standard
189     GST => [+1, 10, 0], ## Guam Standard
190     HDT => [-1, 9, 0], ## Hawaii Daylight 733
191     HKT => [+1, 8, 0], ## Hong Kong
192     HST => [-1, 10, 0], ## Hawaii Standard 733
193     IDLE => [+1, 12, 0], ## International Date Line East
194     IDLW => [-1, 12, 0], ## International Date Line West
195     IDT => [+1, 3, 0],
196     IST => [+1, 2, 0], ## Israel standard
197     #IST => [+1, 5, 30], ## Indian standard
198     IT => [+1, 3, 30], ## Iran
199     JST => [+1, 9, 0], ## Japan Central Standard
200 wakaba 1.18 #JT => [+1, 9, 0], ## Japan Central Standard
201 wakaba 1.17 JT => [+1, 7, 30], ## Java
202     KDT => [+1, 10, 0], ## Korean Daylight
203     KST => [+1, 9, 0], ## Korean Standard
204     LCL => [-1, 0, 0], ## (unknown zone used by LSMTP)
205     LOCAL => [-1, 0, 0], ## local time zone
206     LT => [-1, 0, 0], ## Luna Time [RFC 1607]
207     MDT => [-1, 6, 0], ## (NA)Mountain Daylight 733, 822
208     MET => [+1, 0, 0], ## Middle European
209     METDST => [+1, 2, 0],
210     MEST => [+1, 2, 0], ## Middle European Summer
211     MEWT => [+1, 0, 0], ## Middle European Winter
212     MEZ => [+1, 0, 0], ## Central European (German)
213     MST => [-1, 7, 0], ## (NA)Mountain Standard 733, 822
214     MT => [-1, 0, 0], ## Mars Time [RFC 1607]
215     NDT => [-1, 2, 30], ## Newfoundland Daylight
216     NFT => [-1, 3, 30], ## Newfoundland Standard
217     NST => [-1, 3, 30], ## Newfoundland Standard 733
218     #NST => [-1, 6, 30], ## North Sumatra
219     NT => [-1, 11, 0], ## Nome
220     NZD => [+1, 13, 0], ## New Zealand Daylight
221     NZT => [+1, 12, 0], ## New Zealand
222     NZDT => [+1, 13, 0], ## New Zealand Daylight
223     NZST => [+1, 12, 0], ## New Zealand Standard
224     PDT => [-1, 7, 0], ## (NA)Pacific Daylight 733, 822
225     PST => [-1, 8, 0], ## (NA)Pacific Standard 733, 822
226     SET => [+1, 1, 0], ## Seychelles
227     SST => [+1, 2, 0], ## Swedish Summer
228     #SST => [+1, 7, 0], ## South Sumatra
229     SWT => [+1, 1, 0], ## Swedish Winter
230     UKR => [+1, 2, 0], ## Ukraine
231     UT => [+1, 0, 0], ## Universal Time 822
232     UTC => [+1, 0, 0], ## Coordinated Universal Time
233     WADT => [+1, 8, 0], ## West Australian Daylight
234     WAT => [-1, 0, 0], ## West Africa
235     WET => [+1, 0, 0], ## Western European
236     WST => [+1, 8, 0], ## West Australian Standard
237     YDT => [-1, 8, 0], ## Yukon Daylight 733
238     YST => [-1, 9, 0], ## Yukon Standard 733
239     Z => [+1, 0, 0], ## 822, ISO 8601
240     ZP4 => [+1, 4, 0], ## Z+4
241     ZP5 => [+1, 5, 0], ## Z+5
242     ZP6 => [+1, 6, 0], ## Z+6
243     );
244    
245     ## -use_military_zone => +1 / -1 / 0
246     ## Whether military zone names are understood or not.
247     ## +1 Admits them and treats as standard value. (eg. "A" = +0100)
248     ## -1 Admits them but treats as negative value. (eg. "A" = -0100)
249     ## 0 They are ignored and zone is set as -0000. (eg. "A" = -0000)
250     ## Because of typo in BNF comment of RFCs 733 and 822,
251     ## quite a few implemention use these values incorrectly.
252     ## As a result, these zone names carry no worthful information.
253     ## RFC 2822 recommends these names be taken as '-0000' (i.e.
254     ## unknown zone).
255    
256     sub _set_military_zone_name ($) {
257     my $self = shift;
258     my $mode = $self->{option}->{use_military_zone};
259     my $i = 0;
260     if ($mode == 0) {
261     for my $letter ('A'..'Y') {$ZONE{$letter} = [-1, 0, 0]} return;
262     }
263     for my $letter ('Z', 'A'..'I', 'K'..'M') {
264     $ZONE{$letter} = [+1*$mode, $i++, 0];
265     } $i = 1;
266     for my $letter ('N'..'Y') {
267     $ZONE{$letter} = [-1*$mode, $i++, 0];
268     }
269     }
270    
271     sub _init ($;%) {
272     my $self = shift;
273     my $DEFAULT = Message::Util::make_clone (\%DEFAULT);
274     my %option = @_;
275     $self->SUPER::_init (%$DEFAULT, %option);
276 wakaba 1.18 $self->{date_time} = 0;
277 wakaba 1.17 $self->{date_time} = $option{unix} if defined $option{unix};
278     $self->{secfrac} = $option{frac} if defined $option{frac};
279     unless (ref $self->{option}->{zone}) {
280     my $zone = $self->{option}->{zone} || ''; #$main::ENV{TZ} || '';
281     ## Since _zone_string_to_array does not provide full support
282     ## of parsing TZ format (eg. daytime), not seeing it might be
283     ## better way.
284     if (length $zone) {
285     $self->{option}->{zone} = [ $self->_zone_string_to_array ($zone) ];
286     } else {
287     my $time = time;
288     my $ltime = timegm_nocheck (localtime ($time));
289     my $o = int ( ($ltime - $time) / 60);
290     my @zone;
291     $zone[2] = $o % 60; $o = int ( $o / 60 );
292     $zone[1] = $o % 24;
293     $zone[0] = $o >= 0 ? +1 : -1;
294     $self->{option}->{zone} = \@zone;
295     }
296     }
297    
298     my $format = $self->{option}->{format};
299     if ($format =~ /mail-rfc2822/) {
300     $self->{option}->{use_military_zone} = 0;
301     }
302    
303     $self->_set_military_zone_name;
304     }
305    
306     =item $date = Message::Field::Date->new ([%options])
307    
308     Constructs a new object. You might pass some options as parameters
309     to the constructor.
310    
311     =cut
312    
313     ## Inherited
314    
315     =item $date = Message::Field::Date->parse ($field-body, [%options])
316    
317     Constructs a new object with given field body. You might pass
318     some options as parameters to the constructor.
319    
320     =cut
321    
322     sub parse ($$;%) {
323     my $class = shift;
324     my $self = bless {}, $class;
325     my $body = shift;
326     $self->_init (@_);
327     ($body, @{$self->{comment}})
328     = $self->Message::Util::delete_comment_to_array ($body)
329     if $self->{option}->{use_comment};
330     $body =~ s/^$REG{WSP}+//; $body =~ s/$REG{WSP}+$//;
331     if ($self->{option}->{date_format} eq 'unix') {
332     $self->{date_time} = int ($body);
333     } elsif (!$body) {
334     return $self;
335     } elsif ($body =~ /^$REG{M_dt_rfc822}$/x) {
336     my ($day, $month, $year, $hour, $minute, $second, $zone)
337     = ($1, uc $2, $3, $4, $5, $6, uc $7);
338     $month = $MONTH{$month} || 1;
339     $year = $self->_obvious_year ($year) if length($year)<4;
340     my ($zone_sign, $zone_hour, $zone_minute) = $self->_zone_string_to_array ($zone);
341     eval '$self->{date_time} = timegm_nocheck
342     ($second, $minute-($zone_sign*$zone_minute), $hour-($zone_sign*$zone_hour),
343     $day, $month-1, $year);';
344     $self->{secfrac} = '';
345     $self->{option}->{zone} = [$zone_sign, $zone_hour, $zone_minute];
346     } elsif ($body =~ /^$REG{M_dt_rfc3339}$/x) {
347     my ($year,$month,$day,$hour,$minute,$second,$secfrac,$zone)
348     = ($1, $2, $3, $4, $5, $6, $7, $8);
349     my ($zone_sign, $zone_hour, $zone_minute) = $self->_zone_string_to_array ($zone);
350     eval '$self->{date_time} = timegm_nocheck
351     ($second, $minute-($zone_sign*$zone_minute), $hour-($zone_sign*$zone_hour),
352     $day, $month-1, $year);';
353     $self->{secfrac} = $secfrac;
354     $self->{option}->{zone} = [$zone_sign, $zone_hour, $zone_minute];
355     } elsif ($body =~ /^$REG{M_dt_rfc733}$/x) {
356     my ($day, $month, $year, $hour, $minute, $second, $zone)
357     = ($1, uc $2, $3, $4, $5, $6, uc $7);
358     $month = $MONTH{$month} || 1;
359     $year = $self->_obvious_year ($year) if length($year)<4;
360     my ($zone_sign, $zone_hour, $zone_minute) = $self->_zone_string_to_array ($zone);
361     eval '$self->{date_time} = timegm_nocheck
362     ($second, $minute-($zone_sign*$zone_minute), $hour-($zone_sign*$zone_hour),
363     $day, $month-1, $year);';
364     $self->{secfrac} = '';
365     $self->{option}->{zone} = [$zone_sign, $zone_hour, $zone_minute];
366     } elsif ($body =~ /^$REG{M_dt_rfc724}$/x) {
367     my ($month, $day, $year, $hour, $minute, $second, $zone)
368     = ($1, $2, $3, $4, $5, $6, uc $7);
369     $year = $self->_obvious_year ($year) if length($year)<4;
370     my ($zone_sign, $zone_hour, $zone_minute) = $self->_zone_string_to_array ($zone);
371     eval '$self->{date_time} = timegm_nocheck
372     ($second, $minute-($zone_sign*$zone_minute), $hour-($zone_sign*$zone_hour),
373     $day, $month-1, $year);';
374     $self->{secfrac} = '';
375     $self->{option}->{zone} = [$zone_sign, $zone_hour, $zone_minute];
376     #} elsif ($body =~ /^[0-9]+$/) {
377     # $self->{date_time} = $&;
378     } else {
379     ## From HTTP::Date (revision 1.40) by Gisle Aas
380     #$body =~ s/^\s+//; # kill leading space
381     $body =~ s/^(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)[a-z]*,?\s*//i; # Useless weekday
382     my ($day, $month, $year, $hour, $minute, $second);
383     my ($secfrac, $zone, $ampm) = ('', $self->{option}->{zone_default_string});
384    
385     # Then we are able to check for most of the formats with this regexp
386     (($day,$month,$year,$hour,$minute,$second,$zone) =
387     $body =~ m"^
388     (\d\d?) # day
389     (?:\s+|[-\/])
390     (\w+) # month
391     (?:\s+|[-\/])
392     (\d+) # year
393     (?:
394     (?:\s+|:) # separator before clock
395     (\d\d?):(\d\d) # hour:min
396     (?::(\d\d))? # optional seconds
397     )? # optional clock
398     \s*
399     ([-+]?\d{2,4}|(?![APap][Mm]\b)[A-Za-z]+)? # timezone
400     $"x)
401    
402     ||
403    
404     # Try the ctime and asctime format
405     (($month, $day, $hour, $minute, $second, $zone, $year) =
406     $body =~ m"^
407     (\w{1,3}) # month
408     \s+
409     (\d\d?) # day
410     \s+
411     (\d\d?):(\d\d) # hour:min
412     (?::(\d\d))? # optional seconds
413     \s+
414     (?:([A-Za-z]+)\s+)? # optional timezone
415     (\d+) # year
416     $"x)
417    
418     ||
419    
420     # Then the Unix 'ls -l' date format
421     (($month, $day, $year, $hour, $minute, $second) =
422     $body =~ m"^
423     (\w{3}) # month
424     \s+
425     (\d\d?) # day
426     \s+
427     (?:
428     (\d\d\d\d) | # year
429     (\d{1,2}):(\d{2}) # hour:min
430     (?::(\d\d))? # optional seconds
431     )
432     $"x)
433    
434     ||
435    
436     # ISO 8601 format '1996-02-29 12:00:00 -0100' and variants
437     (($year, $month, $day, $hour, $minute, $second, $secfrac, $zone) =
438     $body =~ m"^
439     (\d{4}) # year
440     [-\/]?
441     (\d\d?) # numerical month
442     [-\/]?
443     (\d\d?) # day
444     (?:
445     (?:\s+|[-:Tt]) # separator before clock
446     (\d\d?):?(\d\d) # hour:min
447     (?:
448     :?
449     (\d\d)
450     (?:\.?(\d+))? ## optional second frac.
451     )? # optional seconds
452     )? # optional clock
453     \s*
454     ([-+]?\d\d?:?(:?\d\d)?
455     |Z|z)? # timezone (Z is 'zero meridian', i.e. GMT)
456    
457     $"x)
458    
459     ||
460    
461     # ISO 8601 like format '96-02-29 2:0:0 -0100' and variants
462     (($year, $month, $day, $hour, $minute, $second, $secfrac, $zone) =
463     $body =~ m"^
464     (\d+) # year
465     [-/]
466     (\d\d?) # numerical month
467     [-/]
468     (\d\d?) # day
469     (?:
470     (?:\s+|[-:Tt]) # separator before clock
471     (\d\d?):(\d+) # hour:min
472     (?:
473     :
474     (\d+)
475     (?:\.(\d+)) ## optional second frac.
476     )? # optional seconds
477     )? # optional clock
478     \s*
479     ([-+]?\d+(:\d+)?
480     |Z|z)? # timezone (Z is 'zero meridian', i.e. GMT)
481    
482     $"x)
483    
484     ||
485    
486     # Windows 'dir' 11-12-96 03:52PM
487     (($month, $day, $year, $hour, $minute, $ampm) =
488     $body =~ m"^
489     (\d{2}) # numerical month
490     -
491     (\d{2}) # day
492     -
493     (\d{2}) # year
494     \s+
495     (\d\d?):(\d\d)([APap][Mm]) # hour:min AM or PM
496     $"x)
497    
498     #||
499     #return; # unrecognized format
500     ;
501    
502     $day ||= 1;
503     # Translate month name to number
504     $month ||= 1;
505     $month = $MONTH{uc $month}
506     || int ($month);
507    
508     # If the year is missing, we assume first date before the current,
509     # because of the formats we support such dates are mostly present
510     # on "ls -l" listings.
511     unless (defined $year) {
512     my $cur_mon;
513     ($cur_mon, $year) = (localtime)[4, 5];
514     $year += 1900; $cur_mon++;
515     $year-- if $month > $cur_mon;
516     } elsif (length($year) < 3) {
517     $year = $self->_obvious_year ($year);
518     }
519    
520     # Make sure clock elements are defined
521     $hour = 0 unless defined($hour);
522     $minute = 0 unless defined($minute);
523     $second = 0 unless defined($second);
524    
525     # Compensate for AM/PM
526     if ($ampm) {
527     $ampm = uc $ampm;
528     $hour = 0 if $hour == 12 && $ampm eq 'AM';
529     $hour += 12 if $ampm eq 'PM' && $hour != 12;
530     }
531    
532     my ($zone_sign, $zone_hour, $zone_minute) = $self->_zone_string_to_array ($zone);
533     eval '$self->{date_time} = timegm_nocheck
534     ($second, $minute-($zone_sign*$zone_minute), $hour-($zone_sign*$zone_hour),
535     $day, $month-1, $year);';
536     $self->{secfrac} = $secfrac;
537     $self->{option}->{zone} = [$zone_sign, $zone_hour, $zone_minute];
538     }
539     $self;
540     }
541    
542     sub zone ($;$) {
543     my $self = shift;
544     my $newzone = shift;
545     if (ref $newzone) {
546     $self->{option}->{zone} = $newzone;
547     } elsif (length $newzone) {
548     $self->{option}->{zone} = [$self->_zone_string_to_array ($newzone)];
549     }
550     $self->{option}->{zone};
551     }
552    
553     ## Find "obvious" year
554     sub _obvious_year ($$) {
555     my $self = shift;
556     my $year = shift;
557     if ($self->{option}->{format} =~ /mail|news/) {
558     ## RFC 2822
559     if ( 0 <=$year && $year < 50) {$year += 2000}
560     elsif (50 < $year && $year < 1000) {$year += 1900}
561     } else {
562     ## RFC 2616
563     my $cur_yr = (localtime)[5] + 1900;
564     my $m = $cur_yr % 100;
565     my $tmp = $year;
566     $year += $cur_yr - $m;
567     $m -= $tmp;
568     $year += ($m > 0) ? 100 : -100 if abs($m) > 50;
569     }
570     $year;
571     }
572    
573     =back
574    
575     =head1 METHODS
576    
577     =over 4
578    
579    
580     =head2 $self->unix_time ([$new_time])
581    
582     Returns or set the unix-time (seconds from the Epoch).
583    
584     =cut
585    
586     sub unix_time ($;$) {
587     my $self = shift;
588     my $new_time = shift;
589     if (defined $new_time) {
590     $self->{date_time} = $new_time + 0;
591     }
592     $self->{date_time};
593     }
594    
595     sub set_datetime ($@) {
596     my $self = shift;
597     my ($year,$month,$day,$hour,$minute,$second,%misc) = @_;
598     my ($zone_sign, $zone_hour, $zone_minute)
599     = $self->_zone_string_to_array ($misc{zone});
600     eval '$self->{date_time} = timegm_nocheck
601     ($second, $minute-($zone_sign*$zone_minute), $hour-($zone_sign*$zone_hour),
602     $day, $month-1, $year);';
603     $self->{secfrac} = $misc{secfrac};
604     $self->{option}->{zone} = [$zone_sign, $zone_hour, $zone_minute];
605     }
606    
607     =head2 $self->second_fraction ([$new_fraction])
608    
609     Returns or set the decimal fraction of a second.
610     Value is a string containing of only [0-9]
611     or empty string.
612    
613     Note that this implemention is temporary and in the near future
614     it can be changed.
615    
616     =cut
617    
618     sub second_fraction ($;$) {
619     my $self = shift;
620     my $new_fraction = shift;
621     if (defined $new_fraction) {
622     $self->{secfrac} = $new_fraction unless $new_fraction =~ /[^0-9]/;
623     }
624     $self->{secfrac};
625     }
626    
627     =item $field-body = $date->stringify ()
628    
629     Returns C<field-body> as a string.
630    
631     =cut
632    
633     sub stringify ($;%) {
634     my $self = shift;
635     my %o = @_;
636     my %option = %{$self->{option}};
637 wakaba 1.18 $option{format_parameters} ||= {};
638 wakaba 1.17 for (grep {/^-/} keys %o) {$option{substr ($_, 1)} = $o{$_}}
639     unless ($option{format_template}) {
640     if ($option{format} =~ /mail-rfc2822|news-usefor|mail-rfc822\+rfc1123|news-son-of-rfc1036|mime/) {
641     $option{format_template} = '%Wdy(local);, %DD(local); %Mon(local); %YYYY(local); %HH(local);:%TT(local);:%SS(local); %zsign;%zHH;%zTT;%comment(prefix=>" ");';
642     } elsif ($option{format} =~ /http/) {
643     $option{format_template} = '%Wdy;, %DD; %Mon; %YYYY; %HH;:%TT;:%SS; GMT';
644     } elsif ($option{format} =~ /mail-rfc822|news-rfc1036/) {
645     $option{format_template} = '%Wdy(local);, %DD(local); %Mon(local); %YY(local); (%YYYY(local);) %HH(local);:%TT(local);:%SS(local); %zsign;%zHH;%zTT;%comment(prefix=>" ");';
646     } elsif ($option{format} =~ /news-rfc850/) {
647     $option{format_template} = '%Weekday;, %DD;-%Mon;-%YY; %HH;:%TT;:%SS; GMT';
648     } elsif ($option{format} =~ /asctime/) {
649     $option{format_template} = '%Wdy; %Mon; %DD(pad=>SP); %HH;:%MM;:%SS; %YYYY;';
650     #} elsif ($option{format} =~ /date\(1\)/) {
651     # $option{format_template} = '%Wdy; %Mon; %DD(pad=>SP); %HH;:%MM;:%SS; GMT %YYYY;';
652     } elsif ($option{format} =~ /un[i*]x/) { ## ;-)
653     $option{format_template} = '%unix;';
654     } else { ## RFC 3339 (IETF's ISO 8601)
655     $option{format_template} = '%YYYY(local);-%MM(local);-%DD(local);T%HH(local);:%TT(local);:%SS(local);%frac(prefix=>.);%zsign;%zHH;:%zTT;%comment(prefix=>" ");';
656     }
657     }
658     my $zone = $option{zone};
659     $zone = [ $self->_zone_string_to_array ($zone) ] if not ref $zone && length $zone;
660     $zone = [+0, 0, 0] unless ref $zone;
661     my $l_time = $self->{date_time} + $zone->[0] * ($zone->[1] * 60 + $zone->[2]) * 60;
662     my $c = '';
663     $c = $self->_comment_stringify
664     if $option{output_comment} && @{ $self->{comment} } > 0;
665 wakaba 1.18 &Message::Util::sprintxf ($option{format_template}, {
666 wakaba 1.17 comment => $c,
667 wakaba 1.18 fmt2str => ($option{format_macros} || \%FMT2STR),
668 wakaba 1.17 option => \%option,
669     time => $self->{date_time},
670     time_local => $l_time,
671     tm => [ gmtime $self->{date_time} ],
672     tm_local => [ gmtime $l_time ],
673     secfrac => $self->{secfrac},
674     secfrac_local => $self->{secfrac}, ## Not supported yet
675 wakaba 1.18 %{ $option{format_parameters} },
676 wakaba 1.17 });
677     }
678     *as_string = \&stringify;
679     *as_plain_string = \&stringify;
680     ## You should use stringify instead of as_rfc2822_time
681     sub as_rfc2822_time ($@) {
682     shift->stringify (-format => 'mail-rfc2822', @_);
683     }
684    
685     sub _zone_string_to_array ($$;$) {
686     my $self = shift;
687     my $zone = shift;
688     return (+1, 0, 0) unless defined $zone;
689     my $format = shift;
690     my @azone = [+1, 0, 0];
691     $zone =~ tr/\x09\x20//d;
692     if ($zone =~ /^[^0-9+-]+(?:([+-]?)([0-9]{1,2}))(?::([0-9]{1,2}))?/) {
693     ## $ENV{TZ} format, but is not fully supported
694     my ($s, $h, $m) = ($1, $2, $3);
695     $s ||= '+'; $s =~ tr/+-/-+/;
696     @azone = ("${s}1", 0+$h, 0+$m);
697     } elsif ($zone =~ /([+-])([0-9][0-9])([0-9][0-9])/) {
698     @azone = ("${1}1", $2, $3);
699     } elsif ($zone =~ /([+-]?)([0-9]+)(?:[:.-]([0-9]+))?/) {
700     @azone = ("${1}1", $2, 0+$3);
701     } else { $zone =~ tr/-//d;
702     if (ref $ZONE{$zone}) {@azone = @{$ZONE{$zone}}}
703     elsif ($zone) {@azone = (-1, 0, 0)}
704     }
705     # }
706     @azone;
707     }
708    
709     =item $option-value = $date->option ($option-name)
710    
711     Gets option value.
712    
713     =item $date->option ($option-name, $option-value, ...)
714    
715     Set option value(s). You can pass multiple option name-value pair
716     as parameter when setting.
717    
718     =item $clone = $date->clone ()
719    
720     Returns a copy of the object.
721    
722     =cut
723    
724     ## option, clone, method_available: Inherited
725    
726     =head1 EXAMPLE
727    
728     use Message::Field::Date;
729    
730     print Message::Field::Date->new (unix => time,
731     -zone => '+0900'),"\n"; ## Thu, 16 May 2002 17:53:44 +0900
732     print Message::Field::Date->new (unix => time,
733     -format_template => ## Century: 21, Year: 02, Month: 05
734     'Century: %CC;, Year: %YY;, Month: %MM;'),"\n";
735    
736     my $field_body = '04 Feb 2002 00:12:33 CST';
737     my $field = Message::Field::Date->parse ($field_body);
738    
739     print "RFC 2822:\t", $field->stringify (-format => 'mail-rfc2822'), "\n";
740     print "HTTP preferred:\t", $field->stringify (-format => 'http-1.1'), "\n";
741     print "ISO 8601:\t", $field->stringify (-format => 'mail-cpim'), "\n";
742     ## RFC 2822: Mon, 04 Feb 2002 00:12:33 -0600
743     ## HTTP preferred: Mon, 04 Feb 2002 06:12:33 GMT
744     ## ISO 8601: 2002-02-04T00:12:33-06:00
745    
746     =head1 LICENSE
747    
748     Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
749    
750     This program is free software; you can redistribute it and/or modify
751     it under the terms of the GNU General Public License as published by
752     the Free Software Foundation; either version 2 of the License, or
753     (at your option) any later version.
754    
755     This program is distributed in the hope that it will be useful,
756     but WITHOUT ANY WARRANTY; without even the implied warranty of
757     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
758     GNU General Public License for more details.
759    
760     You should have received a copy of the GNU General Public License
761     along with this program; see the file COPYING. If not, write to
762     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
763     Boston, MA 02111-1307, USA.
764    
765     =head1 CHANGE
766    
767     See F<ChangeLog>.
768 wakaba 1.18 $Date: 2002/08/05 09:33:18 $
769 wakaba 1.17
770     =cut
771    
772     1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24