-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time handling improved to ensure UTC always used
- Loading branch information
Showing
9 changed files
with
662 additions
and
536 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module CouchRest | ||
module Model | ||
module CoreExtensions | ||
module TimeParsing | ||
|
||
# Attemtps to parse a time string in ISO8601 format. | ||
# If no match is found, the standard time parse will be used. | ||
# | ||
# Times, unless provided with a time zone, are assumed to be in | ||
# UTC. | ||
# | ||
def parse_iso8601(string) | ||
if (string =~ /(\d{4})[\-|\/](\d{2})[\-|\/](\d{2})[T|\s](\d{2}):(\d{2}):(\d{2})(Z| ?([\+|\s|\-])?(\d{2}):?(\d{2}))?/) | ||
# $1 = year | ||
# $2 = month | ||
# $3 = day | ||
# $4 = hours | ||
# $5 = minutes | ||
# $6 = seconds | ||
# $7 = UTC or Timezone | ||
# $8 = time zone direction | ||
# $9 = tz difference hours | ||
# $10 = tz difference minutes | ||
|
||
if (!$7.to_s.empty? && $7 != 'Z') | ||
new($1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, "#{$8 == '-' ? '-' : '+'}#{$9}:#{$10}") | ||
else | ||
utc($1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i) | ||
end | ||
else | ||
parse(string) | ||
end | ||
end | ||
|
||
end | ||
end | ||
end | ||
end | ||
|
||
Time.class_eval do | ||
extend CouchRest::Model::CoreExtensions::TimeParsing | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# encoding: utf-8 | ||
require File.expand_path('../../../spec_helper', __FILE__) | ||
|
||
describe "Time Parsing core extension" do | ||
|
||
describe "Time" do | ||
|
||
it "should respond to .parse_iso8601" do | ||
Time.respond_to?("parse_iso8601").should be_true | ||
end | ||
|
||
describe ".parse_iso8601" do | ||
|
||
describe "parsing" do | ||
|
||
before :each do | ||
# Time.parse should not be called for these tests! | ||
Time.stub!(:parse).and_return(nil) | ||
end | ||
|
||
it "should parse JSON time" do | ||
txt = "2011-04-01T19:05:30Z" | ||
Time.parse_iso8601(txt).should eql(Time.utc(2011, 04, 01, 19, 05, 30)) | ||
end | ||
|
||
it "should parse JSON time as UTC without Z" do | ||
txt = "2011-04-01T19:05:30" | ||
Time.parse_iso8601(txt).should eql(Time.utc(2011, 04, 01, 19, 05, 30)) | ||
end | ||
|
||
it "should parse basic time as UTC" do | ||
txt = "2011-04-01 19:05:30" | ||
Time.parse_iso8601(txt).should eql(Time.utc(2011, 04, 01, 19, 05, 30)) | ||
end | ||
|
||
it "should parse JSON time with zone" do | ||
txt = "2011-04-01T19:05:30 +02:00" | ||
Time.parse_iso8601(txt).should eql(Time.new(2011, 04, 01, 19, 05, 30, "+02:00")) | ||
end | ||
|
||
it "should parse JSON time with zone 2" do | ||
txt = "2011-04-01T19:05:30-0200" | ||
Time.parse_iso8601(txt).should eql(Time.new(2011, 04, 01, 19, 05, 30, "-02:00")) | ||
end | ||
|
||
it "should parse dodgy time with zone" do | ||
txt = "2011-04-01 19:05:30 +0200" | ||
Time.parse_iso8601(txt).should eql(Time.new(2011, 04, 01, 19, 05, 30, "+02:00")) | ||
end | ||
|
||
it "should parse dodgy time with zone 2" do | ||
txt = "2011-04-01 19:05:30+0230" | ||
Time.parse_iso8601(txt).should eql(Time.new(2011, 04, 01, 19, 05, 30, "+02:30")) | ||
end | ||
|
||
it "should parse dodgy time with zone 3" do | ||
txt = "2011-04-01 19:05:30 0230" | ||
Time.parse_iso8601(txt).should eql(Time.new(2011, 04, 01, 19, 05, 30, "+02:30")) | ||
end | ||
|
||
end | ||
|
||
describe "resorting back to normal parse" do | ||
before :each do | ||
Time.should_receive(:parse) | ||
end | ||
it "should work with weird time" do | ||
txt = "16/07/1981 05:04:00" | ||
Time.parse_iso8601(txt) | ||
end | ||
|
||
end | ||
end | ||
|
||
end | ||
|
||
end |
Oops, something went wrong.