/[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.12 - (hide annotations) (download)
Sun Jun 23 12:10:16 2002 UTC (22 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.11: +13 -12 lines
2002-06-23  Wakaba <w@suika.fam.cx>

	* AngleQuoted.pm (%REG): Don't define regex locally.
	(Moved to Message::Util).
	* ContentType.pm, Date.pm, UA.pm,
	ValueParams.pm: Fix some codes not to be warned
	as 'Use of uninitialized value'.
	* Structured.pm 
	(header_default_charset, header_default_charset_input):
	New options.

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.12 $VERSION=do{my @r=(q$Revision: 1.11 $=~/\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     -zone => [+1, 0, 0],
116     -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    
251     my $format = $self->{option}->{format};
252     if ($format =~ /rfc2822/) {
253     $self->{option}->{use_military_zone} = 0;
254     }
255    
256     $self->_set_military_zone_name;
257     }
258    
259     =item $date = Message::Field::Date->new ([%options])
260 wakaba 1.1
261 wakaba 1.9 Constructs a new object. You might pass some options as parameters
262     to the constructor.
263 wakaba 1.1
264     =cut
265    
266 wakaba 1.9 ## Inherited
267 wakaba 1.1
268 wakaba 1.9 =item $date = Message::Field::Date->parse ($field-body, [%options])
269 wakaba 1.1
270 wakaba 1.9 Constructs a new object with given field body. You might pass
271     some options as parameters to the constructor.
272 wakaba 1.1
273     =cut
274    
275     sub parse ($$;%) {
276 wakaba 1.9 my $class = shift;
277     my $self = bless {}, $class;
278     my $body = shift;
279     $self->_init (@_);
280     ($body, @{$self->{comment}})
281     = $self->Message::Util::delete_comment_to_array ($body)
282     if $self->{option}->{use_comment};
283     $body =~ s/^$REG{WSP}+//; $body =~ s/$REG{WSP}+$//;
284     if ($self->{option}->{date_format} eq 'unix') {
285     $self->{date_time} = int ($body);
286     } elsif ($body =~ /^$REG{M_dt_rfc822}$/x) {
287 wakaba 1.1 my ($day, $month, $year, $hour, $minute, $second, $zone)
288     = ($1, uc $2, $3, $4, $5, $6, uc $7);
289     $month = $MONTH{$month} || 1;
290 wakaba 1.9 $year = $self->_obvious_year ($year) if length($year)<4;
291 wakaba 1.1 my ($zone_sign, $zone_hour, $zone_minute) = $self->_zone_string_to_array ($zone);
292     eval '$self->{date_time} = timegm_nocheck
293     ($second, $minute-($zone_sign*$zone_minute), $hour-($zone_sign*$zone_hour),
294     $day, $month-1, $year);';
295 wakaba 1.2 $self->{secfrac} = '';
296     $self->{option}->{zone} = [$zone_sign, $zone_hour, $zone_minute];
297 wakaba 1.9 } elsif ($body =~ /^$REG{M_dt_iso8601}$/x) {
298     my ($year,$month,$day,$hour,$minute,$second,$secfrac,$zone)
299     = ($1, $2, $3, $4, $5, $6, $7, $8);
300     my ($zone_sign, $zone_hour, $zone_minute) = $self->_zone_string_to_array ($zone);
301 wakaba 1.2 eval '$self->{date_time} = timegm_nocheck
302     ($second, $minute-($zone_sign*$zone_minute), $hour-($zone_sign*$zone_hour),
303     $day, $month-1, $year);';
304     $self->{secfrac} = $secfrac;
305 wakaba 1.1 $self->{option}->{zone} = [$zone_sign, $zone_hour, $zone_minute];
306 wakaba 1.9 } elsif ($body =~ /^$REG{M_dt_rfc733}$/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_rfc724}$/x) {
318 wakaba 1.2 my ($month, $day, $year, $hour, $minute, $second, $zone)
319     = ($1, $2, $3, $4, $5, $6, uc $7);
320 wakaba 1.9 $year = $self->_obvious_year ($year) if length($year)<4;
321 wakaba 1.2 my ($zone_sign, $zone_hour, $zone_minute) = $self->_zone_string_to_array ($zone);
322     eval '$self->{date_time} = timegm_nocheck
323     ($second, $minute-($zone_sign*$zone_minute), $hour-($zone_sign*$zone_hour),
324     $day, $month-1, $year);';
325     $self->{secfrac} = '';
326 wakaba 1.1 $self->{option}->{zone} = [$zone_sign, $zone_hour, $zone_minute];
327 wakaba 1.9 #} elsif ($body =~ /^[0-9]+$/) {
328     # $self->{date_time} = $&;
329 wakaba 1.1 } else {
330 wakaba 1.9 ## From HTTP::Date (revision 1.40) by Gisle Aas
331     #$body =~ s/^\s+//; # kill leading space
332     $body =~ s/^(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)[a-z]*,?\s*//i; # Useless weekday
333     my ($day, $month, $year, $hour, $minute, $second);
334     my ($secfrac, $zone, $ampm) = ('', $self->{option}->{zone_default_string});
335    
336     # Then we are able to check for most of the formats with this regexp
337     (($day,$month,$year,$hour,$minute,$second,$zone) =
338     $body =~ m"^
339     (\d\d?) # day
340     (?:\s+|[-\/])
341     (\w+) # month
342     (?:\s+|[-\/])
343     (\d+) # year
344     (?:
345     (?:\s+|:) # separator before clock
346     (\d\d?):(\d\d) # hour:min
347     (?::(\d\d))? # optional seconds
348     )? # optional clock
349     \s*
350     ([-+]?\d{2,4}|(?![APap][Mm]\b)[A-Za-z]+)? # timezone
351     $"x)
352    
353     ||
354    
355     # Try the ctime and asctime format
356     (($month, $day, $hour, $minute, $second, $zone, $year) =
357     $body =~ m"^
358     (\w{1,3}) # month
359     \s+
360     (\d\d?) # day
361     \s+
362     (\d\d?):(\d\d) # hour:min
363     (?::(\d\d))? # optional seconds
364     \s+
365     (?:([A-Za-z]+)\s+)? # optional timezone
366     (\d+) # year
367     $"x)
368    
369     ||
370    
371     # Then the Unix 'ls -l' date format
372     (($month, $day, $year, $hour, $minute, $second) =
373     $body =~ m"^
374     (\w{3}) # month
375     \s+
376     (\d\d?) # day
377     \s+
378     (?:
379     (\d\d\d\d) | # year
380     (\d{1,2}):(\d{2}) # hour:min
381     (?::(\d\d))? # optional seconds
382     )
383     $"x)
384    
385     ||
386    
387     # ISO 8601 format '1996-02-29 12:00:00 -0100' and variants
388     (($year, $month, $day, $hour, $minute, $second, $secfrac, $zone) =
389     $body =~ m"^
390     (\d{4}) # year
391     [-\/]?
392     (\d\d?) # numerical month
393     [-\/]?
394     (\d\d?) # day
395     (?:
396     (?:\s+|[-:Tt]) # separator before clock
397     (\d\d?):?(\d\d) # hour:min
398     (?:
399     :?
400     (\d\d)
401     (?:\.?(\d+))? ## optional second frac.
402     )? # optional seconds
403     )? # optional clock
404     \s*
405     ([-+]?\d\d?:?(:?\d\d)?
406     |Z|z)? # timezone (Z is 'zero meridian', i.e. GMT)
407    
408     $"x)
409    
410     ||
411    
412     # ISO 8601 like format '96-02-29 2:0:0 -0100' and variants
413     (($year, $month, $day, $hour, $minute, $second, $secfrac, $zone) =
414     $body =~ m"^
415     (\d+) # year
416     [-/]
417     (\d\d?) # numerical month
418     [-/]
419     (\d\d?) # day
420     (?:
421     (?:\s+|[-:Tt]) # separator before clock
422     (\d\d?):(\d+) # hour:min
423     (?:
424     :
425     (\d+)
426     (?:\.(\d+)) ## optional second frac.
427     )? # optional seconds
428     )? # optional clock
429     \s*
430     ([-+]?\d+(:\d+)?
431     |Z|z)? # timezone (Z is 'zero meridian', i.e. GMT)
432    
433     $"x)
434    
435     ||
436    
437     # Windows 'dir' 11-12-96 03:52PM
438     (($month, $day, $year, $hour, $minute, $ampm) =
439     $body =~ m"^
440     (\d{2}) # numerical month
441     -
442     (\d{2}) # day
443     -
444     (\d{2}) # year
445     \s+
446     (\d\d?):(\d\d)([APap][Mm]) # hour:min AM or PM
447     $"x)
448    
449     #||
450     #return; # unrecognized format
451     ;
452    
453     $day ||= 1;
454     # Translate month name to number
455 wakaba 1.12 $month ||= 1;
456 wakaba 1.9 $month = $MONTH{uc $month}
457 wakaba 1.12 || int ($month);
458 wakaba 1.9
459     # If the year is missing, we assume first date before the current,
460     # because of the formats we support such dates are mostly present
461     # on "ls -l" listings.
462     unless (defined $year) {
463     my $cur_mon;
464     ($cur_mon, $year) = (localtime)[4, 5];
465     $year += 1900; $cur_mon++;
466     $year-- if $month > $cur_mon;
467     } elsif (length($year) < 3) {
468     $year = $self->_obvious_year ($year);
469     }
470    
471     # Make sure clock elements are defined
472     $hour = 0 unless defined($hour);
473     $minute = 0 unless defined($minute);
474     $second = 0 unless defined($second);
475    
476     # Compensate for AM/PM
477     if ($ampm) {
478     $ampm = uc $ampm;
479     $hour = 0 if $hour == 12 && $ampm eq 'AM';
480     $hour += 12 if $ampm eq 'PM' && $hour != 12;
481     }
482    
483     my ($zone_sign, $zone_hour, $zone_minute) = $self->_zone_string_to_array ($zone);
484     eval '$self->{date_time} = timegm_nocheck
485     ($second, $minute-($zone_sign*$zone_minute), $hour-($zone_sign*$zone_hour),
486     $day, $month-1, $year);';
487     $self->{secfrac} = $secfrac;
488     $self->{option}->{zone} = [$zone_sign, $zone_hour, $zone_minute];
489 wakaba 1.1 }
490     $self;
491     }
492    
493 wakaba 1.10 sub zone ($;$) {
494     my $self = shift;
495     my $newzone = shift;
496 wakaba 1.12 if (ref $newzone) {
497     $self->{option}->{zone} = $newzone;
498     } elsif (length $newzone) {
499 wakaba 1.10 $self->{option}->{zone} = [$self->_zone_string_to_array ($newzone)];
500     }
501     $self->{option}->{zone};
502     }
503    
504 wakaba 1.9 ## Find "obvious" year
505     sub _obvious_year ($$) {
506     my $self = shift;
507     my $year = shift;
508     if ($self->{option}->{format} =~ /mail|news/) {
509     ## RFC 2822
510     if ( 0 <=$year && $year < 50) {$year += 2000}
511     elsif (50 < $year && $year < 1000) {$year += 1900}
512     } else {
513     ## RFC 2616
514     my $cur_yr = (localtime)[5] + 1900;
515     my $m = $cur_yr % 100;
516     my $tmp = $year;
517     $year += $cur_yr - $m;
518     $m -= $tmp;
519     $year += ($m > 0) ? 100 : -100 if abs($m) > 50;
520     }
521     $year;
522     }
523    
524     =back
525    
526     =head1 METHODS
527    
528     =over 4
529    
530    
531 wakaba 1.4 =head2 $self->unix_time ([$new_time])
532    
533     Returns or set the unix-time (seconds from the Epoch).
534    
535     =cut
536    
537     sub unix_time ($;$) {
538     my $self = shift;
539     my $new_time = shift;
540     if (defined $new_time) {
541 wakaba 1.6 $self->{date_time} = $new_time + 0;
542 wakaba 1.4 }
543     $self->{date_time};
544     }
545    
546 wakaba 1.10 sub set_datetime ($@) {
547     my $self = shift;
548     my ($year,$month,$day,$hour,$minute,$second,%misc) = @_;
549     my ($zone_sign, $zone_hour, $zone_minute)
550     = $self->_zone_string_to_array ($misc{zone});
551     eval '$self->{date_time} = timegm_nocheck
552     ($second, $minute-($zone_sign*$zone_minute), $hour-($zone_sign*$zone_hour),
553     $day, $month-1, $year);';
554     $self->{secfrac} = $misc{secfrac};
555     $self->{option}->{zone} = [$zone_sign, $zone_hour, $zone_minute];
556     }
557    
558 wakaba 1.2 =head2 $self->second_fraction ([$new_fraction])
559    
560     Returns or set the decimal fraction of a second.
561     Value is a string containing of only [0-9]
562     or empty string.
563    
564     =cut
565    
566     sub second_fraction ($;$) {
567     my $self = shift;
568     my $new_fraction = shift;
569     if (defined $new_fraction) {
570     $self->{secfrac} = $new_fraction unless $new_fraction =~ /[^0-9]/;
571     }
572     $self->{secfrac};
573     }
574    
575 wakaba 1.9 =item $field-body = $date->stringify ()
576 wakaba 1.1
577     Returns C<field-body> as a string.
578    
579     =cut
580    
581 wakaba 1.2 sub stringify ($;%) {
582 wakaba 1.1 my $self = shift;
583 wakaba 1.9 my %o = @_;
584     my %option = %{$self->{option}};
585     for (grep {/^-/} keys %o) {$option{substr ($_, 1)} = $o{$_}}
586     unless ($option{format_template}) {
587     if ($option{format} =~ /rfc2822|rfc1123|son-of-rfc1036|usefor|mime/) {
588     $option{format_template} = '%Wdy(local);, %DD(local); %Mon(local); %YYYY(local); %HH(local);:%TT(local);:%SS(local); %zsign;%zHH;%zTT;';
589     } elsif ($option{format} =~ /http/) {
590     $option{format_template} = '%Wdy;, %DD; %Mon; %YYYY; %HH;:%TT;:%SS; GMT';
591     } elsif ($option{format} =~ /rfc822|rfc1036/) {
592     $option{format_template} = '%Wdy(local);, %DD(local); %Mon(local); %YY(local); (%YYYY(local);) %HH(local);:%TT(local);:%SS(local); %zsign;%zHH;%zTT;';
593     } elsif ($option{format} =~ /rfc850/) {
594     $option{format_template} = '%Weekday;, %DD;-%Mon;-%YY; %HH;:%TT;:%SS; GMT';
595     } elsif ($option{format} =~ /asctime/) {
596     $option{format_template} = '%Wdy; %Mon; %DD(pad=>SP); %HH;:%MM;:%SS; %YYYY;';
597     #} elsif ($option{format} =~ /date\(1\)/) {
598     # $option{format_template} = '%Wdy; %Mon; %DD(pad=>SP); %HH;:%MM;:%SS; GMT %YYYY;';
599     } elsif ($option{format} =~ /un[i*]x/) { ## :-)
600     $option{format_template} = '%unix;';
601     } else { ## ISO 8601 (IETF)
602     $option{format_template} = '%YYYY(local);-%MM(local);-%DD(local);T%HH(local);:%TT(local);:%SS(local);%frac(prefix=>.);%zsign;%zHH;:%zTT;';
603     }
604 wakaba 1.2 }
605 wakaba 1.9 $self->_date2str (\%option)
606     . (($option{output_comment} && @{$self->{comment}} > 0)?
607     ' ' . $self->_comment_stringify: '');
608     }
609     *as_string = \&stringify;
610     *as_plain_string = \&stringify;
611 wakaba 1.10 ## You should use stringify instead of as_rfc2822_time
612     sub as_rfc2822_time ($@) {
613     shift->stringify (-format => 'mail-rfc2822', @_);
614     }
615 wakaba 1.9
616 wakaba 1.12 sub _date2str ($\%\%) {
617 wakaba 1.9 my $self = shift;
618     my $option = shift;
619 wakaba 1.12 my $gparam = shift;
620 wakaba 1.11 my $template = $option->{format_template};
621     my $time = $self->{date_time};
622     $time = $option->{date_time} if defined $option->{date_time};
623     my $zone = $option->{zone};
624 wakaba 1.12 if (ref $zone) { }
625 wakaba 1.9 elsif (length $zone) {$zone = [$self->_zone_string_to_array ($zone)]}
626     my $l_time = $time + $zone->[0] * ($zone->[1] * 60 + $zone->[2]) * 60;
627     my %time = (unix => $time,
628     tm => [gmtime ($time)],
629     l_unix => $l_time,
630     l_tm => [gmtime ($l_time)],
631     zone => $zone);
632     $template =~ s{%([A-Za-z0-9_]+)(?:\(([A-Za-z0-9,.:\x09\x20=>_-]*)\))?;}{
633     my ($f, $a) = ($1, $2);
634 wakaba 1.11 my $function = $option->{fmt2str}->{$f};
635 wakaba 1.9 if (ref $function) {
636     my %a;
637     for (split /[\x09\x20]*,[\x09\x20]*/, $a) {
638     if (/^([^=]+)=>(.+)$/) {$a{$1} = $2}
639     else {$a{$_} = 1}
640     }
641     $a{_prefix} = $a{local}? 'l_': '';
642     $a{_fmt} = $a{pad} eq 'SP'? '%2d':
643     $a{pad} eq 'none'? '%d':
644     '%02d';
645 wakaba 1.12 my $r = &$function ($self, \%time, \%a, $gparam);
646 wakaba 1.9 length $r? $a{prefix}.$r.$a{suffix}: '';
647     } elsif (length $function) {
648     $function;
649     } else {
650     "[$f: undef]";
651     }
652     }gex;
653     $template;
654 wakaba 1.1 }
655    
656 wakaba 1.2
657 wakaba 1.1
658     sub _zone_string_to_array ($$;$) {
659     my $self = shift;
660     my $zone = shift;
661 wakaba 1.12 return (+1, 0, 0) unless defined $zone;
662 wakaba 1.1 my $format = shift;
663     my @azone = [+1, 0, 0];
664 wakaba 1.9 $zone =~ tr/\x09\x20//d;
665     if ($zone =~ /([+-])([0-9][0-9])([0-9][0-9])/) {
666     @azone = ("${1}1", $2, $3);
667     } elsif ($zone =~ /([+-]?)([0-9]+)(?:[:.-]([0-9]+))?/) {
668 wakaba 1.1 @azone = ("${1}1", $2, $3);
669     } else { $zone =~ tr/-//d;
670     if (ref $ZONE{$zone}) {@azone = @{$ZONE{$zone}}}
671     elsif ($zone) {@azone = (-1, 0, 0)}
672     }
673     # }
674     @azone;
675     }
676    
677 wakaba 1.9 =item $option-value = $date->option ($option-name)
678    
679     Gets option value.
680    
681     =item $date->option ($option-name, $option-value, ...)
682    
683     Set option value(s). You can pass multiple option name-value pair
684     as parameter when setting.
685    
686     =item $clone = $date->clone ()
687    
688     Returns a copy of the object.
689    
690     =cut
691    
692     ## option, clone, method_available: Inherited
693    
694 wakaba 1.1 =head1 EXAMPLE
695    
696 wakaba 1.2 use Message::Field::Date;
697 wakaba 1.1
698 wakaba 1.9 print Message::Field::Date->new (unix => time,
699     -zone => '+0900'),"\n"; ## Thu, 16 May 2002 17:53:44 +0900
700     print Message::Field::Date->new (unix => time,
701     -format_template => ## Century: 21, Year: 02, Month: 05
702     'Century: %CC;, Year: %YY;, Month: %MM;'),"\n";
703    
704 wakaba 1.2 my $field_body = '04 Feb 2002 00:12:33 CST';
705     my $field = Message::Field::Date->parse ($field_body);
706 wakaba 1.1
707 wakaba 1.9 print "RFC 2822:\t", $field->stringify (-format => 'mail-rfc2822'), "\n";
708     print "HTTP preferred:\t", $field->stringify (-format => 'http-1.1'), "\n";
709     print "ISO 8601:\t", $field->stringify (-format => 'mail-cpim'), "\n";
710     ## RFC 2822: Mon, 04 Feb 2002 00:12:33 -0600
711     ## HTTP preferred: Mon, 04 Feb 2002 06:12:33 GMT
712     ## ISO 8601: 2002-02-04T00:12:33-06:00
713 wakaba 1.1
714     =head1 LICENSE
715    
716     Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
717    
718     This program is free software; you can redistribute it and/or modify
719     it under the terms of the GNU General Public License as published by
720     the Free Software Foundation; either version 2 of the License, or
721     (at your option) any later version.
722    
723     This program is distributed in the hope that it will be useful,
724     but WITHOUT ANY WARRANTY; without even the implied warranty of
725     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
726     GNU General Public License for more details.
727    
728     You should have received a copy of the GNU General Public License
729     along with this program; see the file COPYING. If not, write to
730     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
731     Boston, MA 02111-1307, USA.
732    
733     =head1 CHANGE
734    
735     See F<ChangeLog>.
736 wakaba 1.12 $Date: 2002/06/16 10:42:06 $
737 wakaba 1.1
738     =cut
739    
740     1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24