Class: Tai64::Label

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

Instance Method Summary collapse

Methods included from Fudge

included

Constructor Details

#initialize(str) ⇒ Label

Returns a new instance of Label.



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

def initialize str
  self.str = str.gsub /^@/, ''
end

Instance Method Details

#format_stringObject



135
136
137
138
139
140
141
142
# File 'lib/tai64.rb', line 135

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



120
121
122
123
124
# File 'lib/tai64.rb', line 120

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



109
110
111
112
113
# File 'lib/tai64.rb', line 109

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



131
132
133
# File 'lib/tai64.rb', line 131

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

#tai_referenceObject



151
152
153
154
155
156
# File 'lib/tai64.rb', line 151

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



92
93
94
95
96
97
98
99
100
101
# File 'lib/tai64.rb', line 92

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

#to_sObject



88
89
90
# File 'lib/tai64.rb', line 88

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.



160
161
162
163
# File 'lib/tai64.rb', line 160

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

#utc_attosecondObject



126
127
128
129
# File 'lib/tai64.rb', line 126

def utc_attosecond
  tai_attosecond
rescue
end

#utc_nanosecondObject



115
116
117
118
# File 'lib/tai64.rb', line 115

def utc_nanosecond
  tai_nanosecond - nano_second_fudge
rescue
end

#utc_referenceObject



144
145
146
147
148
149
# File 'lib/tai64.rb', line 144

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



103
104
105
106
107
# File 'lib/tai64.rb', line 103

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