/[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.13 - (hide annotations) (download)
Sat Jul 6 10:30:43 2002 UTC (22 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.12: +30 -5 lines
2002-06-29  Wakaba <w@suika.fam.cx>

	* ContentType.pm, Params.pm, ValueParams.pm,
	XMoe.pm: Rewritten.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24