tai64 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tai64.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Craig R Webster
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Tai64
2
+
3
+ Work withTAI64 timestamps:
4
+
5
+ http://cr.yp.to/libtai/tai64.html
6
+
7
+ TAI64 is amazing. You should be using it for your logs.
8
+
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'tai64'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install tai64
23
+
24
+
25
+ ## Usage
26
+
27
+ require 'tai64'
28
+ t = Tai64.parse '@400000005083208a00ffffff'
29
+ puts t.to_time.to_s # => "2012-10-20 22:06:56 UTC"
30
+
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/bin/tai64nlocal ADDED
@@ -0,0 +1,14 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'tai64'
4
+ require 'time'
5
+
6
+ $stdin.each_line do |l|
7
+ parts = l.split /\s+/, 2
8
+ begin
9
+ t = Tai64.parse parts[0]
10
+ puts [ t.to_time.getlocal.iso8601, parts[1] ].join ' '
11
+ rescue
12
+ puts parts.join ' '
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Tai64
2
+ VERSION = "0.0.1"
3
+ end
data/lib/tai64.rb ADDED
@@ -0,0 +1,104 @@
1
+ require "tai64/version"
2
+
3
+ module Tai64
4
+ EPOCH = 2 ** 62
5
+ MAXIMUM = 2 ** 63
6
+
7
+ def self.parse str
8
+ Label.new str
9
+ end
10
+
11
+ class Label
12
+ attr_accessor :str
13
+ private :str=, :str
14
+
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
22
+ self.str = str.gsub /^@/, ''
23
+ self.leap_second_fudge = leap_second_fudge
24
+ self.nano_second_fudge = nano_second_fudge
25
+ end
26
+
27
+ def to_s
28
+ "@#{str}" % tai_parts
29
+ end
30
+
31
+ def tai_second
32
+ s = str.scan(/^\@?([0-9abcdef]{16})/i)[0][0].to_i(16)
33
+ if s.between? 0, EPOCH - 1
34
+ tai_second = EPOCH - s
35
+ elsif s.between? EPOCH, MAXIMUM
36
+ tai_second = s - EPOCH
37
+ else
38
+ raise "I don't know how to deal with s=#{s}"
39
+ end
40
+ end
41
+
42
+ def utc_second
43
+ # UTC was 10 seconds behind TAI when the International Earth Rotation
44
+ # Service started adding leap seconds
45
+ tai_second - 10
46
+ end
47
+
48
+ def tai_nanosecond
49
+ part = str.scan(/^(?:\@)?[0-9abcdef]{16}([0-9a-f]{8})/i)[0][0]
50
+ part.to_i(16).to_f
51
+ rescue
52
+ end
53
+
54
+ def utc_nanosecond
55
+ tai_nanosecond - nano_second_fudge
56
+ rescue
57
+ end
58
+
59
+ def tai_attosecond
60
+ part = str.scan(/^(?:\@)?[0-9abcdef]{16}[0-9a-f]{8}([0-9a-f]{8})/i)[0][0]
61
+ part.to_i(16).to_f
62
+ rescue
63
+ end
64
+
65
+ def utc_attosecond
66
+ tai_attosecond
67
+ rescue
68
+ end
69
+
70
+ def tai_parts
71
+ [ tai_second, tai_nanosecond, tai_attosecond ].compact
72
+ end
73
+
74
+ def format_string
75
+ fmt = "%016x"
76
+ return fmt if tai_nanosecond.nil?
77
+ fmt << "%08x"
78
+ return fmt if tai_attosecond.nil?
79
+ fmt << "%08x"
80
+ return fmt
81
+ end
82
+
83
+ def utc_reference
84
+ utc_time = utc_second
85
+ utc_time += utc_nanosecond / (10 ** 9) unless utc_nanosecond.nil?
86
+ utc_time += utc_attosecond / (10 ** 18) unless utc_attosecond.nil?
87
+ utc_time
88
+ end
89
+
90
+ def tai_reference
91
+ tai_time = tai_second
92
+ tai_time += tai_nanosecond / (10 ** 9) unless tai_nanosecond.nil?
93
+ tai_time += tai_attosecond / (10 ** 18) unless tai_attosecond.nil?
94
+ tai_time
95
+ end
96
+
97
+ # Warning, this will probably lose accuracy - Ruby does not support the
98
+ # same level of timing accuracy as TAI64N and TA64NA can provide.
99
+ def to_time
100
+ t = Time.at utc_reference
101
+ t.utc
102
+ end
103
+ end
104
+ end
data/tai64.gemspec ADDED
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/tai64/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Craig R Webster"]
6
+ gem.email = ["craig@barkingiguana.com"]
7
+ gem.description = %q{Work with TAI64 timestamps}
8
+ gem.summary = %q{Work with TAI64 timestamps}
9
+ gem.homepage = "http://cr.yp.to/libtai/tai64.html"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "tai64"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Tai64::VERSION
17
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tai64
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Craig R Webster
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-20 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Work with TAI64 timestamps
15
+ email:
16
+ - craig@barkingiguana.com
17
+ executables:
18
+ - tai64nlocal
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - bin/tai64nlocal
28
+ - lib/tai64.rb
29
+ - lib/tai64/version.rb
30
+ - tai64.gemspec
31
+ homepage: http://cr.yp.to/libtai/tai64.html
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.24
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Work with TAI64 timestamps
55
+ test_files: []