Created
July 28, 2008 23:43
-
-
Save ktsugita/2983 to your computer and use it in GitHub Desktop.
tai64.rb
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
# tai64.rb | |
# http://cr.yp.to/libtai/tai64.html | |
class Time | |
TAI64_REGEX = Regexp.new(/(?:^\@)?([0-9a-fA-F]{16})/) | |
TAI64N_REGEX = Regexp.new(/#{TAI64_REGEX}([0-9a-fA-F]{8})/) | |
def self.tai64(str, leapseconds=10) | |
if match = TAI64N_REGEX.match(str).to_a.values_at(1) | |
tai64 = match[0].hex - 2**62 - leapseconds | |
return Time.at(tai64) | |
end | |
raise ArgumentError, "not TAI64 compliant date: #{str}" | |
end | |
def self.tai64n(str, leapseconds=10) | |
if match = TAI64N_REGEX.match(str).to_a.values_at(1,2) | |
tai64 = match[0].hex - 2**62 - leapseconds | |
nano = match[1].hex / 10**3 | |
return Time.at(tai64,nano) | |
end | |
raise ArgumentError, "not TAI64N compliant date: #{str}" | |
end | |
def tai64(leapseconds=10) | |
return sprintf("%016x", 2**62 + self.to_i + leapseconds) | |
end | |
def tai64n(leapseconds=10,nanosec=500) | |
return sprintf("%016x%08x", 2**62 + self.to_i + leapseconds, self.usec * 10**3 + nanosec) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment