Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 37f5171e20da8105f78b929592ad916ec9363917 0 parents
@afiskon authored
3  .gitignore
@@ -0,0 +1,3 @@
+Makefile
+blib/*
+*.swp
8 META.info
@@ -0,0 +1,8 @@
+{
+ "name" : "DateTime::Format::W3CDTF",
+ "version" : "*",
+ "description" : "A Perl 6 module to deal with W3CDTF dates",
+ "depends" : [ ],
+ "source-type" : "git",
+ "source-url" : "git://github.com/afiskon/p6-datetime-format-w3cdtf.git"
+}
21 README
@@ -0,0 +1,21 @@
+A Perl 6 module to deal with W3CDTF dates.
+
+ use DateTime::Format::W3CDTF;
+
+ my $w3c = DateTime::Format::W3CDTF.new;
+ my DateTime $datetime = $w3c.parse('2012-09-04T11:22:33.5+04:00');
+ say $w3c.format($datetime);
+
+All files (unless noted otherwise) can be used, modified and redistributed
+under the terms of the Artistic License Version 2.
+
+To build and test this module, plese get 'ufo' from
+http://github.com/masak/ufo and run
+
+ ufo
+ make
+ make test
+ make install
+
+Credits
+ Alexandr Alexeev <afiskon@gmail.com>
72 lib/DateTime/Format/W3CDTF.pm6
@@ -0,0 +1,72 @@
+#!/usr/bin/env perl6
+
+use v6;
+
+class DateTime::Format::W3CDTF;
+
+method parse (Str $str) of DateTime {
+ my Str $date-str = $str;
+ my Match $m;
+
+ # YYYY-MM-DDThh:mm:ss.sTZD --> YYYY-MM-DDThh:mm:ssTZD
+ if $m = $date-str ~~ /^ ( .* T <[0..9]> ** 2 \: <[0..9]> ** 2 \: <[0..9]> ** 2) \. <[0..9]> + ( .*) $/ {
+ $date-str = $m[0] ~ $m[1];
+ }
+
+ # YYYY-MM-DDThh:mmTZD --> YYYY-MM-DDThh:mm:00TZD
+ if $m = $date-str ~~ /^ ( .* T <[0..9]> ** 2 \: <[0..9]> ** 2 ) ( <[Z+-]> .* ) $/ {
+ $date-str = $m[0] ~ ':00' ~ $m[1];
+ }
+
+ if $date-str ~~ / . ** 16..* <[+-]> <[0..9]> ** 4 $/ {
+ die 'Invalid timezone format';
+ }
+
+ if $m = $date-str ~~ /^ ( . ** 16..* <[+-]> <[0..9]> ** 2 ) \: ( <[0..9]> ** 2 ) $/ {
+ $date-str = $m[0] ~ $m[1];
+ }
+
+ given $date-str {
+ when /^ <[0..9]> ** 4 $/ {
+ $date-str ~= '-01-01T00:00:00Z';
+ }
+ when /^ <[0..9]> ** 4 \- <[0..9]> ** 2 $/ {
+ $date-str ~= '-01T00:00:00Z';
+ }
+ when /^ <[0..9]> ** 4 \- <[0..9]> ** 2 \- <[0..9]> ** 2 $/ {
+ $date-str ~= 'T00:00:00Z';
+ }
+ when / ( Z | <[+-]> <[0..9]> ** 4 ) $/ {
+ # ok, do nothing
+ }
+ default {
+ die 'Timezone missing';
+ }
+ }
+ return DateTime.new($date-str);
+}
+
+method format (DateTime $date) of Str {
+ my Str $result = $date.Str;
+ if my $m = $result ~~ /^( .* <[+-]> <[0..9]> ** 2 ) ( <[0..9]> ** 2 )$/ {
+ $result = $m[0] ~ ':' ~ $m[1];
+ }
+ return $result;
+}
+
+=begin pod
+
+=head1 NAME
+
+A Perl 6 module to deal with W3CDTF dates
+
+=head1 SYNOPSYS
+
+ use DateTime::Format::W3CDTF;
+
+ my $w3c = DateTime::Format::W3CDTF.new;
+ my DateTime $datetime = $w3c.parse('2012-09-04T11:22:33.5+04:00');
+ say $w3c.format($datetime);
+
+=end pod
+
70 t/main.t
@@ -0,0 +1,70 @@
+use v6;
+use lib 'lib';
+use Test;
+use DateTime::Format::W3CDTF;
+
+my $w3c = DateTime::Format::W3CDTF.new;
+
+dies_ok({ $w3c.parse('bebebe') });
+dies_ok({ $w3c.parse('999') });
+
+ok($w3c.parse('2012').Str eq '2012-01-01T00:00:00Z');
+ok($w3c.parse('0001').Str eq '0001-01-01T00:00:00Z');
+dies_ok({ $w3c.parse('2012Z') });
+dies_ok({ $w3c.parse('2012+1234') });
+
+ok($w3c.parse('2012-02').Str eq '2012-02-01T00:00:00Z');
+dies_ok({ $w3c.parse('2012-13') });
+dies_ok({ $w3c.parse('2012-1') });
+dies_ok({ $w3c.parse('2012-12Z') });
+dies_ok({ $w3c.parse('2012-12+1234') });
+
+ok($w3c.parse('2012-02-03').Str eq '2012-02-03T00:00:00Z');
+dies_ok({ $w3c.parse('2012-1-01') });
+dies_ok({ $w3c.parse('2012-01-2') });
+dies_ok({ $w3c.parse('2012-13-01') });
+dies_ok({ $w3c.parse('2012-01-32') });
+dies_ok({ $w3c.parse('2012-02-03Z') });
+dies_ok({ $w3c.parse('2012-02-03+1234') });
+
+dies_ok({ $w3c.parse('1988-12-13:14') });
+dies_ok({ $w3c.parse('1988-12-13:14Z') });
+dies_ok({ $w3c.parse('1988-12-13:14+1234') });
+
+ok($w3c.parse('1988-12-13T14:15Z').Str eq '1988-12-13T14:15:00Z');
+ok($w3c.parse('1988-12-13T14:15+04:30').Str eq '1988-12-13T14:15:00+0430');
+ok($w3c.parse('1988-12-13T14:15-02:20').Str eq '1988-12-13T14:15:00-0220');
+dies_ok({ $w3c.parse('1988-12-13:14:15') });
+dies_ok({ $w3c.parse('1988-12-13:14:15+1234') });
+dies_ok({ $w3c.parse('1988-1-13:14:15Z') });
+dies_ok({ $w3c.parse('1988-12-1:14:15Z') });
+dies_ok({ $w3c.parse('1988-12-13:1:15Z') });
+dies_ok({ $w3c.parse('1988-12-13:14:1Z') });
+dies_ok({ $w3c.parse('1988-13-13:14:15Z') });
+dies_ok({ $w3c.parse('1988-12-32:14:15Z') });
+dies_ok({ $w3c.parse('1988-12-13:25:15Z') });
+dies_ok({ $w3c.parse('1988-12-13:14:61Z') });
+
+ok($w3c.parse('1988-12-13T14:15:16Z').Str eq '1988-12-13T14:15:16Z');
+ok($w3c.parse('1988-12-13T14:15:16+01:00').Str eq '1988-12-13T14:15:16+0100');
+ok($w3c.parse('1988-12-13T14:15:16-02:30').Str eq '1988-12-13T14:15:16-0230');
+dies_ok({ $w3c.parse('1988-12-13T14:15:16') });
+dies_ok({ $w3c.parse('1988-12-13T14:15:16+1234') });
+dies_ok({ $w3c.parse('1988-13-13T14:15:16Z') });
+dies_ok({ $w3c.parse('1988-12-32T14:15:16Z') });
+dies_ok({ $w3c.parse('1988-12-13T25:15:16Z') });
+dies_ok({ $w3c.parse('1988-12-13T14:61:16Z') });
+dies_ok({ $w3c.parse('1988-12-13T14:15:61Z') });
+
+ok($w3c.parse('1988-12-13T14:15:16.1Z').Str eq '1988-12-13T14:15:16Z');
+ok($w3c.parse('1988-12-13T14:15:16.123456789+01:30').Str eq '1988-12-13T14:15:16+0130');
+ok($w3c.parse('1988-12-13T14:15:16.123456789-02:40').Str eq '1988-12-13T14:15:16-0240');
+dies_ok({ $w3c.parse('1988-12-13T14:15:16.123456789') });
+dies_ok({ $w3c.parse('1988-12-13T14:15:16.123456789+0130') });
+
+ok($w3c.format(DateTime.new('2012-04-05T06:07:08+1234')) eq '2012-04-05T06:07:08+12:34');
+ok($w3c.format(DateTime.new('2012-04-05T06:07:08-1234')) eq '2012-04-05T06:07:08-12:34');
+ok($w3c.format(DateTime.new('2012-04-05T06:07:08+0000')) eq '2012-04-05T06:07:08Z');
+
+done;
+
Please sign in to comment.
Something went wrong with that request. Please try again.