new_time 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +4 -0
- data/bin/new_time +2 -4
- data/lib/new_time/version.rb +1 -1
- data/lib/new_time.rb +99 -45
- data/new_time.gemspec +1 -0
- data/spec/new_time_spec.rb +89 -0
- data/spec/spec_helper.rb +2 -0
- metadata +20 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ecf87326a298905488030af21a6fea616fbd52fa
|
4
|
+
data.tar.gz: cd7f3bf81f047ab7adcc3b8745dfef7d2e1bdf64
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b6e2389d3c4e479eb63cc4a8894e539b41bedf1f322f765ddbcd81fcca84987aa9c61bccca235d0bdd1b3c5a295abe0eca35ee5c4b8ae51483310fa0e699ec6
|
7
|
+
data.tar.gz: 563ba3fbb987eabf1b56b12d7176e42825959daa068a222754186d7fa217ee9d0cc3fc1778ccf8952600f7a75a3c49f73e628de87dcf8afa26edfa19b29351e9
|
data/Rakefile
CHANGED
data/bin/new_time
CHANGED
@@ -2,8 +2,6 @@
|
|
2
2
|
|
3
3
|
require "new_time"
|
4
4
|
|
5
|
-
|
6
|
-
longitude = 150.311407
|
7
|
-
tz = "Australia/Sydney"
|
5
|
+
point = NewTime::Point.new(-33.714955, 150.311407, "Australia/Sydney")
|
8
6
|
|
9
|
-
puts NewTime.current_time(
|
7
|
+
puts NewTime::NewTime.current_time(point)
|
data/lib/new_time/version.rb
CHANGED
data/lib/new_time.rb
CHANGED
@@ -1,55 +1,109 @@
|
|
1
1
|
require 'solareventcalculator'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module NewTime
|
4
|
+
class Point
|
5
|
+
attr_accessor :latitude, :longitude, :tz
|
5
6
|
|
6
|
-
|
7
|
-
|
7
|
+
def initialize(latitude, longitude, tz)
|
8
|
+
@latitude, @longitude, @tz = latitude, longitude, tz
|
9
|
+
end
|
8
10
|
end
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
-
end
|
12
|
+
class NewTime
|
13
|
+
attr_accessor :year, :month, :day, :hours, :minutes, :seconds, :fractional
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
15
|
+
def initialize(year, month, day, hours, minutes, seconds, fractional)
|
16
|
+
@year, @month, @day, @hours, @minutes, @seconds, @fractional = year, month, day, hours, minutes, seconds, fractional
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.current_time(point)
|
20
|
+
convert(DateTime.now, point)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.sunrise(date, point)
|
24
|
+
SolarEventCalculator.new(date, point.latitude, point.longitude).compute_official_sunrise(point.tz)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.sunset(date, point)
|
28
|
+
SolarEventCalculator.new(date, point.latitude, point.longitude).compute_official_sunset(point.tz)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Convert back to "normal" time
|
32
|
+
def convert(point)
|
33
|
+
today = Date.new(year, month, day)
|
34
|
+
new_seconds = seconds + fractional + (minutes + hours * 60) * 60
|
35
|
+
|
36
|
+
# During daylight hours?
|
37
|
+
if hours >= 6 && hours < 18
|
38
|
+
start = NewTime.sunrise(today, point)
|
39
|
+
finish = NewTime.sunset(today, point)
|
40
|
+
new_start_hour = 6
|
41
|
+
else
|
42
|
+
# Is it before sunrise or after sunset?
|
43
|
+
if hours < 6
|
44
|
+
start = NewTime.sunset(today - 1, point)
|
45
|
+
finish = NewTime.sunrise(today, point)
|
46
|
+
new_start_hour = 18 - 24
|
47
|
+
else
|
48
|
+
start = NewTime.sunset(today, point)
|
49
|
+
finish = NewTime.sunrise(today + 1, point)
|
50
|
+
new_start_hour = 18
|
51
|
+
end
|
52
|
+
end
|
53
|
+
start + (new_seconds / (60 * 60) - new_start_hour) * (finish - start) / 12
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.convert(date_time, point)
|
57
|
+
sunrise_today = sunrise(date_time.to_date, point)
|
58
|
+
sunset_today = sunset(date_time.to_date, point)
|
59
|
+
|
60
|
+
# During daylight hours?
|
61
|
+
if date_time >= sunrise_today && date_time < sunset_today
|
62
|
+
start, finish = sunrise_today, sunset_today
|
63
|
+
new_start_hour = 6
|
64
|
+
new_date = date_time.to_date
|
65
|
+
else
|
66
|
+
# Is it before sunrise or after sunset?
|
67
|
+
if date_time < sunrise_today
|
68
|
+
new_date = date_time.to_date - 1
|
69
|
+
else
|
70
|
+
new_date = date_time.to_date
|
71
|
+
end
|
72
|
+
new_start_hour = 18
|
73
|
+
start = sunset(new_date, point)
|
74
|
+
finish = sunrise(new_date + 1, point)
|
75
|
+
end
|
76
|
+
|
77
|
+
new_seconds = (new_start_hour + (date_time - start).to_f / (finish - start) * 12) * 60 * 60
|
78
|
+
|
79
|
+
new_fractional = new_seconds - new_seconds.floor
|
80
|
+
new_seconds = new_seconds.floor
|
81
|
+
new_minutes = new_seconds / 60
|
82
|
+
new_seconds -= new_minutes * 60
|
83
|
+
new_hours = new_minutes / 60
|
84
|
+
new_minutes -= new_hours * 60
|
85
|
+
if new_hours >= 24
|
86
|
+
new_hours -= 24
|
87
|
+
new_date += 1
|
88
|
+
end
|
89
|
+
|
90
|
+
NewTime.new(new_date.year, new_date.month, new_date.day, new_hours, new_minutes, new_seconds, new_fractional)
|
91
|
+
end
|
92
|
+
|
93
|
+
def time_to_s
|
94
|
+
if hours > 12
|
95
|
+
"%i:%02i pm" % [hours - 12, minutes]
|
96
|
+
else
|
97
|
+
"%i:%02i am" % [hours, minutes]
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def date_to_s
|
102
|
+
Date.new(year, month, day).to_s
|
103
|
+
end
|
47
104
|
|
48
|
-
|
49
|
-
|
50
|
-
"%i:%02i pm" % [hours - 12, minutes]
|
51
|
-
else
|
52
|
-
"%i:%02i am" % [hours, minutes]
|
105
|
+
def to_s
|
106
|
+
date_to_s + " " + time_to_s
|
53
107
|
end
|
54
108
|
end
|
55
109
|
end
|
data/new_time.gemspec
CHANGED
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe NewTime do
|
4
|
+
it 'has a version number' do
|
5
|
+
expect(NewTime::VERSION).not_to be nil
|
6
|
+
end
|
7
|
+
|
8
|
+
context "For a fixed place in the northern hemisphere" do
|
9
|
+
let(:point) { NewTime::Point.new(51.829731, -0.860455, "Europe/London") }
|
10
|
+
|
11
|
+
def c(month, hour)
|
12
|
+
NewTime::NewTime.convert(DateTime.new(2015,month,1,hour,0,0,"+0"), point).to_s
|
13
|
+
end
|
14
|
+
|
15
|
+
def i(month, hour)
|
16
|
+
t1 = DateTime.new(2015,month,1,hour,0,0,"+0")
|
17
|
+
t2 = NewTime::NewTime.convert(t1, point).convert(point)
|
18
|
+
expect(t2).to eq t1
|
19
|
+
end
|
20
|
+
|
21
|
+
describe ".convert" do
|
22
|
+
context "2015-01-01" do
|
23
|
+
it { expect(c(1, 0)).to eq "2014-12-31 11:55 pm" }
|
24
|
+
it { expect(c(1, 6)).to eq "2015-01-01 4:23 am" }
|
25
|
+
it { expect(c(1, 12)).to eq "2015-01-01 11:50 am" }
|
26
|
+
it { expect(c(1, 18)).to eq "2015-01-01 7:27 pm" }
|
27
|
+
end
|
28
|
+
|
29
|
+
context "2015-07-01" do
|
30
|
+
it { expect(c(7, 0)).to eq "2015-06-30 11:49 pm" }
|
31
|
+
it { expect(c(7, 6)).to eq "2015-07-01 7:35 am" }
|
32
|
+
it { expect(c(7, 12)).to eq "2015-07-01 11:55 am" }
|
33
|
+
it { expect(c(7, 18)).to eq "2015-07-01 4:15 pm" }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "it should be invertable" do
|
38
|
+
it { i(1, 0) }
|
39
|
+
it { i(1, 6) }
|
40
|
+
it { i(1, 12) }
|
41
|
+
it { i(1, 18) }
|
42
|
+
it { i(7, 0) }
|
43
|
+
it { i(7, 6) }
|
44
|
+
it { i(7, 12) }
|
45
|
+
it { i(7, 18) }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "For a fixed place in the southern hemisphere" do
|
50
|
+
let(:point) { NewTime::Point.new(-33.714955, 150.311407, "Australia/Sydney") }
|
51
|
+
|
52
|
+
def c(month, hour)
|
53
|
+
NewTime::NewTime.convert(DateTime.new(2015,month,1,hour,0,0,"+11"), point).to_s
|
54
|
+
end
|
55
|
+
|
56
|
+
def i(month, hour)
|
57
|
+
t1 = DateTime.new(2015,month,1,hour,0,0,"+11")
|
58
|
+
t2 = NewTime::NewTime.convert(t1, point).convert(point)
|
59
|
+
expect(t2).to eq t1
|
60
|
+
end
|
61
|
+
|
62
|
+
describe ".convert" do
|
63
|
+
context "2015-01-01" do
|
64
|
+
it { expect(c(1, 0)).to eq "2014-12-31 10:43 pm" }
|
65
|
+
it { expect(c(1, 6)).to eq "2015-01-01 6:07 am" }
|
66
|
+
it { expect(c(1, 12)).to eq "2015-01-01 11:08 am" }
|
67
|
+
it { expect(c(1, 18)).to eq "2015-01-01 4:09 pm" }
|
68
|
+
end
|
69
|
+
|
70
|
+
context "2015-07-01" do
|
71
|
+
it { expect(c(7, 0)).to eq "2015-06-30 11:07 pm" }
|
72
|
+
it { expect(c(7, 6)).to eq "2015-07-01 4:14 am" }
|
73
|
+
it { expect(c(7, 12)).to eq "2015-07-01 10:45 am" }
|
74
|
+
it { expect(c(7, 18)).to eq "2015-07-01 6:00 pm" }
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "it should be invertable" do
|
79
|
+
it { i(1, 0) }
|
80
|
+
it { i(1, 6) }
|
81
|
+
it { i(1, 12) }
|
82
|
+
it { i(1, 18) }
|
83
|
+
it { i(7, 0) }
|
84
|
+
it { i(7, 6) }
|
85
|
+
it { i(7, 12) }
|
86
|
+
it { i(7, 18) }
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: new_time
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Landauer
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: RubySunrise
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -70,6 +84,8 @@ files:
|
|
70
84
|
- lib/new_time.rb
|
71
85
|
- lib/new_time/version.rb
|
72
86
|
- new_time.gemspec
|
87
|
+
- spec/new_time_spec.rb
|
88
|
+
- spec/spec_helper.rb
|
73
89
|
homepage: https://github.com/mlandauer/new_time
|
74
90
|
licenses:
|
75
91
|
- MIT
|
@@ -94,4 +110,6 @@ rubygems_version: 2.2.0
|
|
94
110
|
signing_key:
|
95
111
|
specification_version: 4
|
96
112
|
summary: A new kind of time that anchors you to the movement of the sun
|
97
|
-
test_files:
|
113
|
+
test_files:
|
114
|
+
- spec/new_time_spec.rb
|
115
|
+
- spec/spec_helper.rb
|