Created
July 8, 2009 01:52
-
-
Save mattetti/142504 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'time' | |
require 'date' | |
STAMP = "2009/06/29 23:19:35 +0000" | |
class Time | |
def self.parse_utc(string) | |
string =~ /(\d{4})\/(\d{2})\/(\d{2}) (\d{2}):(\d{2}):(\d{2})/ | |
utc($1, RFC2822_MONTH_NAME[$2.to_i - 1], $3, $4, $5, $6) | |
end | |
# Returns a UTC time object | |
def self.parse_gm(string) | |
string =~ /(\d{4})\/(\d{2})\/(\d{2}) (\d{2}):(\d{2}):(\d{2}) ([\+\-]\d{4})/ | |
# $1 = year | |
# $2 = month | |
# $3 = day | |
# $4 = hours | |
# $5 = minutes | |
# $6 = seconds | |
# $7 = time zone | |
gm($1, RFC2822_MONTH_NAME[$2.to_i - 1], $3, $4, $5, $6, $7) | |
end | |
# Returns a local time object | |
def self.parse_mktime(string) | |
string =~ /(\d{4})\/(\d{2})\/(\d{2}) (\d{2}):(\d{2}):(\d{2}) ([\+\-]\d{4})/ | |
mktime($1, RFC2822_MONTH_NAME[$2.to_i - 1], $3, $4, $5, $6, $7) | |
end | |
def self.parse_maketime(string) | |
string =~ /(\d{4})\/(\d{2})\/(\d{2}) (\d{2}):(\d{2}):(\d{2}) ([\+\-]\d{4})/ | |
make_time($1, RFC2822_MONTH_NAME[$2.to_i - 1], $3, $4, $5, $6, nil, $7, now) | |
end | |
# returns a local time value much faster than Time.parse | |
def self.mktime_with_offset(string) | |
string =~ /(\d{4})\/(\d{2})\/(\d{2}) (\d{2}):(\d{2}):(\d{2}) ([\+\-])(\d{2})/ | |
# $1 = year | |
# $2 = month | |
# $3 = day | |
# $4 = hours | |
# $5 = minutes | |
# $6 = seconds | |
# $7 = time zone direction | |
# $8 = tz difference | |
time = mktime($1, RFC2822_MONTH_NAME[$2.to_i - 1], $3, $4, $5, $6, $7) | |
tz_difference = ("#{$7 == '-' ? '+' : '-'}#{$8}".to_i * 3600) | |
time + tz_difference + zone_offset(time.zone) | |
end | |
end | |
require 'benchmark' | |
n = 500 | |
Benchmark.bm(10) do |test| | |
test.report("Time.parse: ") do n.times {Time.parse(STAMP)} end | |
test.report("Time.parse_gm: ") do n.times {Time.parse_gm(STAMP)} end | |
test.report("Time.parse_mktime: ") do n.times {Time.parse_mktime(STAMP)} end | |
test.report("Time.parse_maketime: ") do n.times {Time.parse_maketime(STAMP)} end | |
test.report("Time.parse_utc: ") do n.times {Time.parse_utc(STAMP)} end | |
test.report("Time::mktime_with_offset: ") do n.times {Time.mktime_with_offset(STAMP)} end | |
end |
This file contains hidden or 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
user system total real | |
Time.parse: 0.380000 0.960000 1.340000 ( 3.684890) | |
Time.parse_gm: 0.290000 0.950000 1.240000 ( 3.539086) | |
Time.parse_mktime: 0.020000 0.010000 0.030000 ( 0.025100) | |
Time.parse_maketime: 0.290000 0.950000 1.240000 ( 3.572833) | |
Time.parse_utc: 0.280000 0.960000 1.240000 ( 3.552548) | |
Time.mktime_with_offset: 0.020000 0.000000 0.020000 ( 0.024883) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment