Class: Tai64::Label

Inherits:
Object
  • Object
show all
Defined in:
lib/tai64.rb

Instance Method Summary collapse

Constructor Details

#initialize(str, leap_second_fudge = 10, nano_second_fudge = 500) ⇒ Label

Returns a new instance of Label.



21
22
23
24
25
# File 'lib/tai64.rb', line 21

def initialize str, leap_second_fudge = 10, nano_second_fudge = 500
  self.str = str.gsub /^@/, ''
  self.leap_second_fudge = leap_second_fudge
  self.nano_second_fudge = nano_second_fudge
end

Instance Method Details

#format_stringObject



74
75
76
77
78
79
80
81
# File 'lib/tai64.rb', line 74

def format_string
  fmt = "%016x"
  return fmt if tai_nanosecond.nil?
  fmt << "%08x"
  return fmt if tai_attosecond.nil?
  fmt << "%08x"
  return fmt
end

#tai_attosecondObject



59
60
61
62
63
# File 'lib/tai64.rb', line 59

def tai_attosecond
  part = str.scan(/^(?:\@)?[0-9abcdef]{16}[0-9a-f]{8}([0-9a-f]{8})/i)[0][0]
  part.to_i(16).to_f
rescue
end

#tai_nanosecondObject



48
49
50
51
52
# File 'lib/tai64.rb', line 48

def tai_nanosecond
  part = str.scan(/^(?:\@)?[0-9abcdef]{16}([0-9a-f]{8})/i)[0][0]
  part.to_i(16).to_f
rescue
end

#tai_partsObject



70
71
72
# File 'lib/tai64.rb', line 70

def tai_parts
  [ tai_second, tai_nanosecond, tai_attosecond ].compact
end

#tai_referenceObject



90
91
92
93
94
95
# File 'lib/tai64.rb', line 90

def tai_reference
  tai_time = tai_second
  tai_time += tai_nanosecond / (10 ** 9) unless tai_nanosecond.nil?
  tai_time += tai_attosecond / (10 ** 18) unless tai_attosecond.nil?
  tai_time
end

#tai_secondObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/tai64.rb', line 31

def tai_second
  s = str.scan(/^\@?([0-9abcdef]{16})/i)[0][0].to_i(16)
  if s.between? 0, EPOCH - 1
    tai_second = EPOCH - s
  elsif s.between? EPOCH, MAXIMUM
    tai_second = s - EPOCH
  else
    raise "I don't know how to deal with s=#{s}"
  end
end

#to_sObject



27
28
29
# File 'lib/tai64.rb', line 27

def to_s
  "@#{str}" % tai_parts
end

#to_timeObject

Warning, this will probably lose accuracy - Ruby does not support the same level of timing accuracy as TAI64N and TA64NA can provide.



99
100
101
102
# File 'lib/tai64.rb', line 99

def to_time
  t = Time.at utc_reference
  t.utc
end

#utc_attosecondObject



65
66
67
68
# File 'lib/tai64.rb', line 65

def utc_attosecond
  tai_attosecond
rescue
end

#utc_nanosecondObject



54
55
56
57
# File 'lib/tai64.rb', line 54

def utc_nanosecond
  tai_nanosecond - nano_second_fudge
rescue
end

#utc_referenceObject



83
84
85
86
87
88
# File 'lib/tai64.rb', line 83

def utc_reference
  utc_time = utc_second
  utc_time += utc_nanosecond / (10 ** 9) unless utc_nanosecond.nil?
  utc_time += utc_attosecond / (10 ** 18) unless utc_attosecond.nil?
  utc_time
end

#utc_secondObject



42
43
44
45
46
# File 'lib/tai64.rb', line 42

def utc_second
  # UTC was 10 seconds behind TAI when the International Earth Rotation
  # Service started adding leap seconds
  tai_second - 10
end