Skip to content

Instantly share code, notes, and snippets.

@ryangreenberg
Last active December 26, 2015 04:19
Show Gist options
  • Save ryangreenberg/7092264 to your computer and use it in GitHub Desktop.
Save ryangreenberg/7092264 to your computer and use it in GitHub Desktop.
Ruby's strptime %z parses things that I didn't expect it to. Equivalent examples from other languages welcome.
#!/usr/bin/env ruby -wKU
require 'date'
# strptime %z parses all of the following in Ruby
iso_8601_timezones = %[
Z
UTC
PST
PDT
NZDT
+00
-00
+0000
-0000
+00:00
-00:00
Pacific Standard Time
Eastern Daylight Time
Hawaiian
Dateline
Samoa
].strip.split("\n")
iso_8601_timezones.each do |tz|
dt_str = "2013-01-01T00:00:00#{tz}"
parsed_time = DateTime.strptime(dt_str, "%Y-%m-%dT%H:%M:%S%z").to_time
puts "#{dt_str}\t#{parsed_time}"
end
$ rbenv shell 1.9.3-p448; ./percent_z_strptime.rb
2013-01-01T00:00:00Z 2012-12-31 16:00:00 -0800
2013-01-01T00:00:00UTC 2012-12-31 16:00:00 -0800
2013-01-01T00:00:00PST 2013-01-01 00:00:00 -0800
2013-01-01T00:00:00PDT 2012-12-31 23:00:00 -0800
2013-01-01T00:00:00NZDT 2012-12-31 03:00:00 -0800
2013-01-01T00:00:00+00 2012-12-31 16:00:00 -0800
2013-01-01T00:00:00-00 2012-12-31 16:00:00 -0800
2013-01-01T00:00:00+0000 2012-12-31 16:00:00 -0800
2013-01-01T00:00:00-0000 2012-12-31 16:00:00 -0800
2013-01-01T00:00:00+00:00 2012-12-31 16:00:00 -0800
2013-01-01T00:00:00-00:00 2012-12-31 16:00:00 -0800
2013-01-01T00:00:00Pacific Standard Time 2013-01-01 00:00:00 -0800
2013-01-01T00:00:00Eastern Daylight Time 2012-12-31 20:00:00 -0800
2013-01-01T00:00:00Hawaiian 2013-01-01 02:00:00 -0800
2013-01-01T00:00:00Dateline 2013-01-01 04:00:00 -0800
2013-01-01T00:00:00Samoa 2013-01-01 03:00:00 -0800
$ rbenv shell 2.0.0-p247; ./percent_z_strptime.rb
ruby: warning: -K is specified; it is for 1.8 compatibility and may cause odd behavior
2013-01-01T00:00:00Z 2012-12-31 16:00:00 -0800
2013-01-01T00:00:00UTC 2012-12-31 16:00:00 -0800
2013-01-01T00:00:00PST 2013-01-01 00:00:00 -0800
2013-01-01T00:00:00PDT 2012-12-31 23:00:00 -0800
2013-01-01T00:00:00NZDT 2012-12-31 03:00:00 -0800
2013-01-01T00:00:00+00 2012-12-31 16:00:00 -0800
2013-01-01T00:00:00-00 2012-12-31 16:00:00 -0800
2013-01-01T00:00:00+0000 2012-12-31 16:00:00 -0800
2013-01-01T00:00:00-0000 2012-12-31 16:00:00 -0800
2013-01-01T00:00:00+00:00 2012-12-31 16:00:00 -0800
2013-01-01T00:00:00-00:00 2012-12-31 16:00:00 -0800
2013-01-01T00:00:00Pacific Standard Time 2013-01-01 00:00:00 -0800
2013-01-01T00:00:00Eastern Daylight Time 2012-12-31 20:00:00 -0800
2013-01-01T00:00:00Hawaiian 2013-01-01 02:00:00 -0800
2013-01-01T00:00:00Dateline 2013-01-01 04:00:00 -0800
2013-01-01T00:00:00Samoa 2013-01-01 03:00:00 -0800
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment