new_time 0.0.1 → 0.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a86c25b0008c1dadb0688466d715a0b87859fed0
4
- data.tar.gz: ace76a625c8af3981a8d2f14e9fc76ba3647a80e
3
+ metadata.gz: ecf87326a298905488030af21a6fea616fbd52fa
4
+ data.tar.gz: cd7f3bf81f047ab7adcc3b8745dfef7d2e1bdf64
5
5
  SHA512:
6
- metadata.gz: 5925b6b0073a9ad1ea3d113844b38899dbe1e9dfbe52441cb66929f7aa1ebdf3ac7353d74e985938b1e8ebc9e1d6deb7e60f7971570fb3d8f3db0c02bcfb587d
7
- data.tar.gz: 2424df0db905db66038ddc3779a3c6f4f6e56a85aae957ab301e0149c1d77177a465d1059df76643b236d6247b81338af0cebe445127f1d2a16a6afb1d18ae47
6
+ metadata.gz: 9b6e2389d3c4e479eb63cc4a8894e539b41bedf1f322f765ddbcd81fcca84987aa9c61bccca235d0bdd1b3c5a295abe0eca35ee5c4b8ae51483310fa0e699ec6
7
+ data.tar.gz: 563ba3fbb987eabf1b56b12d7176e42825959daa068a222754186d7fa217ee9d0cc3fc1778ccf8952600f7a75a3c49f73e628de87dcf8afa26edfa19b29351e9
data/Rakefile CHANGED
@@ -1,2 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
2
3
 
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/new_time CHANGED
@@ -2,8 +2,6 @@
2
2
 
3
3
  require "new_time"
4
4
 
5
- latitude = -33.714955
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(latitude, longitude, tz)
7
+ puts NewTime::NewTime.current_time(point)
@@ -1,3 +1,3 @@
1
1
  module NewTime
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/new_time.rb CHANGED
@@ -1,55 +1,109 @@
1
1
  require 'solareventcalculator'
2
2
 
3
- class NewTime
4
- attr_accessor :hours, :minutes, :seconds, :fractional
3
+ module NewTime
4
+ class Point
5
+ attr_accessor :latitude, :longitude, :tz
5
6
 
6
- def initialize(hours, minutes, seconds, fractional)
7
- @hours, @minutes, @seconds, @fractional = hours, minutes, seconds, fractional
7
+ def initialize(latitude, longitude, tz)
8
+ @latitude, @longitude, @tz = latitude, longitude, tz
9
+ end
8
10
  end
9
11
 
10
- def self.current_time(latitude, longitude, tz)
11
- convert(DateTime.now, latitude, longitude, tz)
12
- end
12
+ class NewTime
13
+ attr_accessor :year, :month, :day, :hours, :minutes, :seconds, :fractional
13
14
 
14
- def self.convert(date_time, latitude, longitude, tz)
15
- yesterday = SolarEventCalculator.new(date_time.to_date - 1, latitude, longitude)
16
- today = SolarEventCalculator.new(date_time.to_date, latitude, longitude)
17
- tomorrow = SolarEventCalculator.new(date_time.to_date + 1, latitude, longitude)
18
-
19
- sunset_yesterday = yesterday.compute_official_sunset(tz)
20
- sunrise_today = today.compute_official_sunrise(tz)
21
- sunset_today = today.compute_official_sunset(tz)
22
- sunrise_tomorrow = tomorrow.compute_official_sunrise(tz)
23
-
24
- if date_time < sunrise_today
25
- start, finish = sunset_yesterday, sunrise_today
26
- start_hour = 18
27
- elsif date_time < sunset_today
28
- start, finish = sunrise_today, sunset_today
29
- start_hour = 6
30
- else
31
- start, finish = sunset_today, sunrise_tomorrow
32
- start_hour = 18
33
- end
34
-
35
- seconds = (start_hour + (date_time - start).to_f / (finish - start) * 12) * 60 * 60
36
-
37
- fractional = seconds - seconds.floor
38
- seconds = seconds.floor
39
- minutes = seconds / 60
40
- seconds -= minutes * 60
41
- hours = minutes / 60
42
- minutes -= hours * 60
43
- hours -= 24 if hours >= 24
44
-
45
- NewTime.new(hours, minutes, seconds, fractional)
46
- end
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
- def to_s
49
- if hours > 12
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
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.5"
22
22
  spec.add_development_dependency "rake", '~> 0'
23
+ spec.add_development_dependency "rspec"
23
24
 
24
25
  spec.add_dependency "RubySunrise", '~> 0'
25
26
  end
@@ -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
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'new_time'
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.1
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