Permalink
Browse files

Added iso-offset() function and a test for it.

  • Loading branch information...
1 parent d60dff4 commit bc94966cde8e14a633e5d69d2b10207c7dbf977e @supernovus committed Jul 21, 2010
Showing with 36 additions and 1 deletion.
  1. +2 −0 .gitignore
  2. +2 −0 README
  3. +13 −0 lib/DateTime/Utils.pm
  4. +18 −0 t/DateTime-iso-offset.t
  5. +1 −1 t/DateTime-strftime.t
View
@@ -0,0 +1,2 @@
+Makefile
+blib
View
@@ -10,4 +10,6 @@ in the Rakudo tree as DateTime::strftime.
It offers some useful exported subroutines to make working with DateTime
objects much easier.
+Use 'ufo' to generate the Makefile for this project.
+See http://github.com/masak/ufo for more details.
View
@@ -79,5 +79,18 @@ module DateTime::Utils {
September October November December>[$i - 1]
}
+ # This lets you do: $dt2 = $dt.in-timezone(iso-offset('-0800'));
+ # Eventually, I'd like to make a named-offset() for values like 'PST', etc.
+ sub iso-offset(Str $offset) is export(:DEFAULT) {
+ $offset ~~ /^ (Z || (<[\-\+]>) (\d\d)(\d\d)) $/
+ or die "parse-offset() expects an ISO style timezone offset.";
+ if $0 eq 'Z' { return 0; }
+ else {
+ my $seconds = (($0[1]*60 + $0[2]) * 60).Int;
+ $0[0] eq '-' and $seconds = -$seconds;
+ return $seconds;
+ }
+ }
+
}
@@ -0,0 +1,18 @@
+use v6;
+use Test;
+
+BEGIN { @*INC.unshift: 'lib' }
+
+use DateTime::Utils;
+
+plan 3;
+
+my $offset = iso-offset('-0800');
+is $offset, -28800, 'offset in seconds';
+
+my $dt = DateTime.now(:timezone($offset));
+
+is $dt.offset(), -28800, 'DateTime offset in seconds';
+
+is strftime('%z', $dt), '-0800', 'strftime round robin';
+
@@ -1,7 +1,7 @@
use v6;
use Test;
-BEGIN { @*INC.push: 'lib' }
+BEGIN { @*INC.unshift: 'lib' }
plan 2;

0 comments on commit bc94966

Please sign in to comment.