glitch-time 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in glitch-time.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ raGEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.6.4)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.9.2)
10
+ rcov (0.9.10)
11
+ rdoc (3.9.4)
12
+ shoulda (2.11.3)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ bundler (~> 1.0.0)
19
+ jeweler (~> 1.6.4)
20
+ rcov
21
+ rdoc
22
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Simon Peck
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = glitch-time
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to glitch-time
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Simon Peck. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "glitch-time/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "glitch-time"
7
+ s.version = Glitch::Time::VERSION
8
+ s.authors = ["Simon Peck"]
9
+ s.email = ""
10
+ s.homepage = ""
11
+ s.summary = %q{This is a port of the Glitch time converter.}
12
+ s.description = %q{The glitch time and date specification is here: http://api.glitch.com/docs/time/}
13
+
14
+ s.rubyforge_project = "glitch-time"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
@@ -0,0 +1,7 @@
1
+ require "glitch-time/version"
2
+
3
+ module Glitch
4
+ module Time
5
+ # Your code goes here...
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module Glitch
2
+ module Time
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,158 @@
1
+ #
2
+ # Converts real time to Glitch Time
3
+ # In the world of Glitch a game day is four real hours long.
4
+ #
5
+ # There are 308 days in a glitch year.
6
+ # There are 24 hours in a day.
7
+ # There are 60 minutes in an hour.
8
+ # There are 4435200 real seconds in a game year.
9
+ # There are 14400 real seconds in a game day.
10
+ # There are 600 real seconds in a game hour.
11
+ # There are 10 real seconds in a game minute.
12
+ #
13
+ # The Glitch epoch began at 1238562000 Unix time.
14
+
15
+ class GlitchTime
16
+ GAME_EPOCH = 1238562000
17
+ DAYS_IN_YEAR = 308
18
+ HOURS_IN_HALF_DAY = 12
19
+ SECONDS_IN_GAME_YEAR = 4435200
20
+ SECONDS_IN_GAME_DAY = 14400
21
+ SECONDS_IN_GAME_HOUR = 600
22
+ SECONDS_IN_GAME_MINUTE = 10
23
+ MONTHS = ['Primuary',
24
+ 'Spork',
25
+ 'Bruise',
26
+ 'Candy',
27
+ 'Fever',
28
+ 'Junuary',
29
+ 'Septa',
30
+ 'Remember',
31
+ 'Doom',
32
+ 'Widdershins',
33
+ 'Eleventy',
34
+ 'Recurse']
35
+
36
+ DAYS = ['Hairday', 'Moonday', 'Twoday', 'Weddingday', 'Theday', 'Fryday', 'Standay', 'Fabday']
37
+
38
+ attr_reader :real_now
39
+
40
+ def initialize(seconds_since_epoch = nil)
41
+ seconds_since_epoch ||= Time.now.to_i
42
+ @real_now = seconds_since_epoch
43
+ @year_offset = (year * SECONDS_IN_GAME_YEAR)
44
+ @day_of_year_offset = (day_of_year * SECONDS_IN_GAME_DAY)
45
+ @hour_offset = (hour * SECONDS_IN_GAME_HOUR)
46
+ @minute_offset = (minute * SECONDS_IN_GAME_MINUTE)
47
+ end
48
+
49
+ def now
50
+ @real_now - GAME_EPOCH;
51
+ end
52
+
53
+ def year
54
+ (now / SECONDS_IN_GAME_YEAR).floor
55
+ end
56
+
57
+ def day_of_year
58
+ offset = now - @year_offset
59
+ (offset / SECONDS_IN_GAME_DAY).floor
60
+ end
61
+
62
+ def hour
63
+ offset = now - @year_offset - @day_of_year_offset
64
+ (offset / SECONDS_IN_GAME_HOUR).floor
65
+ end
66
+
67
+ def standard_hour
68
+ current_hour = hour == 0 ? HOURS_IN_HALF_DAY : hour
69
+ current_hour > HOURS_IN_HALF_DAY ? current_hour -= HOURS_IN_HALF_DAY : current_hour
70
+ end
71
+
72
+ def meridian_indicator
73
+ current_hour = hour
74
+ current_hour < HOURS_IN_HALF_DAY ? 'am' : 'pm'
75
+ end
76
+
77
+ def minute
78
+ offset = now - @year_offset - @day_of_year_offset - @hour_offset
79
+ (offset / SECONDS_IN_GAME_MINUTE).floor
80
+ end
81
+
82
+ def pretty_minute
83
+ "%02d" % minute
84
+ end
85
+
86
+ def second
87
+ now - @year_offset - @day_of_year_offset - @hour_offset - @minute_offset
88
+ end
89
+
90
+ def weekdays_since_epoch
91
+ # This does not include the annual day of recurse, since it is not a weekday.
92
+ day_of_year + ((DAYS_IN_YEAR - 1) * year)
93
+ end
94
+
95
+ def day_of_week
96
+ return nil if month_of_year == 11
97
+ weekdays_since_epoch.modulo 8;
98
+ end
99
+
100
+ def day_of_month
101
+ day_to_month_day(day_of_year)[1]
102
+ end
103
+
104
+ def month_of_year
105
+ day_to_month_day(day_of_year)[0] - 1
106
+ end
107
+
108
+ # Converts an absolute day to month, day pair
109
+ def day_to_month_day incremental_day
110
+ month_lengths = [29, 3, 53, 17, 73, 19, 13, 37, 5, 47, 11, 1]
111
+ current_day = 0
112
+ month_lengths.each_with_index do |month, index|
113
+ current_day += month
114
+ if (current_day > incremental_day)
115
+ m = index + 1
116
+ d = incremental_day + 1 - (current_day - month)
117
+ return [m, d]
118
+ end
119
+ end
120
+ [0, 0]
121
+ end
122
+
123
+ def day_name
124
+ return nil if month_of_year == 11
125
+ DAYS[day_of_week]
126
+ end
127
+
128
+ def month_name
129
+ MONTHS[month_of_year]
130
+ end
131
+
132
+ def suffix value = day_of_month
133
+ case value
134
+ when 0
135
+ 'th'
136
+ when 1
137
+ 'st'
138
+ when 2
139
+ 'nd'
140
+ when 3
141
+ 'rd'
142
+ else
143
+ 'th'
144
+ end
145
+ end
146
+
147
+ def time_and_date
148
+ if month_of_year == 11
149
+ "#{standard_hour}:#{pretty_minute} #{meridian_indicator}, #{month_name}, year #{year}"
150
+ else
151
+ "#{standard_hour}:#{pretty_minute} #{meridian_indicator}, #{day_name} #{day_of_month}#{suffix} of #{month_name}, year #{year}"
152
+ end
153
+ end
154
+
155
+ def verbose_time_and_date
156
+ "Right at this very moment, it's #{time_and_date}"
157
+ end
158
+ end
@@ -0,0 +1,202 @@
1
+ require "test/unit"
2
+ require File.join('', File.dirname(__FILE__), '..', 'lib', 'glitch_time')
3
+
4
+ class GlitchTimeTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @now = Time.now.to_i
8
+ end
9
+
10
+ def test_now
11
+ expected = @now - 1238562000
12
+ actual = GlitchTime.new.now
13
+
14
+ puts "now: #{actual}, #{expected}"
15
+
16
+ assert_equal(expected, actual, "Current time is incorrect.")
17
+ end
18
+
19
+ def test_year
20
+ expected = (@now - 1238562000) / 4435200
21
+ actual = GlitchTime.new.year
22
+ assert_equal(expected, actual, "Current year is incorrect.")
23
+
24
+ actual = GlitchTime.new(1315381646).year
25
+ assert_equal(expected, actual, "Current year is incorrect.")
26
+ end
27
+
28
+ def test_day_of_year
29
+ glitch_time = GlitchTime.new 1315381646
30
+ expected = 98
31
+ actual = glitch_time.day_of_year
32
+
33
+ assert_equal(expected, actual)
34
+ end
35
+
36
+ def test_hour
37
+ glitch_time = GlitchTime.new 1315381646
38
+ expected = 16
39
+ actual = glitch_time.hour
40
+
41
+ assert_equal(expected, actual)
42
+ end
43
+
44
+ def test_meridian_indicator
45
+ glitch_time = GlitchTime.new 1315381646
46
+ expected = 'pm'
47
+ actual = glitch_time.meridian_indicator
48
+
49
+ assert_equal(expected, actual)
50
+ end
51
+
52
+ def test_minute
53
+ glitch_time = GlitchTime.new 1315381646
54
+ expected = 44
55
+ actual = glitch_time.minute
56
+
57
+ assert_equal(expected, actual)
58
+ end
59
+
60
+ def test_pretty_minute
61
+ glitch_time = GlitchTime.new 1315381846
62
+ expected = '04'
63
+ actual = glitch_time.pretty_minute
64
+
65
+ assert_equal(expected, actual)
66
+ end
67
+
68
+ def test_second
69
+ glitch_time = GlitchTime.new 1315381646
70
+ expected = 6
71
+ actual = glitch_time.second
72
+
73
+ assert_equal(expected, actual)
74
+ end
75
+
76
+ def test_glitch_time
77
+ glitch_time = GlitchTime.new
78
+ sec = glitch_time.real_now - 1238562000
79
+ year = (sec / 4435200).floor
80
+ assert_equal(year, glitch_time.year, "Year is incorrect.")
81
+
82
+ sec -= year * 4435200
83
+ day_of_year = (sec / 14400).floor
84
+ assert_equal(day_of_year, glitch_time.day_of_year, "Day of year is incorrect.")
85
+
86
+ sec -= day_of_year * 14400
87
+ hour = (sec / 600).floor
88
+ assert_equal(hour, glitch_time.hour, "Hour is incorrect.")
89
+
90
+ sec -= hour * 600
91
+ minute = (sec / 10).floor
92
+ assert_equal(minute, glitch_time.minute, "Minute is incorrect.")
93
+
94
+ sec -= minute * 10
95
+ assert_equal(sec, glitch_time.second, "Seconds are incorrect.")
96
+ end
97
+
98
+
99
+ def test_weekdays_since_epoch
100
+ glitch_time = GlitchTime.new
101
+ years_since_epoch = glitch_time.now / 4435200
102
+
103
+ # The day of Recurse does not count as a weekday.
104
+ expected = (glitch_time.now / 14400) - years_since_epoch
105
+
106
+ assert_equal(expected, glitch_time.weekdays_since_epoch, "Days since epoch is incorrect.")
107
+ end
108
+
109
+ def test_day_to_month_day
110
+ glitch_time = GlitchTime.new
111
+
112
+ puts "Today is #{glitch_time.month_of_year}/#{glitch_time.day_of_month}!"
113
+
114
+ third_of_primuary = [1, 3]
115
+ actual = glitch_time.day_to_month_day(2)
116
+ assert_equal(third_of_primuary, actual, "Third of primuary is wrong.")
117
+
118
+ first_of_spork = [2, 1]
119
+ actual = glitch_time.day_to_month_day(29)
120
+ assert_equal(first_of_spork, actual, "First of spork is wrong.")
121
+
122
+ fifty_third_of_bruise = [3, 53]
123
+ actual = glitch_time.day_to_month_day(84)
124
+ assert_equal(fifty_third_of_bruise, actual, "Fifty-third of bruise is wrong.")
125
+ end
126
+
127
+ def test_suffix
128
+ glitch_time = GlitchTime.new
129
+
130
+ expected = 'th'
131
+ actual = glitch_time.suffix 0
132
+ assert_equal(expected, actual, "0 failed suffix test")
133
+
134
+ expected = 'st'
135
+ actual = glitch_time.suffix 1
136
+ assert_equal(expected, actual, "1 failed suffix test")
137
+
138
+ expected = 'nd'
139
+ actual = glitch_time.suffix 2
140
+ assert_equal(expected, actual, "2 failed suffix test")
141
+
142
+ expected = 'rd'
143
+ actual = glitch_time.suffix 3
144
+ assert_equal(expected, actual, "3 failed suffix test")
145
+
146
+ expected = 'th'
147
+ actual = glitch_time.suffix 4
148
+ assert_equal(expected, actual, "4 failed suffix test")
149
+
150
+ expected = 'th'
151
+ actual = glitch_time.suffix 5
152
+ assert_equal(expected, actual, "5 failed suffix test")
153
+
154
+ expected = 'th'
155
+ actual = glitch_time.suffix 6
156
+ assert_equal(expected, actual, "6 failed suffix test")
157
+
158
+ expected = 'th'
159
+ actual = glitch_time.suffix 7
160
+ assert_equal(expected, actual, "7 failed suffix test")
161
+
162
+ expected = 'th'
163
+ actual = glitch_time.suffix 8
164
+ assert_equal(expected, actual, "8 failed suffix test")
165
+
166
+ expected = 'th'
167
+ actual = glitch_time.suffix 9
168
+ assert_equal(expected, actual, "9 failed suffix test")
169
+
170
+ expected = 'th'
171
+ actual = glitch_time.suffix 10
172
+ assert_equal(expected, actual, "10 failed suffix test")
173
+ end
174
+
175
+ def test_pretty_printing
176
+ glitch_time = GlitchTime.new 1315381646
177
+ actual = glitch_time.verbose_time_and_date
178
+ expected = "Right at this very moment, it's 4:44 pm, Fryday 14th of Candy, year 17"
179
+ puts actual
180
+
181
+ assert_equal(expected, actual)
182
+ end
183
+
184
+ def test_time_and_date_for_recurse
185
+ glitch_time = GlitchTime.new 1313946861
186
+ actual = glitch_time.time_and_date
187
+ expected = "1:26 am, Recurse, year 16"
188
+ puts actual
189
+
190
+ assert_equal(expected, actual)
191
+ end
192
+
193
+ def test_day_name_for_recurse
194
+ glitch_time = GlitchTime.new 1313946861
195
+ assert_equal(nil, glitch_time.day_name)
196
+ end
197
+
198
+ def test_day_of_week_for_recurse
199
+ glitch_time = GlitchTime.new 1313946861
200
+ assert_equal(nil, glitch_time.day_of_week)
201
+ end
202
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: glitch-time
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Simon Peck
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-14 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: ! 'The glitch time and date specification is here: http://api.glitch.com/docs/time/'
15
+ email: ''
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - .document
21
+ - .gitignore
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - LICENSE.txt
25
+ - README.rdoc
26
+ - Rakefile
27
+ - VERSION
28
+ - glitch-time.gemspec
29
+ - lib/glitch-time.rb
30
+ - lib/glitch-time/version.rb
31
+ - lib/glitch_time.rb
32
+ - test/glitch_time_test.rb
33
+ homepage: ''
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project: glitch-time
53
+ rubygems_version: 1.8.6
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: This is a port of the Glitch time converter.
57
+ test_files:
58
+ - test/glitch_time_test.rb