tai64 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +6 -2
  2. data/lib/tai64/version.rb +1 -1
  3. data/lib/tai64.rb +72 -13
  4. metadata +1 -1
data/README.md CHANGED
@@ -25,8 +25,12 @@ Or install it yourself as:
25
25
  ## Usage
26
26
 
27
27
  require 'tai64'
28
- t = Tai64.parse '@400000005083208a00ffffff'
29
- puts t.to_time.to_s # => "2012-10-20 22:06:56 UTC"
28
+
29
+ l = Tai64.parse '@400000005083208a00ffffff'
30
+ l.to_time.to_s # => "2012-10-20 22:06:56 UTC"
31
+
32
+ t = Tai64::Time.new Time.at(1350770816)
33
+ t.to_label.to_s # => "@400000005083208a000001f4"
30
34
 
31
35
 
32
36
  ## Contributing
data/lib/tai64/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Tai64
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/tai64.rb CHANGED
@@ -8,20 +8,79 @@ module Tai64
8
8
  Label.new str
9
9
  end
10
10
 
11
+ module Fudge
12
+ def self.included into
13
+ into.class_eval do
14
+ attr_accessor :leap_second_fudge
15
+ private :leap_second_fudge=, :leap_second_fudge
16
+
17
+ attr_accessor :nano_second_fudge
18
+ private :nano_second_fudge=
19
+
20
+ def leap_second_fudge
21
+ @leap_second_fudge ||= 10
22
+ end
23
+
24
+ def nano_second_fudge
25
+ @nano_second_fudge ||= 500
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ class Time
32
+ include Fudge
33
+
34
+ attr_accessor :time
35
+ private :time=, :time
36
+
37
+ def initialize time
38
+ self.time = time
39
+ end
40
+
41
+ def utc_second
42
+ time.to_i
43
+ end
44
+
45
+ def tai_second
46
+ utc_second + 10
47
+ end
48
+
49
+ def utc_nanosecond
50
+ time.to_f - time.to_i
51
+ end
52
+
53
+ def tai_nanosecond
54
+ utc_nanosecond + nano_second_fudge
55
+ end
56
+
57
+ # Warning, this will probably gain inappropriate accuracy - Ruby does not
58
+ # support the same level of timing accuracy as TAI64N and TA64NA can
59
+ # provide.
60
+ def to_label
61
+ s = '%016x%08x'
62
+ sec = tai_second
63
+ ts = if sec >= 0
64
+ sec + EPOCH
65
+ else
66
+ EPOCH - sec
67
+ end
68
+ Label.new s % [ ts, tai_nanosecond ]
69
+ end
70
+
71
+ def to_s
72
+ time.to_s
73
+ end
74
+ end
75
+
11
76
  class Label
77
+ include Fudge
78
+
12
79
  attr_accessor :str
13
80
  private :str=, :str
14
81
 
15
- attr_accessor :leap_second_fudge
16
- private :leap_second_fudge=, :leap_second_fudge
17
-
18
- attr_accessor :nano_second_fudge
19
- private :nano_second_fudge=, :nano_second_fudge
20
-
21
- def initialize str, leap_second_fudge = 10, nano_second_fudge = 500
82
+ def initialize str
22
83
  self.str = str.gsub /^@/, ''
23
- self.leap_second_fudge = leap_second_fudge
24
- self.nano_second_fudge = nano_second_fudge
25
84
  end
26
85
 
27
86
  def to_s
@@ -31,9 +90,9 @@ module Tai64
31
90
  def tai_second
32
91
  s = str.scan(/^\@?([0-9abcdef]{16})/i)[0][0].to_i(16)
33
92
  if s.between? 0, EPOCH - 1
34
- tai_second = EPOCH - s
93
+ return EPOCH - s
35
94
  elsif s.between? EPOCH, MAXIMUM
36
- tai_second = s - EPOCH
95
+ return s - EPOCH
37
96
  else
38
97
  raise "I don't know how to deal with s=#{s}"
39
98
  end
@@ -97,8 +156,8 @@ module Tai64
97
156
  # Warning, this will probably lose accuracy - Ruby does not support the
98
157
  # same level of timing accuracy as TAI64N and TA64NA can provide.
99
158
  def to_time
100
- t = Time.at utc_reference
101
- t.utc
159
+ t = ::Time.at utc_reference
160
+ Time.new t.utc
102
161
  end
103
162
  end
104
163
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tai64
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: