wtf 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/CHANGELOG +2 -0
- data/LICENSE +21 -0
- data/README +17 -0
- data/Rakefile +48 -0
- data/lib/wtf.rb +288 -0
- data/test/test_helper.rb +3 -0
- data/test/wtf_test.rb +138 -0
- metadata +63 -0
data/CHANGELOG
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2009 Wyatt M. Greene
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= World Time Format Converter
|
2
|
+
|
3
|
+
== Description
|
4
|
+
|
5
|
+
The WTF gem provides a Ruby class to convert between standard
|
6
|
+
time and World Time Format. For more information, visit
|
7
|
+
http://www.worldtimeformat.com
|
8
|
+
|
9
|
+
== Examples
|
10
|
+
|
11
|
+
WTF::Date.new("FIWIT:NAAAA").as_utc # converts a WTF datetime to a Ruby Time object
|
12
|
+
WTF::Date.new(time).as_wtf # converts from a Ruby Time object to a WTF string
|
13
|
+
WTF::Date.now.as_wtf # returns a WTF string representing the current datetime
|
14
|
+
WTF::Date.now.date_part # returns a WTF string representing the current date
|
15
|
+
WTF::Date.now.time_part # returns a WTF string representing the current time
|
16
|
+
WTF::Date.convert("FIWIT:NAAAA") # convenience method for converting
|
17
|
+
WTF::Date.convert(time) # convenience method for converting
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test WTF.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for WTF.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'WTF'
|
19
|
+
rdoc.rdoc_files.include('README')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
begin
|
24
|
+
require 'jeweler'
|
25
|
+
Jeweler::Tasks.new do |s|
|
26
|
+
s.name = "wtf"
|
27
|
+
s.version = "0.0.1"
|
28
|
+
s.author = "Wyatt Greene"
|
29
|
+
s.email = "techiferous@gmail.com"
|
30
|
+
s.summary = "Converts between World Time Format and standard dates and times."
|
31
|
+
s.description = %Q{
|
32
|
+
The WTF gem provides a Ruby class to convert between standard
|
33
|
+
time and World Time Format. For more information, visit
|
34
|
+
http://www.worldtimeformat.com
|
35
|
+
}
|
36
|
+
s.require_path = "lib"
|
37
|
+
s.files = ["lib/wtf.rb", "LICENSE", "Rakefile", "README",
|
38
|
+
"CHANGELOG",
|
39
|
+
"test/wtf_test.rb", "test/test_helper.rb"]
|
40
|
+
s.homepage = "http://github.com/techiferous/wtf"
|
41
|
+
s.requirements << "none"
|
42
|
+
s.has_rdoc = true
|
43
|
+
s.test_files = Dir.glob("test/**/*.rb")
|
44
|
+
end
|
45
|
+
Jeweler::GemcutterTasks.new
|
46
|
+
rescue LoadError
|
47
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
48
|
+
end
|
data/lib/wtf.rb
ADDED
@@ -0,0 +1,288 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
module WTF
|
4
|
+
class Date
|
5
|
+
|
6
|
+
# creates a new WTF::Date object. The argument can be a string representing the
|
7
|
+
# date and/or time in World Time Format, or it can be a Time object. If the
|
8
|
+
# argument is missing, Time.now is assumed.
|
9
|
+
#
|
10
|
+
# Examples:
|
11
|
+
#
|
12
|
+
# WTF::Date.new(":NB") #=> returns a new WTF::Date object for the WTF time :NB
|
13
|
+
# WTF::Date.new(":RJA") #=> returns a new WTF::Date object for the WTF date :RJA
|
14
|
+
# WTF::Date.new("MM:BRT") #=> returns a new WTF::Date object for the WTF datetime MM:BRT
|
15
|
+
# WTF::Date.new #=> returns a new WTF::Date object corresponding to right now
|
16
|
+
# WTF::Date.new(Time.utc(2010, 5, 6)) #=> returns a new WTF::Date object for the given Time
|
17
|
+
#
|
18
|
+
# If the argument is in World Time Format, it must can contain a date part,
|
19
|
+
# a time part, and a colon to separate them. Here are some examples of valid formats:
|
20
|
+
#
|
21
|
+
# - "FJKRM:BROMQ" -- This is a fully specified World Time Format date and
|
22
|
+
# time. The first part is the date part and the second part is the time
|
23
|
+
# part. The date part cannot be longer than five characters. The time
|
24
|
+
# part can be longer than five characters, but any characters after
|
25
|
+
# the fifth character are ignored when calculating.
|
26
|
+
# - ":BROMQ" -- You can leave out the date part. Today is assumed.
|
27
|
+
# - "FJKRM:" -- You can leave out the time part. The beginning of the
|
28
|
+
# Julian day is assumed (which is noon).
|
29
|
+
# - "BROMQ" -- If you leave out the colon, it is assumed that you are
|
30
|
+
# giving the time, not the date.
|
31
|
+
# - "RM:BROMQ" -- You can leave out some of the digits for the date. If
|
32
|
+
# you do, the remaining digits will be filled in according to today's
|
33
|
+
# date. If today's date is FMBAZ, then "RM:BROMQ" becomes
|
34
|
+
# "FMBRM:BROMQ".
|
35
|
+
# - ":BR" -- You don't have to specify all five of the time digits.
|
36
|
+
# - "A:B" -- This is a valid format.
|
37
|
+
#
|
38
|
+
# Also note:
|
39
|
+
#
|
40
|
+
# - The date conversion does not work before the Gregorian calendar change
|
41
|
+
# that happened in October 1582.
|
42
|
+
# - Since the date part is limited to five characters, there is an upper
|
43
|
+
# bound for how far into the future you can do a date conversion.
|
44
|
+
# - The time part is limited to five characters and therefore the
|
45
|
+
# time precision is limited to about 10 milliseconds.
|
46
|
+
# - Leap seconds were not taken into account.
|
47
|
+
#
|
48
|
+
def initialize(time = nil)
|
49
|
+
if time
|
50
|
+
if time.is_a? String
|
51
|
+
@time = convert_from_wtf(time)
|
52
|
+
elsif time.is_a? Time
|
53
|
+
@time = time
|
54
|
+
else
|
55
|
+
raise ArgumentError.new("Argument must be a String or a Time.")
|
56
|
+
end
|
57
|
+
else
|
58
|
+
@time = ::Time.now
|
59
|
+
end
|
60
|
+
@wtf = convert_to_wtf(@time)
|
61
|
+
end
|
62
|
+
|
63
|
+
# now is a synonym for WTF::Date.new. It returns a WTF::Date object initialized with
|
64
|
+
# the current date and time.
|
65
|
+
#
|
66
|
+
def self.now
|
67
|
+
self.new
|
68
|
+
end
|
69
|
+
|
70
|
+
# convert is a convenience method to make it easier to convert between WTF
|
71
|
+
# times and standard times.
|
72
|
+
#
|
73
|
+
# Examples:
|
74
|
+
#
|
75
|
+
# WTF::Date.convert("FIWIT:NAAAA") #=> same as WTF::Date.new("FIWIT:NAAAA").as_utc
|
76
|
+
# WTF::Date.convert(time) #=> same as WTF::Date.new(time).as_wtf
|
77
|
+
#
|
78
|
+
def self.convert(arg)
|
79
|
+
return nil if arg.nil?
|
80
|
+
return self.new(arg).as_utc if arg.is_a?(String)
|
81
|
+
return self.new(arg).as_wtf if arg.is_a?(Time)
|
82
|
+
raise ArgumentError.new("Argument must be a String or a Time.")
|
83
|
+
end
|
84
|
+
|
85
|
+
# returns a Time object representing the WTF::Date's date and time in UTC.
|
86
|
+
#
|
87
|
+
# Examples:
|
88
|
+
#
|
89
|
+
# standard_time = WTF::Date.new("FIWIT:NAAAA").as_utc
|
90
|
+
# standard_time = WTF::Date.new(":BJR").as_utc
|
91
|
+
# standard_time = WTF::Date.new("XQ:ARM").as_utc
|
92
|
+
#
|
93
|
+
def as_utc
|
94
|
+
@time
|
95
|
+
end
|
96
|
+
|
97
|
+
# returns a string representing the date and time in World Time Format.
|
98
|
+
#
|
99
|
+
# Example:
|
100
|
+
#
|
101
|
+
# wtf_date = WTF::Date.new(Time.utc(2002, 7, 10, 13, 55, 1, 777000))
|
102
|
+
# wtf_date.as_wtf #=> "FJNXQ:CCAAA"
|
103
|
+
#
|
104
|
+
def as_wtf
|
105
|
+
@wtf
|
106
|
+
end
|
107
|
+
|
108
|
+
# returns just the World Time Format date.
|
109
|
+
#
|
110
|
+
# Example:
|
111
|
+
#
|
112
|
+
# wtf_date = WTF::Date.new(Time.utc(2002, 7, 10, 13, 55, 1, 777000))
|
113
|
+
# wtf_date.date_part #=> "FJNXQ"
|
114
|
+
#
|
115
|
+
def date_part
|
116
|
+
@wtf =~ /^([A-Z]{0,5}):([A-Z]*)$/
|
117
|
+
$1
|
118
|
+
end
|
119
|
+
|
120
|
+
# returns just the World Time Format time.
|
121
|
+
#
|
122
|
+
# Example:
|
123
|
+
#
|
124
|
+
# wtf_date = WTF::Date.new(Time.utc(2002, 7, 10, 13, 55, 1, 777000))
|
125
|
+
# wtf_date.time_part #=> "CCAAA"
|
126
|
+
#
|
127
|
+
def time_part
|
128
|
+
@wtf =~ /^([A-Z]{0,5}):([A-Z]*)$/
|
129
|
+
$2
|
130
|
+
end
|
131
|
+
|
132
|
+
#--------------------#
|
133
|
+
# PRIVATE METHODS #
|
134
|
+
#++
|
135
|
+
|
136
|
+
private
|
137
|
+
|
138
|
+
SECONDS_IN_A_DAY = 86400
|
139
|
+
MILLIS_IN_A_DAY = SECONDS_IN_A_DAY * 1000 # millis means milliseconds
|
140
|
+
|
141
|
+
def convert_from_wtf(wtf)
|
142
|
+
|
143
|
+
raise ArgumentError.new("Argument is empty.") if wtf.nil? || wtf.empty?
|
144
|
+
|
145
|
+
# If no colon is given, we assume we have a time, not a date.
|
146
|
+
if !wtf.include?(":")
|
147
|
+
wtf = ":" + wtf
|
148
|
+
end
|
149
|
+
|
150
|
+
if wtf !~ /^([A-Z]{0,5}):([A-Z]*)$/
|
151
|
+
raise ArgumentError.new("Time format error")
|
152
|
+
end
|
153
|
+
wtf_date = $1
|
154
|
+
wtf_time = $2
|
155
|
+
|
156
|
+
# we need to fill in ambiguous dates. If today is FJSTR: and the given
|
157
|
+
# date is BM:, we fill in the blanks according to today's date
|
158
|
+
# like so: FJSBM:
|
159
|
+
if wtf_date.length < 5
|
160
|
+
reference_date = self.class.now.date_part
|
161
|
+
wtf_date = reference_date[0, 5-wtf_date.length] + wtf_date
|
162
|
+
end
|
163
|
+
|
164
|
+
# we don't care about anything more precise than 5 time digits
|
165
|
+
if wtf_time.length > 5
|
166
|
+
wtf_time = wtf_time[0, 5]
|
167
|
+
end
|
168
|
+
|
169
|
+
julian_date = 0
|
170
|
+
# compute integer part of Julian Date
|
171
|
+
for i in (0..4) do
|
172
|
+
julian_date += (wtf_date[i]-65) * (26**(4-i))
|
173
|
+
end
|
174
|
+
# compute fractional part of Julian Date
|
175
|
+
fractional = 0
|
176
|
+
for i in (0..(wtf_time.length-1)) do
|
177
|
+
fractional += (wtf_time[i]-65) / (26**(i+1)).to_f
|
178
|
+
end
|
179
|
+
|
180
|
+
# adjust for the astronomical Julian day, which starts at noon
|
181
|
+
if fractional >= 0.5
|
182
|
+
fractional -= 0.5
|
183
|
+
julian_date += 1
|
184
|
+
else
|
185
|
+
fractional += 0.5
|
186
|
+
end
|
187
|
+
|
188
|
+
date = ::Date.jd(julian_date+fractional)
|
189
|
+
|
190
|
+
# Normally I love Ruby and how it gets out of my way, but this is
|
191
|
+
# absolutely ridiculous. I expect to be able to create a Date object,
|
192
|
+
# initialize it with the Julian Day, then simply call to_time.
|
193
|
+
# Why Ruby doesn't provide a to_time method, I don't know. So I'm resorting
|
194
|
+
# to calling private (!) methods on the Date class. Shame on Ruby
|
195
|
+
# and shame on me! :)
|
196
|
+
fraction_of_seconds = date.send(:sec_fraction) * SECONDS_IN_A_DAY
|
197
|
+
microseconds = fraction_of_seconds * 1000000
|
198
|
+
utc_time = ::Time.utc(date.year,
|
199
|
+
date.month,
|
200
|
+
date.day,
|
201
|
+
date.send(:hour),
|
202
|
+
date.send(:min),
|
203
|
+
date.send(:sec),
|
204
|
+
microseconds)
|
205
|
+
|
206
|
+
utc_time.getlocal
|
207
|
+
|
208
|
+
end
|
209
|
+
|
210
|
+
def convert_to_wtf(time)
|
211
|
+
utc = time.utc
|
212
|
+
date = ::Date.civil(utc.year, utc.month, utc.day)
|
213
|
+
julian_day = date.jd.to_i
|
214
|
+
# adjust for the astronomical Julian day, which starts at noon
|
215
|
+
if utc.hour >= 12
|
216
|
+
hour = utc.hour - 12
|
217
|
+
else
|
218
|
+
hour = utc.hour + 12
|
219
|
+
julian_day -= 1
|
220
|
+
end
|
221
|
+
fractional = hour * 3600 + utc.min * 60 + utc.sec + (utc.usec / 1000000.0)
|
222
|
+
date_part = self.class.send(:decimal_to_alphabase, julian_day)
|
223
|
+
time_part = self.class.send(:time_to_wtf, fractional*1000)
|
224
|
+
date_part + ':' + time_part
|
225
|
+
end
|
226
|
+
|
227
|
+
# Given a number in base 10, return a number in alphabase. Note that
|
228
|
+
# alphabase is different than base 26. Base 26 uses digits within the range
|
229
|
+
# 0-9,a-p. Alphabase is also based on a radix of 26, but uses digits within
|
230
|
+
# the range A-Z.
|
231
|
+
#
|
232
|
+
# To simplify the algorithm, this function does not accept real numbers as
|
233
|
+
# input, only integers.
|
234
|
+
#
|
235
|
+
def self.decimal_to_alphabase(decimal)
|
236
|
+
if (decimal.floor != decimal)
|
237
|
+
raise ArgumentError.new("Floats are not supported.")
|
238
|
+
end
|
239
|
+
alphabase = ""
|
240
|
+
base26 = decimal.to_s(26)
|
241
|
+
for i in (0..(base26.length-1))
|
242
|
+
c = base26[i]
|
243
|
+
if (c == 45) # hyphen for negative numbers
|
244
|
+
alphabase += "-"
|
245
|
+
elsif ((c >= 97) && (c <= 112)) # lower case a-p -> K-Z
|
246
|
+
alphabase += (c-22).chr
|
247
|
+
elsif ((c >= 48) && (c <= 57)) # number 0-9 -> A-J
|
248
|
+
alphabase += (c+17).chr
|
249
|
+
else
|
250
|
+
raise "Unexpected character."
|
251
|
+
end
|
252
|
+
end
|
253
|
+
alphabase
|
254
|
+
end
|
255
|
+
private_class_method :decimal_to_alphabase
|
256
|
+
|
257
|
+
# Given an integer representing the number of milliseconds into the Julian
|
258
|
+
# day, return a five-character string representing the time in World Time
|
259
|
+
# Format.
|
260
|
+
#
|
261
|
+
def self.time_to_wtf(millis_into_the_day)
|
262
|
+
if (millis_into_the_day < 0)
|
263
|
+
raise ArgumentError.new("Negative values are not supported.")
|
264
|
+
end
|
265
|
+
if (millis_into_the_day >= MILLIS_IN_A_DAY)
|
266
|
+
message = "Value (#{millis_into_the_day}) must be smaller than"
|
267
|
+
message << " the number of milliseconds in a day (#{MILLIS_IN_A_DAY})."
|
268
|
+
raise ArgumentError.new(message)
|
269
|
+
end
|
270
|
+
result = ""
|
271
|
+
# For the first loop iteration, the unit represents the number of
|
272
|
+
# milliseconds in an entire day. Then it represents the number of
|
273
|
+
# milliseconds in an alphabetic hour, then in an alphabetic minute, all
|
274
|
+
# the way down to the number of milliseconds in an alphabetic subsubsecond.
|
275
|
+
unit = MILLIS_IN_A_DAY
|
276
|
+
remainder = millis_into_the_day # milliseconds left to process
|
277
|
+
5.times do
|
278
|
+
unit /= 26.0
|
279
|
+
num_units = (remainder.floor / unit).to_i
|
280
|
+
remainder -= (num_units * unit)
|
281
|
+
result += (num_units.to_i+65).chr
|
282
|
+
end
|
283
|
+
result
|
284
|
+
end
|
285
|
+
private_class_method :time_to_wtf
|
286
|
+
|
287
|
+
end
|
288
|
+
end
|
data/test/test_helper.rb
ADDED
data/test/wtf_test.rb
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
require 'test/test_helper'
|
2
|
+
require 'wtf'
|
3
|
+
|
4
|
+
class WtfTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
#--------------------------#
|
7
|
+
# Testing private methods. #
|
8
|
+
#--------------------------#
|
9
|
+
|
10
|
+
def test_alphabase_conversion_with_whole_numbers
|
11
|
+
assert_equal "A", WTF::Date.send(:decimal_to_alphabase, 0)
|
12
|
+
assert_equal "B", WTF::Date.send(:decimal_to_alphabase, 1)
|
13
|
+
assert_equal "C", WTF::Date.send(:decimal_to_alphabase, 2)
|
14
|
+
assert_equal "J", WTF::Date.send(:decimal_to_alphabase, 9)
|
15
|
+
assert_equal "Z", WTF::Date.send(:decimal_to_alphabase, 25)
|
16
|
+
assert_equal "BA", WTF::Date.send(:decimal_to_alphabase, 26)
|
17
|
+
assert_equal "BB", WTF::Date.send(:decimal_to_alphabase, 27)
|
18
|
+
assert_equal "BAA", WTF::Date.send(:decimal_to_alphabase, 676)
|
19
|
+
assert_equal "BAB", WTF::Date.send(:decimal_to_alphabase, 677)
|
20
|
+
assert_equal "BAC", WTF::Date.send(:decimal_to_alphabase, 678)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_alphabase_conversion_with_negative_numbers
|
24
|
+
assert_equal "-B", WTF::Date.send(:decimal_to_alphabase, -1)
|
25
|
+
assert_equal "-C", WTF::Date.send(:decimal_to_alphabase, -2)
|
26
|
+
assert_equal "-BB", WTF::Date.send(:decimal_to_alphabase, -27)
|
27
|
+
assert_equal "-BAA", WTF::Date.send(:decimal_to_alphabase, -676)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_alphabase_conversion_with_floats
|
31
|
+
assert_raise ArgumentError do
|
32
|
+
WTF::Date.send(:decimal_to_alphabase, 7.7)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_time_to_wtf
|
37
|
+
assert_equal "AAAAA", WTF::Date.send(:time_to_wtf, 0) # midnight
|
38
|
+
assert_equal "NAAAA", WTF::Date.send(:time_to_wtf, 12*60*60*1000) # noon
|
39
|
+
assert_equal "BAAAA", WTF::Date.send(:time_to_wtf, 3323077)
|
40
|
+
end
|
41
|
+
|
42
|
+
#---------------------#
|
43
|
+
# Testing public API. #
|
44
|
+
#---------------------#
|
45
|
+
|
46
|
+
def test_converting_to_wtf
|
47
|
+
|
48
|
+
time = Time.utc(1970, 1, 1, 0)
|
49
|
+
wtf = WTF::Date.new(time)
|
50
|
+
assert_equal "FIWIT", wtf.date_part
|
51
|
+
assert_equal "NAAAA", wtf.time_part
|
52
|
+
assert_equal "FIWIT:NAAAA", wtf.as_wtf
|
53
|
+
|
54
|
+
time = Time.utc(1969, 12, 31, 12)
|
55
|
+
wtf = WTF::Date.new(time)
|
56
|
+
assert_equal "FIWIT", wtf.date_part
|
57
|
+
assert_equal "AAAAA", wtf.time_part
|
58
|
+
assert_equal "FIWIT:AAAAA", wtf.as_wtf
|
59
|
+
|
60
|
+
time = Time.utc(2002, 7, 10, 12)
|
61
|
+
wtf = WTF::Date.new(time)
|
62
|
+
assert_equal "FJNXQ", wtf.date_part
|
63
|
+
assert_equal "AAAAA", wtf.time_part
|
64
|
+
assert_equal "FJNXQ:AAAAA", wtf.as_wtf
|
65
|
+
|
66
|
+
time = Time.utc(2002, 7, 10, 13, 55, 1, 777000)
|
67
|
+
wtf = WTF::Date.new(time)
|
68
|
+
assert_equal "FJNXQ", wtf.date_part
|
69
|
+
assert_equal "CCAAA", wtf.time_part
|
70
|
+
assert_equal "FJNXQ:CCAAA", wtf.as_wtf
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_converting_to_standard_time
|
75
|
+
time = Time.utc(1970, 1, 1, 0)
|
76
|
+
assert_equal time, WTF::Date.new("FIWIT:NAAAA").as_utc
|
77
|
+
assert_equal time, WTF::Date.new("FIWIT:NAAA").as_utc
|
78
|
+
assert_equal time, WTF::Date.new("FIWIT:NAA").as_utc
|
79
|
+
assert_equal time, WTF::Date.new("FIWIT:NA").as_utc
|
80
|
+
assert_equal time, WTF::Date.new("FIWIT:N").as_utc
|
81
|
+
time = Time.utc(1969, 12, 31, 12)
|
82
|
+
assert_equal time, WTF::Date.new("FIWIT:AAAAA").as_utc
|
83
|
+
assert_equal time, WTF::Date.new("FIWIT:AAAA").as_utc
|
84
|
+
assert_equal time, WTF::Date.new("FIWIT:AAA").as_utc
|
85
|
+
assert_equal time, WTF::Date.new("FIWIT:AA").as_utc
|
86
|
+
assert_equal time, WTF::Date.new("FIWIT:A").as_utc
|
87
|
+
assert_equal time, WTF::Date.new("FIWIT:").as_utc
|
88
|
+
assert_equal time, WTF::Date.new("IWIT:").as_utc
|
89
|
+
assert_equal time, WTF::Date.new("IWIT:A").as_utc
|
90
|
+
assert_equal time, WTF::Date.new("IWIT:AA").as_utc
|
91
|
+
time = Time.utc(2002, 7, 10, 12)
|
92
|
+
assert_equal time, WTF::Date.new("FJNXQ:AAAAA").as_utc
|
93
|
+
assert_equal time, WTF::Date.new("FJNXQ:").as_utc
|
94
|
+
assert_equal time, WTF::Date.new("NXQ:").as_utc
|
95
|
+
time = Time.utc(2002, 7, 10, 13, 55, 1, 777000)
|
96
|
+
assert_in_delta time.to_f, WTF::Date.new("FJNXQ:CCAAA").as_utc.to_f, 0.006
|
97
|
+
assert_in_delta time.to_f, WTF::Date.new("FJNXQ:CC").as_utc.to_f, 0.006
|
98
|
+
assert_equal 12, WTF::Date.new(":AA").as_utc.hour
|
99
|
+
assert_equal 0, WTF::Date.new(":AA").as_utc.min
|
100
|
+
assert_equal 0, WTF::Date.new(":N").as_utc.hour
|
101
|
+
assert_equal 0, WTF::Date.new(":N").as_utc.min
|
102
|
+
assert_equal 0, WTF::Date.new("N").as_utc.hour
|
103
|
+
assert_equal 0, WTF::Date.new("N").as_utc.min
|
104
|
+
assert_equal 12, WTF::Date.new(":").as_utc.hour
|
105
|
+
assert_equal 0, WTF::Date.new(":").as_utc.min
|
106
|
+
assert_raise ArgumentError do
|
107
|
+
WTF::Date.new("").as_utc
|
108
|
+
end
|
109
|
+
assert_raise ArgumentError do
|
110
|
+
WTF::Date.new("ABCDEF:ABC").as_utc # date part can't be longer than five characters
|
111
|
+
end
|
112
|
+
assert_nothing_raised do
|
113
|
+
WTF::Date.new("FJCDE:ABCDEFGHIJ").as_utc # time part can be longer than five characters
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_now
|
118
|
+
wtf = WTF::Date.now
|
119
|
+
wtf2 = WTF::Date.now
|
120
|
+
time = wtf.as_utc
|
121
|
+
time2 = wtf2.as_utc
|
122
|
+
past = Time.utc(2009, 11, 28)
|
123
|
+
future = Time.utc(2030, 1, 12)
|
124
|
+
assert time <= time2
|
125
|
+
assert time < future
|
126
|
+
assert time > past
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_convert
|
130
|
+
time = Time.utc(1970, 1, 1, 0)
|
131
|
+
assert_equal time, WTF::Date.convert("FIWIT:NAAAA")
|
132
|
+
assert_equal time, WTF::Date.convert("FIWIT:NAA")
|
133
|
+
assert_equal time, WTF::Date.convert("FIWIT:N")
|
134
|
+
assert_equal time, WTF::Date.convert("IWIT:N")
|
135
|
+
assert_equal "FIWIT:NAAAA", WTF::Date.convert(time)
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wtf
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Wyatt Greene
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-29 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: "\n The WTF gem provides a Ruby class to convert between standard\n time and World Time Format. For more information, visit\n http://www.worldtimeformat.com\n "
|
17
|
+
email: techiferous@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README
|
25
|
+
files:
|
26
|
+
- CHANGELOG
|
27
|
+
- LICENSE
|
28
|
+
- README
|
29
|
+
- Rakefile
|
30
|
+
- lib/wtf.rb
|
31
|
+
- test/test_helper.rb
|
32
|
+
- test/wtf_test.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://github.com/techiferous/wtf
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options:
|
39
|
+
- --charset=UTF-8
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
requirements:
|
55
|
+
- none
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.3.5
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: Converts between World Time Format and standard dates and times.
|
61
|
+
test_files:
|
62
|
+
- test/test_helper.rb
|
63
|
+
- test/wtf_test.rb
|