1 package DateTime::Format::W3CDTF;
5 use vars qw ($VERSION);
14 return bless {}, $class;
17 # key is string length
20 params => [qw( year month day hour minute second)],
21 regex => qr/^(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)$/,
25 params => [qw( year month day hour minute)],
26 regex => qr/^(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d)$/,
27 zero => { second => 0 },
30 params => [qw( year month day )],
31 regex => qr/^(\d{4})-(\d\d)-(\d\d)$/,
32 zero => { hour => 0, minute => 0, second => 0 },
35 params => [qw( year month )],
36 regex => qr/^(\d{4})-(\d\d)$/,
37 zero => { day => 1, hour => 0, minute => 0, second => 0 },
40 params => [qw( year )],
41 regex => qr/^(\d\d\d\d)$/,
42 zero => { month => 1, day => 1, hour => 0, minute => 0, second => 0 }
47 my ( $self, $date ) = @_;
49 # save for error messages
53 if ( $date =~ s/([+-]\d\d:\d\d)$// ) {
58 elsif ( $date =~ s/Z$// ) {
59 $p{time_zone} = 'UTC';
62 $p{time_zone} = 'floating';
65 my $format = $valid_formats{ length $date }
66 or die "Invalid W3CDTF datetime string ($original)";
68 @p{ @{ $format->{params} } } = $date =~ /$format->{regex}/;
70 return DateTime->new( %p, %{ $format->{zero} } );
74 my ( $self, $dt ) = @_;
76 # removed in 0.4 as it behaved improperly at midnight - kellan 2003/11/23
78 # ( $dt->hour || $dt->min || $dt->sec ?
79 # sprintf( '%04d-%02d-%02dT%02d:%02d:%02d',
80 # $dt->year, $dt->month, $dt->day,
81 # $dt->hour, $dt->minute, $dt->second ) :
82 # sprintf( '%04d-%02d-%02d', $dt->year, $dt->month, $dt->day )
86 '%04d-%02d-%02dT%02d:%02d:%02d',
87 $dt->year, $dt->month, $dt->day,
88 $dt->hour, $dt->minute, $dt->second
91 my $tz = $dt->time_zone;
93 return $base if $tz->is_floating;
95 return $base . 'Z' if $tz->is_utc;
97 my $offset = $dt->offset();
99 return $base unless defined $offset;
101 return $base . offset_as_string($offset)
105 my ( $self, $dt ) = @_;
107 my $base = sprintf( '%04d-%02d-%02d', $dt->year, $dt->month, $dt->day );
111 # minor offset_as_string variant w/ :
113 sub offset_as_string {
116 return undef unless defined $offset;
118 my $sign = $offset < 0 ? '-' : '+';
120 my $hours = $offset / ( 60 * 60 );
121 $hours = abs($hours) % 24;
123 my $mins = ( $offset % ( 60 * 60 ) ) / 60;
125 my $secs = $offset % 60;
129 ? sprintf( '%s%02d:%02d:%02d', $sign, $hours, $mins, $secs )
130 : sprintf( '%s%02d:%02d', $sign, $hours, $mins )
140 DateTime::Format::W3CDTF - Parse and format W3CDTF datetime strings
144 use DateTime::Format::W3CDTF;
146 my $w3c = DateTime::Format::W3CDTF->new;
147 my $dt = $w3c->parse_datetime( '2003-02-15T13:50:05-05:00' );
149 # 2003-02-15T13:50:05-05:00
150 $w3c->format_datetime($dt);
154 This module understands the W3CDTF date/time format, an ISO 8601 profile,
155 defined at http://www.w3.org/TR/NOTE-datetime. This format as the native
156 date format of RSS 1.0.
158 It can be used to parse these formats in order to create the appropriate
163 This API is currently experimental and may change in the future.
167 =item * parse_datetime($string)
169 Given a W3CDTF datetime string, this method will return a new
172 If given an improperly formatted string, this method may die.
174 =item * format_datetime($datetime)
176 Given a C<DateTime> object, this methods returns a W3CDTF datetime
179 NOTE: As of version 0.4, format_datetime no longer attempts to truncate
180 datetimes without a time component. This is due to the fact that C<DateTime>
181 doesn't distinguish between a date with no time component, and midnight.
183 =item * format_date($datetime)
185 Given a C<DateTime> object, return a W3CDTF datetime string without the time component.
191 Support for this module is provided via the datetime@perl.org email
192 list. See http://datetime.perl.org/?MailingList for details.
194 Please submit bugs to the CPAN RT system at
195 http://rt.cpan.org/NoAuth/ReportBug.html?Queue=datetime-format-w3cdtf or via
196 email at bug-datetime-format-w3cdtf@rt.cpan.org.
200 Dave Rolsky E<lt>autarch@urth.orgE<gt>
204 This module was originally created by Kellan Elliott-McCrea
205 E<lt>kellan@protest.netE<gt>.
207 This module was inspired by L<DateTime::Format::ICal>
211 Copyright (c) 2009 David Rolsky. All rights reserved. This
212 program is free software; you can redistribute it and/or modify it
213 under the same terms as Perl itself.
215 Copyright (c) 2003 Kellan Elliott-McCrea
217 Portions of the code in this distribution are derived from other
218 works. Please see the CREDITS file for more details.
220 The full text of the license can be found in the LICENSE file included
225 datetime@perl.org mailing list
227 http://datetime.perl.org/