unix_epoch 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +12 -0
- data/VERSION +1 -1
- data/lib/unix_epoch.rb +23 -9
- data/test/unix_epoch_test.rb +8 -0
- data/unix_epoch.gemspec +5 -4
- metadata +5 -10
data/README.rdoc
CHANGED
@@ -28,6 +28,18 @@ Documentation is available online at at rdoc.info[http://rdoc.info/projects/chet
|
|
28
28
|
|
29
29
|
sudo gem install unix_epoch
|
30
30
|
|
31
|
+
== FURTHER READING:
|
32
|
+
|
33
|
+
Here's some good information on various Calendar systems:
|
34
|
+
|
35
|
+
* http://mysite.verizon.net/aesir_research/date/back.htm
|
36
|
+
* https://secure.wikimedia.org/wikipedia/en/wiki/Julian_day
|
37
|
+
* http://www.fourmilab.ch/solar/help/timedate.html
|
38
|
+
|
39
|
+
And the Perl library from which I borrowed some ideas:
|
40
|
+
|
41
|
+
* http://search.cpan.org/~chorny/DateTime-Format-Epoch-0.13/
|
42
|
+
|
31
43
|
== LICENSE:
|
32
44
|
|
33
45
|
(The MIT License)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/unix_epoch.rb
CHANGED
@@ -4,6 +4,7 @@
|
|
4
4
|
# better understood by Ruby's DateTime library
|
5
5
|
|
6
6
|
require 'date'
|
7
|
+
require 'tzinfo'
|
7
8
|
|
8
9
|
module UnixEpoch
|
9
10
|
|
@@ -12,10 +13,14 @@ module UnixEpoch
|
|
12
13
|
# Create a new DateTime object from a given Unix Timestamp
|
13
14
|
#
|
14
15
|
# @param [Fixnum, Bignum] unix_ts seconds since unix epoch (1970-01-01 00:00:00 UTC)
|
15
|
-
# @param [Fixnum] offset offset in seconds from UTC time
|
16
|
+
# @param [Fixnum, TZInfo::Timezone] offset offset in seconds from UTC time or an instance of TZInfo::TimeZone
|
16
17
|
# @return [DateTime] DateTime object representation of the given Unix TS
|
17
18
|
def from_unix_ts(unix_ts, offset = 0)
|
18
19
|
|
20
|
+
if not (offset.respond_to? :to_i or offset.kind_of? TZInfo::Timezone) then
|
21
|
+
raise "Invalid value passed for offset: must be an integer or TZInfo::Timezone"
|
22
|
+
end
|
23
|
+
|
19
24
|
# step 1) get the delta in days, seconds and nano seconds represented by unix_ts
|
20
25
|
|
21
26
|
delta_days = UnixEpoch._floor(unix_ts / 86_400)
|
@@ -36,10 +41,19 @@ module UnixEpoch
|
|
36
41
|
|
37
42
|
jd_days = epoch_jd_days + delta_days
|
38
43
|
|
39
|
-
secs = epoch_secs + delta_secs
|
44
|
+
secs = epoch_secs + delta_secs
|
45
|
+
if offset.respond_to? :to_i
|
46
|
+
secs += offset # add tz offset back in also
|
47
|
+
end
|
40
48
|
jd_secs = UnixEpoch.secs_to_jd_secs(secs) + Rational(delta_nano.round, 86_400_000_000)
|
41
49
|
|
42
|
-
|
50
|
+
if offset.respond_to? :to_i
|
51
|
+
return DateTime.from_jd(jd_days, jd_secs, offset)
|
52
|
+
elsif offset.kind_of? TZInfo::Timezone
|
53
|
+
dt = DateTime.from_jd(jd_days, jd_secs, 0) # first create a new DateTime
|
54
|
+
ldt = offset.utc_to_local(dt) # then convert it to our target TZ
|
55
|
+
return DateTime.from_jd(ldt.jd, ldt.day_fraction, (ldt.to_i - dt.to_i)) # then create a new DateTime with the current offset
|
56
|
+
end
|
43
57
|
end
|
44
58
|
|
45
59
|
end
|
@@ -84,17 +98,17 @@ module UnixEpoch
|
|
84
98
|
end
|
85
99
|
|
86
100
|
# Retrieve the seconds part of the given Julian Date
|
87
|
-
|
88
|
-
|
89
|
-
|
101
|
+
#
|
102
|
+
# @param [Float] Julian Date including fractional value (e.g., 2455647.543)
|
103
|
+
# @return [Fixnum] Number of seconds represented by the fractional value
|
90
104
|
def self.get_jd_secs(jd)
|
91
105
|
jd_fr_to_secs(jd % 1)
|
92
106
|
end
|
93
107
|
|
94
108
|
# Convert the given Julian Date fractional value into seconds
|
95
|
-
|
96
|
-
|
97
|
-
|
109
|
+
#
|
110
|
+
# @param [Float] Julian Date fractional value (e.g., 0.543)
|
111
|
+
# @return [Fixnum] Number of seconds represented by the fractional value
|
98
112
|
def self.jd_fr_to_secs(fr)
|
99
113
|
h, m, s = DateTime.day_fraction_to_time(fr)
|
100
114
|
(h * 3600 + m * 60 + s)
|
data/test/unix_epoch_test.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
|
2
|
+
require 'rubygems'
|
2
3
|
require 'test/unit'
|
3
4
|
require 'unix_epoch'
|
5
|
+
require 'timezone_local'
|
4
6
|
|
5
7
|
class UnixEpoch_Test < Test::Unit::TestCase
|
6
8
|
|
@@ -41,4 +43,10 @@ class UnixEpoch_Test < Test::Unit::TestCase
|
|
41
43
|
assert t.to_i == i
|
42
44
|
assert d.to_unix_ts == i
|
43
45
|
end
|
46
|
+
|
47
|
+
def test_from_unix_ts_with_tzinfo
|
48
|
+
tz = TZInfo::Timezone.get_local_timezone
|
49
|
+
i = 1301103992
|
50
|
+
assert DateTime.from_unix_ts(i, tz).strftime(@f) == Time.at(i).strftime(@f)
|
51
|
+
end
|
44
52
|
end
|
data/unix_epoch.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{unix_epoch}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Chetan Sarva"]
|
12
|
-
s.date = %q{2011-03-
|
12
|
+
s.date = %q{2011-03-28}
|
13
13
|
s.description = %q{Adds from_unix_ts and to_unix_ts methods to the DateTime class}
|
14
14
|
s.email = %q{chetan@pixelcop.net}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -25,16 +25,17 @@ Gem::Specification.new do |s|
|
|
25
25
|
]
|
26
26
|
s.homepage = %q{http://github.com/chetan/unix_epoch}
|
27
27
|
s.require_paths = ["lib"]
|
28
|
-
s.rubygems_version = %q{1.
|
28
|
+
s.rubygems_version = %q{1.3.6}
|
29
29
|
s.summary = %q{Adds methods to DateTime for dealing with Unix Timestamps}
|
30
30
|
s.test_files = [
|
31
31
|
"test/unix_epoch_test.rb"
|
32
32
|
]
|
33
33
|
|
34
34
|
if s.respond_to? :specification_version then
|
35
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
35
36
|
s.specification_version = 3
|
36
37
|
|
37
|
-
if Gem::Version.new(Gem::
|
38
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
38
39
|
else
|
39
40
|
end
|
40
41
|
else
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unix_epoch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
4
|
+
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
|
-
-
|
7
|
+
- 2
|
9
8
|
- 0
|
10
|
-
version: 0.
|
9
|
+
version: 0.2.0
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Chetan Sarva
|
@@ -15,7 +14,7 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2011-03-
|
17
|
+
date: 2011-03-28 00:00:00 -04:00
|
19
18
|
default_executable:
|
20
19
|
dependencies: []
|
21
20
|
|
@@ -44,27 +43,23 @@ rdoc_options: []
|
|
44
43
|
require_paths:
|
45
44
|
- lib
|
46
45
|
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
-
none: false
|
48
46
|
requirements:
|
49
47
|
- - ">="
|
50
48
|
- !ruby/object:Gem::Version
|
51
|
-
hash: 3
|
52
49
|
segments:
|
53
50
|
- 0
|
54
51
|
version: "0"
|
55
52
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
-
none: false
|
57
53
|
requirements:
|
58
54
|
- - ">="
|
59
55
|
- !ruby/object:Gem::Version
|
60
|
-
hash: 3
|
61
56
|
segments:
|
62
57
|
- 0
|
63
58
|
version: "0"
|
64
59
|
requirements: []
|
65
60
|
|
66
61
|
rubyforge_project:
|
67
|
-
rubygems_version: 1.
|
62
|
+
rubygems_version: 1.3.6
|
68
63
|
signing_key:
|
69
64
|
specification_version: 3
|
70
65
|
summary: Adds methods to DateTime for dealing with Unix Timestamps
|