rubysl-time 1.0.2 → 2.0.3
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 +4 -4
- data/.travis.yml +7 -5
- data/lib/rubysl/time/time.rb +284 -475
- data/lib/rubysl/time/version.rb +1 -1
- data/rubysl-time.gemspec +2 -2
- data/spec/strptime_spec.rb +18 -0
- metadata +30 -41
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 22ecb11d60dd66b66a2d6cbf65ee0e454fa83b69
|
4
|
+
data.tar.gz: 2f99b4a151ee5f0cc92758e778bc588fb8605cd7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d9e9c4d7d52ff61fe4ff13b289147b5742d6e6938c880f4781d20f9ab320dc0ea9cf24269dfdecc09c5bd2d570f8eecf2bf3ed6a35810c34153ef42e8d9c760
|
7
|
+
data.tar.gz: 89278f56d1ae5574f53542d2da0d9f4cbac1240569e99f10e4cdbe31dd316f73bf63cca28a2c0924c159fe217aafb66c8baafc38fbc9ff29002c0094887ce305
|
data/.travis.yml
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
language: ruby
|
2
|
+
before_install:
|
3
|
+
- rvm use $RVM --install --binary --fuzzy
|
4
|
+
- gem update --system
|
5
|
+
- gem --version
|
6
|
+
- gem install rubysl-bundler
|
2
7
|
env:
|
3
|
-
- RUBYLIB=lib
|
4
|
-
script: bundle exec mspec
|
5
|
-
rvm:
|
6
|
-
- 1.8.7
|
7
|
-
- rbx-nightly-18mode
|
8
|
+
- RVM=rbx-nightly-d21 RUBYLIB=lib
|
9
|
+
script: bundle exec mspec spec
|
data/lib/rubysl/time/time.rb
CHANGED
@@ -1,55 +1,98 @@
|
|
1
|
+
require 'date'
|
1
2
|
|
3
|
+
# = time.rb
|
2
4
|
#
|
3
|
-
#
|
4
|
-
#
|
5
|
-
# This library extends the Time class:
|
6
|
-
# * conversion between date string and time object.
|
7
|
-
# * date-time defined by RFC 2822
|
8
|
-
# * HTTP-date defined by RFC 2616
|
9
|
-
# * dateTime defined by XML Schema Part 2: Datatypes (ISO 8601)
|
10
|
-
# * various formats handled by Date._parse (string to time only)
|
11
|
-
#
|
12
|
-
# == Design Issues
|
13
|
-
#
|
14
|
-
# === Specialized interface
|
15
|
-
#
|
16
|
-
# This library provides methods dedicated to special purposes:
|
17
|
-
# * RFC 2822, RFC 2616 and XML Schema.
|
18
|
-
# * They makes usual life easier.
|
19
|
-
#
|
20
|
-
# === Doesn't depend on strftime
|
21
|
-
#
|
22
|
-
# This library doesn't use +strftime+. Especially #rfc2822 doesn't depend
|
23
|
-
# on +strftime+ because:
|
24
|
-
#
|
25
|
-
# * %a and %b are locale sensitive
|
26
|
-
#
|
27
|
-
# Since they are locale sensitive, they may be replaced to
|
28
|
-
# invalid weekday/month name in some locales.
|
29
|
-
# Since ruby-1.6 doesn't invoke setlocale by default,
|
30
|
-
# the problem doesn't arise until some external library invokes setlocale.
|
31
|
-
# Ruby/GTK is the example of such library.
|
32
|
-
#
|
33
|
-
# * %z is not portable
|
34
|
-
#
|
35
|
-
# %z is required to generate zone in date-time of RFC 2822
|
36
|
-
# but it is not portable.
|
5
|
+
# When 'time' is required, Time is extended with additional methods for parsing
|
6
|
+
# and converting Times.
|
37
7
|
#
|
38
|
-
# ==
|
8
|
+
# == Features
|
39
9
|
#
|
40
|
-
#
|
10
|
+
# This library extends the Time class with the following conversions between
|
11
|
+
# date strings and Time objects:
|
41
12
|
#
|
42
|
-
|
43
|
-
|
44
|
-
|
13
|
+
# * date-time defined by {RFC 2822}[http://www.ietf.org/rfc/rfc2822.txt]
|
14
|
+
# * HTTP-date defined by {RFC 2616}[http://www.ietf.org/rfc/rfc2616.txt]
|
15
|
+
# * dateTime defined by XML Schema Part 2: Datatypes ({ISO
|
16
|
+
# 8601}[http://www.iso.org/iso/date_and_time_format])
|
17
|
+
# * various formats handled by Date._parse
|
18
|
+
# * custom formats handled by Date._strptime
|
19
|
+
#
|
20
|
+
# == Examples
|
21
|
+
#
|
22
|
+
# All examples assume you have loaded Time with:
|
23
|
+
#
|
24
|
+
# require 'time'
|
25
|
+
#
|
26
|
+
# All of these examples were done using the EST timezone which is GMT-5.
|
27
|
+
#
|
28
|
+
# === Converting to a String
|
29
|
+
#
|
30
|
+
# t = Time.now
|
31
|
+
# t.iso8601 # => "2011-10-05T22:26:12-04:00"
|
32
|
+
# t.rfc2822 # => "Wed, 05 Oct 2011 22:26:12 -0400"
|
33
|
+
# t.httpdate # => "Thu, 06 Oct 2011 02:26:12 GMT"
|
34
|
+
#
|
35
|
+
# === Time.parse
|
36
|
+
#
|
37
|
+
# #parse takes a string representation of a Time and attempts to parse it
|
38
|
+
# using a heuristic.
|
39
|
+
#
|
40
|
+
# Date.parse("2010-10-31") #=> 2010-10-31 00:00:00 -0500
|
41
|
+
#
|
42
|
+
# Any missing pieces of the date are inferred based on the current date.
|
43
|
+
#
|
44
|
+
# # assuming the current date is "2011-10-31"
|
45
|
+
# Time.parse("12:00") #=> 2011-10-31 12:00:00 -0500
|
46
|
+
#
|
47
|
+
# We can change the date used to infer our missing elements by passing a second
|
48
|
+
# object that responds to #mon, #day and #year, such as Date, Time or DateTime.
|
49
|
+
# We can also use our own object.
|
50
|
+
#
|
51
|
+
# class MyDate
|
52
|
+
# attr_reader :mon, :day, :year
|
45
53
|
#
|
46
|
-
#
|
47
|
-
#
|
54
|
+
# def initialize(mon, day, year)
|
55
|
+
# @mon, @day, @year = mon, day, year
|
56
|
+
# end
|
57
|
+
# end
|
48
58
|
#
|
59
|
+
# d = Date.parse("2010-10-28")
|
60
|
+
# t = Time.parse("2010-10-29")
|
61
|
+
# dt = DateTime.parse("2010-10-30")
|
62
|
+
# md = MyDate.new(10,31,2010)
|
63
|
+
#
|
64
|
+
# Time.parse("12:00", d) #=> 2010-10-28 12:00:00 -0500
|
65
|
+
# Time.parse("12:00", t) #=> 2010-10-29 12:00:00 -0500
|
66
|
+
# Time.parse("12:00", dt) #=> 2010-10-30 12:00:00 -0500
|
67
|
+
# Time.parse("12:00", md) #=> 2010-10-31 12:00:00 -0500
|
68
|
+
#
|
69
|
+
# #parse also accepts an optional block. You can use this block to specify how
|
70
|
+
# to handle the year component of the date. This is specifically designed for
|
71
|
+
# handling two digit years. For example, if you wanted to treat all two digit
|
72
|
+
# years prior to 70 as the year 2000+ you could write this:
|
73
|
+
#
|
74
|
+
# Time.parse("01-10-31") {|year| year + (year < 70 ? 2000 : 1900)}
|
75
|
+
# #=> 2001-10-31 00:00:00 -0500
|
76
|
+
# Time.parse("70-10-31") {|year| year + (year < 70 ? 2000 : 1900)}
|
77
|
+
# #=> 1970-10-31 00:00:00 -0500
|
78
|
+
#
|
79
|
+
# === Time.strptime
|
80
|
+
#
|
81
|
+
# #strptime works similar to +parse+ except that instead of using a heuristic
|
82
|
+
# to detect the format of the input string, you provide a second argument that
|
83
|
+
# is describes the format of the string. For example:
|
84
|
+
#
|
85
|
+
# Time.strptime("2000-10-31", "%Y-%m-%d") #=> 2000-10-31 00:00:00 -0500
|
86
|
+
|
49
87
|
class Time
|
50
88
|
class << Time
|
51
89
|
|
52
|
-
|
90
|
+
#
|
91
|
+
# A hash of timezones mapped to hour differences from UTC. The
|
92
|
+
# set of time zones corresponds to the ones specified by RFC 2822
|
93
|
+
# and ISO 8601.
|
94
|
+
#
|
95
|
+
ZoneOffset = { # :nodoc:
|
53
96
|
'UTC' => 0,
|
54
97
|
# ISO 8601
|
55
98
|
'Z' => 0,
|
@@ -61,11 +104,31 @@ class Time
|
|
61
104
|
'PST' => -8, 'PDT' => -7,
|
62
105
|
# Following definition of military zones is original one.
|
63
106
|
# See RFC 1123 and RFC 2822 for the error in RFC 822.
|
64
|
-
'A' => +1, 'B' => +2, 'C' => +3, 'D' => +4, 'E' => +5, 'F' => +6,
|
107
|
+
'A' => +1, 'B' => +2, 'C' => +3, 'D' => +4, 'E' => +5, 'F' => +6,
|
65
108
|
'G' => +7, 'H' => +8, 'I' => +9, 'K' => +10, 'L' => +11, 'M' => +12,
|
66
|
-
'N' => -1, 'O' => -2, 'P' => -3, 'Q' => -4, 'R' => -5, 'S' => -6,
|
109
|
+
'N' => -1, 'O' => -2, 'P' => -3, 'Q' => -4, 'R' => -5, 'S' => -6,
|
67
110
|
'T' => -7, 'U' => -8, 'V' => -9, 'W' => -10, 'X' => -11, 'Y' => -12,
|
68
111
|
}
|
112
|
+
|
113
|
+
#
|
114
|
+
# Return the number of seconds the specified time zone differs
|
115
|
+
# from UTC.
|
116
|
+
#
|
117
|
+
# Numeric time zones that include minutes, such as
|
118
|
+
# <code>-10:00</code> or <code>+1330</code> will work, as will
|
119
|
+
# simpler hour-only time zones like <code>-10</code> or
|
120
|
+
# <code>+13</code>.
|
121
|
+
#
|
122
|
+
# Textual time zones listed in ZoneOffset are also supported.
|
123
|
+
#
|
124
|
+
# If the time zone does not match any of the above, +zone_offset+
|
125
|
+
# will check if the local time zone (both with and without
|
126
|
+
# potential Daylight Saving \Time changes being in effect) matches
|
127
|
+
# +zone+. Specifying a value for +year+ will change the year used
|
128
|
+
# to find the local time zone.
|
129
|
+
#
|
130
|
+
# If +zone_offset+ is unable to determine the offset, nil will be
|
131
|
+
# returned.
|
69
132
|
def zone_offset(zone, year=self.now.year)
|
70
133
|
off = nil
|
71
134
|
zone = zone.upcase
|
@@ -84,8 +147,25 @@ class Time
|
|
84
147
|
end
|
85
148
|
|
86
149
|
def zone_utc?(zone)
|
87
|
-
# * +0000
|
88
|
-
#
|
150
|
+
# * +0000
|
151
|
+
# In RFC 2822, +0000 indicate a time zone at Universal Time.
|
152
|
+
# Europe/Lisbon is "a time zone at Universal Time" in Winter.
|
153
|
+
# Atlantic/Reykjavik is "a time zone at Universal Time".
|
154
|
+
# Africa/Dakar is "a time zone at Universal Time".
|
155
|
+
# So +0000 is a local time such as Europe/London, etc.
|
156
|
+
# * GMT
|
157
|
+
# GMT is used as a time zone abbreviation in Europe/London,
|
158
|
+
# Africa/Dakar, etc.
|
159
|
+
# So it is a local time.
|
160
|
+
#
|
161
|
+
# * -0000, -00:00
|
162
|
+
# In RFC 2822, -0000 the date-time contains no information about the
|
163
|
+
# local time zone.
|
164
|
+
# In RFC 3339, -00:00 is used for the time in UTC is known,
|
165
|
+
# but the offset to local time is unknown.
|
166
|
+
# They are not appropriate for specific time zone such as
|
167
|
+
# Europe/London because time zone neutral,
|
168
|
+
# So -00:00 and -0000 are treated as UTC.
|
89
169
|
if /\A(?:-00:00|-0000|-00|UTC|Z|UT)\z/i =~ zone
|
90
170
|
true
|
91
171
|
else
|
@@ -94,8 +174,8 @@ class Time
|
|
94
174
|
end
|
95
175
|
private :zone_utc?
|
96
176
|
|
97
|
-
LeapYearMonthDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
98
|
-
CommonYearMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
177
|
+
LeapYearMonthDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] # :nodoc:
|
178
|
+
CommonYearMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] # :nodoc:
|
99
179
|
def month_days(y, m)
|
100
180
|
if ((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)
|
101
181
|
LeapYearMonthDays[m-1]
|
@@ -150,7 +230,7 @@ class Time
|
|
150
230
|
|
151
231
|
def make_time(year, mon, day, hour, min, sec, sec_fraction, zone, now)
|
152
232
|
usec = nil
|
153
|
-
usec =
|
233
|
+
usec = sec_fraction * 1000000 if sec_fraction
|
154
234
|
if now
|
155
235
|
begin
|
156
236
|
break if year; year = now.year
|
@@ -192,20 +272,22 @@ class Time
|
|
192
272
|
# If a block is given, the year described in +date+ is converted by the
|
193
273
|
# block. For example:
|
194
274
|
#
|
195
|
-
# Time.parse(...) {|y| y < 100 ? (y >= 69 ? y + 1900 : y + 2000) : y}
|
275
|
+
# Time.parse(...) {|y| 0 <= y && y < 100 ? (y >= 69 ? y + 1900 : y + 2000) : y}
|
196
276
|
#
|
197
277
|
# If the upper components of the given time are broken or missing, they are
|
198
278
|
# supplied with those of +now+. For the lower components, the minimum
|
199
279
|
# values (1 or 0) are assumed if broken or missing. For example:
|
200
280
|
#
|
201
281
|
# # Suppose it is "Thu Nov 29 14:33:20 GMT 2001" now and
|
202
|
-
# # your
|
203
|
-
# Time.parse("
|
204
|
-
# Time.parse("
|
205
|
-
# Time.parse("
|
282
|
+
# # your time zone is GMT:
|
283
|
+
# now = Time.parse("Thu Nov 29 14:33:20 GMT 2001")
|
284
|
+
# Time.parse("16:30", now) #=> 2001-11-29 16:30:00 +0900
|
285
|
+
# Time.parse("7/23", now) #=> 2001-07-23 00:00:00 +0900
|
286
|
+
# Time.parse("Aug 31", now) #=> 2001-08-31 00:00:00 +0900
|
287
|
+
# Time.parse("Aug 2000", now) #=> 2000-08-01 00:00:00 +0900
|
206
288
|
#
|
207
|
-
# Since there are numerous conflicts among locally defined
|
208
|
-
# abbreviations all over the world, this method is not
|
289
|
+
# Since there are numerous conflicts among locally defined time zone
|
290
|
+
# abbreviations all over the world, this method is not intended to
|
209
291
|
# understand all of them. For example, the abbreviation "CST" is
|
210
292
|
# used variously as:
|
211
293
|
#
|
@@ -216,34 +298,114 @@ class Time
|
|
216
298
|
# +10:30 in Australia/Adelaide,
|
217
299
|
# etc.
|
218
300
|
#
|
219
|
-
# Based on
|
220
|
-
# abbreviations described in RFC 822 and the system
|
301
|
+
# Based on this fact, this method only understands the time zone
|
302
|
+
# abbreviations described in RFC 822 and the system time zone, in the
|
221
303
|
# order named. (i.e. a definition in RFC 822 overrides the system
|
222
|
-
#
|
304
|
+
# time zone definition.) The system time zone is taken from
|
223
305
|
# <tt>Time.local(year, 1, 1).zone</tt> and
|
224
306
|
# <tt>Time.local(year, 7, 1).zone</tt>.
|
225
|
-
# If the extracted
|
307
|
+
# If the extracted time zone abbreviation does not match any of them,
|
226
308
|
# it is ignored and the given time is regarded as a local time.
|
227
309
|
#
|
228
310
|
# ArgumentError is raised if Date._parse cannot extract information from
|
229
|
-
# +date+ or Time class cannot represent specified date.
|
311
|
+
# +date+ or if the Time class cannot represent specified date.
|
230
312
|
#
|
231
|
-
# This method can be used as fail-safe for other parsing methods as:
|
313
|
+
# This method can be used as a fail-safe for other parsing methods as:
|
232
314
|
#
|
233
315
|
# Time.rfc2822(date) rescue Time.parse(date)
|
234
316
|
# Time.httpdate(date) rescue Time.parse(date)
|
235
317
|
# Time.xmlschema(date) rescue Time.parse(date)
|
236
318
|
#
|
237
|
-
# A failure
|
319
|
+
# A failure of Time.parse should be checked, though.
|
320
|
+
#
|
321
|
+
# You must require 'time' to use this method.
|
238
322
|
#
|
239
323
|
def parse(date, now=self.now)
|
240
|
-
|
241
|
-
|
242
|
-
year
|
243
|
-
|
324
|
+
comp = !block_given?
|
325
|
+
d = Date._parse(date, comp)
|
326
|
+
if !d[:year] && !d[:mon] && !d[:mday] && !d[:hour] && !d[:min] && !d[:sec] && !d[:sec_fraction]
|
327
|
+
raise ArgumentError, "no time information in #{date.inspect}"
|
328
|
+
end
|
329
|
+
year = d[:year]
|
330
|
+
year = yield(year) if year && !comp
|
331
|
+
make_time(year, d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now)
|
332
|
+
end
|
333
|
+
|
334
|
+
#
|
335
|
+
# Parses +date+ using Date._strptime and converts it to a Time object.
|
336
|
+
#
|
337
|
+
# If a block is given, the year described in +date+ is converted by the
|
338
|
+
# block. For example:
|
339
|
+
#
|
340
|
+
# Time.strptime(...) {|y| y < 100 ? (y >= 69 ? y + 1900 : y + 2000) : y}
|
341
|
+
#
|
342
|
+
# Below is a list of the formating options:
|
343
|
+
#
|
344
|
+
# %a :: The abbreviated weekday name ("Sun")
|
345
|
+
# %A :: The full weekday name ("Sunday")
|
346
|
+
# %b :: The abbreviated month name ("Jan")
|
347
|
+
# %B :: The full month name ("January")
|
348
|
+
# %c :: The preferred local date and time representation
|
349
|
+
# %C :: Century (20 in 2009)
|
350
|
+
# %d :: Day of the month (01..31)
|
351
|
+
# %D :: Date (%m/%d/%y)
|
352
|
+
# %e :: Day of the month, blank-padded ( 1..31)
|
353
|
+
# %F :: Equivalent to %Y-%m-%d (the ISO 8601 date format)
|
354
|
+
# %h :: Equivalent to %b
|
355
|
+
# %H :: Hour of the day, 24-hour clock (00..23)
|
356
|
+
# %I :: Hour of the day, 12-hour clock (01..12)
|
357
|
+
# %j :: Day of the year (001..366)
|
358
|
+
# %k :: hour, 24-hour clock, blank-padded ( 0..23)
|
359
|
+
# %l :: hour, 12-hour clock, blank-padded ( 0..12)
|
360
|
+
# %L :: Millisecond of the second (000..999)
|
361
|
+
# %m :: Month of the year (01..12)
|
362
|
+
# %M :: Minute of the hour (00..59)
|
363
|
+
# %n :: Newline (\n)
|
364
|
+
# %N :: Fractional seconds digits, default is 9 digits (nanosecond)
|
365
|
+
# %3N :: millisecond (3 digits)
|
366
|
+
# %6N :: microsecond (6 digits)
|
367
|
+
# %9N :: nanosecond (9 digits)
|
368
|
+
# %p :: Meridian indicator ("AM" or "PM")
|
369
|
+
# %P :: Meridian indicator ("am" or "pm")
|
370
|
+
# %r :: time, 12-hour (same as %I:%M:%S %p)
|
371
|
+
# %R :: time, 24-hour (%H:%M)
|
372
|
+
# %s :: Number of seconds since 1970-01-01 00:00:00 UTC.
|
373
|
+
# %S :: Second of the minute (00..60)
|
374
|
+
# %t :: Tab character (\t)
|
375
|
+
# %T :: time, 24-hour (%H:%M:%S)
|
376
|
+
# %u :: Day of the week as a decimal, Monday being 1. (1..7)
|
377
|
+
# %U :: Week number of the current year, starting with the first Sunday as
|
378
|
+
# the first day of the first week (00..53)
|
379
|
+
# %v :: VMS date (%e-%b-%Y)
|
380
|
+
# %V :: Week number of year according to ISO 8601 (01..53)
|
381
|
+
# %W :: Week number of the current year, starting with the first Monday
|
382
|
+
# as the first day of the first week (00..53)
|
383
|
+
# %w :: Day of the week (Sunday is 0, 0..6)
|
384
|
+
# %x :: Preferred representation for the date alone, no time
|
385
|
+
# %X :: Preferred representation for the time alone, no date
|
386
|
+
# %y :: Year without a century (00..99)
|
387
|
+
# %Y :: Year with century
|
388
|
+
# %z :: Time zone as hour offset from UTC (e.g. +0900)
|
389
|
+
# %Z :: Time zone name
|
390
|
+
# %% :: Literal "%" character
|
391
|
+
|
392
|
+
def strptime(date, format, now=self.now)
|
393
|
+
d = Date._strptime(date, format)
|
394
|
+
raise ArgumentError, "invalid strptime format - `#{format}'" unless d
|
395
|
+
if seconds = d[:seconds]
|
396
|
+
if offset = d[:offset]
|
397
|
+
Time.at(seconds).localtime(offset)
|
398
|
+
else
|
399
|
+
Time.at(seconds)
|
400
|
+
end
|
401
|
+
else
|
402
|
+
year = d[:year]
|
403
|
+
year = yield(year) if year && block_given?
|
404
|
+
make_time(year, d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now)
|
405
|
+
end
|
244
406
|
end
|
245
407
|
|
246
|
-
MonthValue = {
|
408
|
+
MonthValue = { # :nodoc:
|
247
409
|
'JAN' => 1, 'FEB' => 2, 'MAR' => 3, 'APR' => 4, 'MAY' => 5, 'JUN' => 6,
|
248
410
|
'JUL' => 7, 'AUG' => 8, 'SEP' => 9, 'OCT' =>10, 'NOV' =>11, 'DEC' =>12
|
249
411
|
}
|
@@ -254,10 +416,12 @@ class Time
|
|
254
416
|
# updated by RFC 1123.
|
255
417
|
#
|
256
418
|
# ArgumentError is raised if +date+ is not compliant with RFC 2822
|
257
|
-
# or Time class cannot represent specified date.
|
419
|
+
# or if the Time class cannot represent specified date.
|
258
420
|
#
|
259
421
|
# See #rfc2822 for more information on this format.
|
260
422
|
#
|
423
|
+
# You must require 'time' to use this method.
|
424
|
+
#
|
261
425
|
def rfc2822(date)
|
262
426
|
if /\A\s*
|
263
427
|
(?:(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s*,\s*)?
|
@@ -299,14 +463,16 @@ class Time
|
|
299
463
|
alias rfc822 rfc2822
|
300
464
|
|
301
465
|
#
|
302
|
-
# Parses +date+ as HTTP-date defined by RFC 2616 and converts it to a
|
303
|
-
# object.
|
466
|
+
# Parses +date+ as an HTTP-date defined by RFC 2616 and converts it to a
|
467
|
+
# Time object.
|
304
468
|
#
|
305
|
-
# ArgumentError is raised if +date+ is not compliant with RFC 2616 or
|
306
|
-
# class cannot represent specified date.
|
469
|
+
# ArgumentError is raised if +date+ is not compliant with RFC 2616 or if
|
470
|
+
# the Time class cannot represent specified date.
|
307
471
|
#
|
308
472
|
# See #httpdate for more information on this format.
|
309
473
|
#
|
474
|
+
# You must require 'time' to use this method.
|
475
|
+
#
|
310
476
|
def httpdate(date)
|
311
477
|
if /\A\s*
|
312
478
|
(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun),\x20
|
@@ -323,7 +489,13 @@ class Time
|
|
323
489
|
(\d\d):(\d\d):(\d\d)\x20
|
324
490
|
GMT
|
325
491
|
\s*\z/ix =~ date
|
326
|
-
|
492
|
+
year = $3.to_i
|
493
|
+
if year < 50
|
494
|
+
year += 2000
|
495
|
+
else
|
496
|
+
year += 1900
|
497
|
+
end
|
498
|
+
self.utc(year, $2, $1.to_i, $4.to_i, $5.to_i, $6.to_i)
|
327
499
|
elsif /\A\s*
|
328
500
|
(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\x20
|
329
501
|
(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\x20
|
@@ -339,21 +511,23 @@ class Time
|
|
339
511
|
end
|
340
512
|
|
341
513
|
#
|
342
|
-
# Parses +date+ as dateTime defined by XML Schema and converts it to
|
343
|
-
# object. The format is restricted version of the format defined
|
344
|
-
# 8601.
|
514
|
+
# Parses +date+ as a dateTime defined by the XML Schema and converts it to
|
515
|
+
# a Time object. The format is a restricted version of the format defined
|
516
|
+
# by ISO 8601.
|
345
517
|
#
|
346
|
-
# ArgumentError is raised if +date+ is not compliant with the format or
|
347
|
-
# class cannot represent specified date.
|
518
|
+
# ArgumentError is raised if +date+ is not compliant with the format or if
|
519
|
+
# the Time class cannot represent specified date.
|
348
520
|
#
|
349
521
|
# See #xmlschema for more information on this format.
|
350
522
|
#
|
523
|
+
# You must require 'time' to use this method.
|
524
|
+
#
|
351
525
|
def xmlschema(date)
|
352
526
|
if /\A\s*
|
353
527
|
(-?\d+)-(\d\d)-(\d\d)
|
354
528
|
T
|
355
529
|
(\d\d):(\d\d):(\d\d)
|
356
|
-
(\.\d
|
530
|
+
(\.\d+)?
|
357
531
|
(Z|[+-]\d\d:\d\d)?
|
358
532
|
\s*\z/ix =~ date
|
359
533
|
year = $1.to_i
|
@@ -363,7 +537,9 @@ class Time
|
|
363
537
|
min = $5.to_i
|
364
538
|
sec = $6.to_i
|
365
539
|
usec = 0
|
366
|
-
|
540
|
+
if $7
|
541
|
+
usec = Rational($7) * 1000000
|
542
|
+
end
|
367
543
|
if $8
|
368
544
|
zone = $8
|
369
545
|
year, mon, day, hour, min, sec =
|
@@ -388,10 +564,12 @@ class Time
|
|
388
564
|
#
|
389
565
|
# If +self+ is a UTC time, -0000 is used as zone.
|
390
566
|
#
|
567
|
+
# You must require 'time' to use this method.
|
568
|
+
#
|
391
569
|
def rfc2822
|
392
|
-
sprintf('%s, %02d %s %d %02d:%02d:%02d ',
|
570
|
+
sprintf('%s, %02d %s %0*d %02d:%02d:%02d ',
|
393
571
|
RFC2822_DAY_NAME[wday],
|
394
|
-
day, RFC2822_MONTH_NAME[mon-1], year,
|
572
|
+
day, RFC2822_MONTH_NAME[mon-1], year < 0 ? 5 : 4, year,
|
395
573
|
hour, min, sec) +
|
396
574
|
if utc?
|
397
575
|
'-0000'
|
@@ -403,62 +581,36 @@ class Time
|
|
403
581
|
end
|
404
582
|
alias rfc822 rfc2822
|
405
583
|
|
406
|
-
|
584
|
+
|
585
|
+
RFC2822_DAY_NAME = [ # :nodoc:
|
407
586
|
'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
|
408
587
|
]
|
409
|
-
|
588
|
+
|
589
|
+
RFC2822_MONTH_NAME = [ # :nodoc:
|
410
590
|
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
|
411
591
|
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
|
412
592
|
]
|
413
593
|
|
414
594
|
#
|
415
|
-
# Returns a string which represents the time as
|
416
|
-
# defined by RFC 2616:
|
417
|
-
#
|
595
|
+
# Returns a string which represents the time as RFC 1123 date of HTTP-date
|
596
|
+
# defined by RFC 2616:
|
597
|
+
#
|
418
598
|
# day-of-week, DD month-name CCYY hh:mm:ss GMT
|
419
599
|
#
|
420
600
|
# Note that the result is always UTC (GMT).
|
421
601
|
#
|
602
|
+
# You must require 'time' to use this method.
|
603
|
+
#
|
422
604
|
def httpdate
|
423
605
|
t = dup.utc
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
day = t.day
|
430
|
-
if day < 10
|
431
|
-
day = "0#{day}"
|
432
|
-
else
|
433
|
-
day = day.to_s
|
434
|
-
end
|
435
|
-
|
436
|
-
hour = t.hour
|
437
|
-
if hour < 10
|
438
|
-
hour = "0#{hour}"
|
439
|
-
else
|
440
|
-
hour = hour.to_s
|
441
|
-
end
|
442
|
-
|
443
|
-
min = t.min
|
444
|
-
if min < 10
|
445
|
-
min = "0#{min}"
|
446
|
-
else
|
447
|
-
min = min.to_s
|
448
|
-
end
|
449
|
-
|
450
|
-
sec = t.sec
|
451
|
-
if sec < 10
|
452
|
-
sec = "0#{sec}"
|
453
|
-
else
|
454
|
-
sec = sec.to_s
|
455
|
-
end
|
456
|
-
|
457
|
-
"#{RFC2822_DAY_NAME[t.wday]}, #{day} #{RFC2822_MONTH_NAME[t.mon-1]} #{t.year} #{hour}:#{min}:#{sec} GMT"
|
606
|
+
sprintf('%s, %02d %s %0*d %02d:%02d:%02d GMT',
|
607
|
+
RFC2822_DAY_NAME[t.wday],
|
608
|
+
t.day, RFC2822_MONTH_NAME[t.mon-1], t.year < 0 ? 5 : 4, t.year,
|
609
|
+
t.hour, t.min, t.sec)
|
458
610
|
end
|
459
611
|
|
460
612
|
#
|
461
|
-
# Returns a string which represents the time as dateTime defined by XML
|
613
|
+
# Returns a string which represents the time as a dateTime defined by XML
|
462
614
|
# Schema:
|
463
615
|
#
|
464
616
|
# CCYY-MM-DDThh:mm:ssTZD
|
@@ -468,362 +620,19 @@ class Time
|
|
468
620
|
#
|
469
621
|
# If self is a UTC time, Z is used as TZD. [+-]hh:mm is used otherwise.
|
470
622
|
#
|
471
|
-
# +
|
472
|
-
# Its default value is 0.
|
623
|
+
# +fractional_digits+ specifies a number of digits to use for fractional
|
624
|
+
# seconds. Its default value is 0.
|
625
|
+
#
|
626
|
+
# You must require 'time' to use this method.
|
473
627
|
#
|
474
628
|
def xmlschema(fraction_digits=0)
|
475
|
-
|
476
|
-
|
477
|
-
if fraction_digits
|
478
|
-
|
479
|
-
elsif fraction_digits <= 6
|
480
|
-
'.' + sprintf('%06d', usec)[0, fraction_digits]
|
481
|
-
else
|
482
|
-
'.' + sprintf('%06d', usec) + '0' * (fraction_digits - 6)
|
483
|
-
end +
|
484
|
-
if utc?
|
485
|
-
'Z'
|
486
|
-
else
|
487
|
-
off = utc_offset
|
488
|
-
sign = off < 0 ? '-' : '+'
|
489
|
-
sprintf('%s%02d:%02d', sign, *(off.abs / 60).divmod(60))
|
629
|
+
fraction_digits = fraction_digits.to_i
|
630
|
+
s = strftime("%FT%T")
|
631
|
+
if fraction_digits > 0
|
632
|
+
s << strftime(".%#{fraction_digits}N")
|
490
633
|
end
|
634
|
+
s << (utc? ? 'Z' : strftime("%:z"))
|
491
635
|
end
|
492
636
|
alias iso8601 xmlschema
|
493
637
|
end
|
494
638
|
|
495
|
-
if __FILE__ == $0
|
496
|
-
require 'test/unit'
|
497
|
-
|
498
|
-
class TimeExtentionTest < Test::Unit::TestCase # :nodoc:
|
499
|
-
def test_rfc822
|
500
|
-
assert_equal(Time.utc(1976, 8, 26, 14, 30) + 4 * 3600,
|
501
|
-
Time.rfc2822("26 Aug 76 14:30 EDT"))
|
502
|
-
assert_equal(Time.utc(1976, 8, 27, 9, 32) + 7 * 3600,
|
503
|
-
Time.rfc2822("27 Aug 76 09:32 PDT"))
|
504
|
-
end
|
505
|
-
|
506
|
-
def test_rfc2822
|
507
|
-
assert_equal(Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600,
|
508
|
-
Time.rfc2822("Fri, 21 Nov 1997 09:55:06 -0600"))
|
509
|
-
assert_equal(Time.utc(2003, 7, 1, 10, 52, 37) - 2 * 3600,
|
510
|
-
Time.rfc2822("Tue, 1 Jul 2003 10:52:37 +0200"))
|
511
|
-
assert_equal(Time.utc(1997, 11, 21, 10, 1, 10) + 6 * 3600,
|
512
|
-
Time.rfc2822("Fri, 21 Nov 1997 10:01:10 -0600"))
|
513
|
-
assert_equal(Time.utc(1997, 11, 21, 11, 0, 0) + 6 * 3600,
|
514
|
-
Time.rfc2822("Fri, 21 Nov 1997 11:00:00 -0600"))
|
515
|
-
assert_equal(Time.utc(1997, 11, 24, 14, 22, 1) + 8 * 3600,
|
516
|
-
Time.rfc2822("Mon, 24 Nov 1997 14:22:01 -0800"))
|
517
|
-
begin
|
518
|
-
Time.at(-1)
|
519
|
-
rescue ArgumentError
|
520
|
-
# ignore
|
521
|
-
else
|
522
|
-
assert_equal(Time.utc(1969, 2, 13, 23, 32, 54) + 3 * 3600 + 30 * 60,
|
523
|
-
Time.rfc2822("Thu, 13 Feb 1969 23:32:54 -0330"))
|
524
|
-
assert_equal(Time.utc(1969, 2, 13, 23, 32, 0) + 3 * 3600 + 30 * 60,
|
525
|
-
Time.rfc2822(" Thu,
|
526
|
-
13
|
527
|
-
Feb
|
528
|
-
1969
|
529
|
-
23:32
|
530
|
-
-0330 (Newfoundland Time)"))
|
531
|
-
end
|
532
|
-
assert_equal(Time.utc(1997, 11, 21, 9, 55, 6),
|
533
|
-
Time.rfc2822("21 Nov 97 09:55:06 GMT"))
|
534
|
-
assert_equal(Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600,
|
535
|
-
Time.rfc2822("Fri, 21 Nov 1997 09 : 55 : 06 -0600"))
|
536
|
-
assert_raise(ArgumentError) {
|
537
|
-
# inner comment is not supported.
|
538
|
-
Time.rfc2822("Fri, 21 Nov 1997 09(comment): 55 : 06 -0600")
|
539
|
-
}
|
540
|
-
end
|
541
|
-
|
542
|
-
def test_rfc2616
|
543
|
-
t = Time.utc(1994, 11, 6, 8, 49, 37)
|
544
|
-
assert_equal(t, Time.httpdate("Sun, 06 Nov 1994 08:49:37 GMT"))
|
545
|
-
assert_equal(t, Time.httpdate("Sunday, 06-Nov-94 08:49:37 GMT"))
|
546
|
-
assert_equal(t, Time.httpdate("Sun Nov 6 08:49:37 1994"))
|
547
|
-
assert_equal(Time.utc(1995, 11, 15, 6, 25, 24),
|
548
|
-
Time.httpdate("Wed, 15 Nov 1995 06:25:24 GMT"))
|
549
|
-
assert_equal(Time.utc(1995, 11, 15, 4, 58, 8),
|
550
|
-
Time.httpdate("Wed, 15 Nov 1995 04:58:08 GMT"))
|
551
|
-
assert_equal(Time.utc(1994, 11, 15, 8, 12, 31),
|
552
|
-
Time.httpdate("Tue, 15 Nov 1994 08:12:31 GMT"))
|
553
|
-
assert_equal(Time.utc(1994, 12, 1, 16, 0, 0),
|
554
|
-
Time.httpdate("Thu, 01 Dec 1994 16:00:00 GMT"))
|
555
|
-
assert_equal(Time.utc(1994, 10, 29, 19, 43, 31),
|
556
|
-
Time.httpdate("Sat, 29 Oct 1994 19:43:31 GMT"))
|
557
|
-
assert_equal(Time.utc(1994, 11, 15, 12, 45, 26),
|
558
|
-
Time.httpdate("Tue, 15 Nov 1994 12:45:26 GMT"))
|
559
|
-
assert_equal(Time.utc(1999, 12, 31, 23, 59, 59),
|
560
|
-
Time.httpdate("Fri, 31 Dec 1999 23:59:59 GMT"))
|
561
|
-
end
|
562
|
-
|
563
|
-
def test_rfc3339
|
564
|
-
t = Time.utc(1985, 4, 12, 23, 20, 50, 520000)
|
565
|
-
s = "1985-04-12T23:20:50.52Z"
|
566
|
-
assert_equal(t, Time.iso8601(s))
|
567
|
-
assert_equal(s, t.iso8601(2))
|
568
|
-
|
569
|
-
t = Time.utc(1996, 12, 20, 0, 39, 57)
|
570
|
-
s = "1996-12-19T16:39:57-08:00"
|
571
|
-
assert_equal(t, Time.iso8601(s))
|
572
|
-
# There is no way to generate time string with arbitrary timezone.
|
573
|
-
s = "1996-12-20T00:39:57Z"
|
574
|
-
assert_equal(t, Time.iso8601(s))
|
575
|
-
assert_equal(s, t.iso8601)
|
576
|
-
|
577
|
-
t = Time.utc(1990, 12, 31, 23, 59, 60)
|
578
|
-
s = "1990-12-31T23:59:60Z"
|
579
|
-
assert_equal(t, Time.iso8601(s))
|
580
|
-
# leap second is representable only if timezone file has it.
|
581
|
-
s = "1990-12-31T15:59:60-08:00"
|
582
|
-
assert_equal(t, Time.iso8601(s))
|
583
|
-
|
584
|
-
begin
|
585
|
-
Time.at(-1)
|
586
|
-
rescue ArgumentError
|
587
|
-
# ignore
|
588
|
-
else
|
589
|
-
t = Time.utc(1937, 1, 1, 11, 40, 27, 870000)
|
590
|
-
s = "1937-01-01T12:00:27.87+00:20"
|
591
|
-
assert_equal(t, Time.iso8601(s))
|
592
|
-
end
|
593
|
-
end
|
594
|
-
|
595
|
-
# http://www.w3.org/TR/xmlschema-2/
|
596
|
-
def test_xmlschema
|
597
|
-
assert_equal(Time.utc(1999, 5, 31, 13, 20, 0) + 5 * 3600,
|
598
|
-
Time.xmlschema("1999-05-31T13:20:00-05:00"))
|
599
|
-
assert_equal(Time.local(2000, 1, 20, 12, 0, 0),
|
600
|
-
Time.xmlschema("2000-01-20T12:00:00"))
|
601
|
-
assert_equal(Time.utc(2000, 1, 20, 12, 0, 0),
|
602
|
-
Time.xmlschema("2000-01-20T12:00:00Z"))
|
603
|
-
assert_equal(Time.utc(2000, 1, 20, 12, 0, 0) - 12 * 3600,
|
604
|
-
Time.xmlschema("2000-01-20T12:00:00+12:00"))
|
605
|
-
assert_equal(Time.utc(2000, 1, 20, 12, 0, 0) + 13 * 3600,
|
606
|
-
Time.xmlschema("2000-01-20T12:00:00-13:00"))
|
607
|
-
assert_equal(Time.utc(2000, 3, 4, 23, 0, 0) - 3 * 3600,
|
608
|
-
Time.xmlschema("2000-03-04T23:00:00+03:00"))
|
609
|
-
assert_equal(Time.utc(2000, 3, 4, 20, 0, 0),
|
610
|
-
Time.xmlschema("2000-03-04T20:00:00Z"))
|
611
|
-
assert_equal(Time.local(2000, 1, 15, 0, 0, 0),
|
612
|
-
Time.xmlschema("2000-01-15T00:00:00"))
|
613
|
-
assert_equal(Time.local(2000, 2, 15, 0, 0, 0),
|
614
|
-
Time.xmlschema("2000-02-15T00:00:00"))
|
615
|
-
assert_equal(Time.local(2000, 1, 15, 12, 0, 0),
|
616
|
-
Time.xmlschema("2000-01-15T12:00:00"))
|
617
|
-
assert_equal(Time.utc(2000, 1, 16, 12, 0, 0),
|
618
|
-
Time.xmlschema("2000-01-16T12:00:00Z"))
|
619
|
-
assert_equal(Time.local(2000, 1, 1, 12, 0, 0),
|
620
|
-
Time.xmlschema("2000-01-01T12:00:00"))
|
621
|
-
assert_equal(Time.utc(1999, 12, 31, 23, 0, 0),
|
622
|
-
Time.xmlschema("1999-12-31T23:00:00Z"))
|
623
|
-
assert_equal(Time.local(2000, 1, 16, 12, 0, 0),
|
624
|
-
Time.xmlschema("2000-01-16T12:00:00"))
|
625
|
-
assert_equal(Time.local(2000, 1, 16, 0, 0, 0),
|
626
|
-
Time.xmlschema("2000-01-16T00:00:00"))
|
627
|
-
assert_equal(Time.utc(2000, 1, 12, 12, 13, 14),
|
628
|
-
Time.xmlschema("2000-01-12T12:13:14Z"))
|
629
|
-
assert_equal(Time.utc(2001, 4, 17, 19, 23, 17, 300000),
|
630
|
-
Time.xmlschema("2001-04-17T19:23:17.3Z"))
|
631
|
-
end
|
632
|
-
|
633
|
-
def test_encode_xmlschema
|
634
|
-
t = Time.utc(2001, 4, 17, 19, 23, 17, 300000)
|
635
|
-
assert_equal("2001-04-17T19:23:17Z", t.xmlschema)
|
636
|
-
assert_equal("2001-04-17T19:23:17.3Z", t.xmlschema(1))
|
637
|
-
assert_equal("2001-04-17T19:23:17.300000Z", t.xmlschema(6))
|
638
|
-
assert_equal("2001-04-17T19:23:17.3000000Z", t.xmlschema(7))
|
639
|
-
|
640
|
-
t = Time.utc(2001, 4, 17, 19, 23, 17, 123456)
|
641
|
-
assert_equal("2001-04-17T19:23:17.1234560Z", t.xmlschema(7))
|
642
|
-
assert_equal("2001-04-17T19:23:17.123456Z", t.xmlschema(6))
|
643
|
-
assert_equal("2001-04-17T19:23:17.12345Z", t.xmlschema(5))
|
644
|
-
assert_equal("2001-04-17T19:23:17.1Z", t.xmlschema(1))
|
645
|
-
|
646
|
-
begin
|
647
|
-
Time.at(-1)
|
648
|
-
rescue ArgumentError
|
649
|
-
# ignore
|
650
|
-
else
|
651
|
-
t = Time.utc(1960, 12, 31, 23, 0, 0, 123456)
|
652
|
-
assert_equal("1960-12-31T23:00:00.123456Z", t.xmlschema(6))
|
653
|
-
end
|
654
|
-
|
655
|
-
assert_equal(249, Time.xmlschema("2008-06-05T23:49:23.000249+09:00").usec)
|
656
|
-
end
|
657
|
-
|
658
|
-
def test_completion
|
659
|
-
now = Time.local(2001,11,29,21,26,35)
|
660
|
-
assert_equal(Time.local( 2001,11,29,21,12),
|
661
|
-
Time.parse("2001/11/29 21:12", now))
|
662
|
-
assert_equal(Time.local( 2001,11,29),
|
663
|
-
Time.parse("2001/11/29", now))
|
664
|
-
assert_equal(Time.local( 2001,11,29),
|
665
|
-
Time.parse( "11/29", now))
|
666
|
-
#assert_equal(Time.local(2001,11,1), Time.parse("Nov", now))
|
667
|
-
assert_equal(Time.local( 2001,11,29,10,22),
|
668
|
-
Time.parse( "10:22", now))
|
669
|
-
end
|
670
|
-
|
671
|
-
def test_invalid
|
672
|
-
# They were actually used in some web sites.
|
673
|
-
assert_raise(ArgumentError) { Time.httpdate("1 Dec 2001 10:23:57 GMT") }
|
674
|
-
assert_raise(ArgumentError) { Time.httpdate("Sat, 1 Dec 2001 10:25:42 GMT") }
|
675
|
-
assert_raise(ArgumentError) { Time.httpdate("Sat, 1-Dec-2001 10:53:55 GMT") }
|
676
|
-
assert_raise(ArgumentError) { Time.httpdate("Saturday, 01-Dec-2001 10:15:34 GMT") }
|
677
|
-
assert_raise(ArgumentError) { Time.httpdate("Saturday, 01-Dec-101 11:10:07 GMT") }
|
678
|
-
assert_raise(ArgumentError) { Time.httpdate("Fri, 30 Nov 2001 21:30:00 JST") }
|
679
|
-
|
680
|
-
# They were actually used in some mails.
|
681
|
-
assert_raise(ArgumentError) { Time.rfc2822("01-5-20") }
|
682
|
-
assert_raise(ArgumentError) { Time.rfc2822("7/21/00") }
|
683
|
-
assert_raise(ArgumentError) { Time.rfc2822("2001-8-28") }
|
684
|
-
assert_raise(ArgumentError) { Time.rfc2822("00-5-6 1:13:06") }
|
685
|
-
assert_raise(ArgumentError) { Time.rfc2822("2001-9-27 9:36:49") }
|
686
|
-
assert_raise(ArgumentError) { Time.rfc2822("2000-12-13 11:01:11") }
|
687
|
-
assert_raise(ArgumentError) { Time.rfc2822("2001/10/17 04:29:55") }
|
688
|
-
assert_raise(ArgumentError) { Time.rfc2822("9/4/2001 9:23:19 PM") }
|
689
|
-
assert_raise(ArgumentError) { Time.rfc2822("01 Nov 2001 09:04:31") }
|
690
|
-
assert_raise(ArgumentError) { Time.rfc2822("13 Feb 2001 16:4 GMT") }
|
691
|
-
assert_raise(ArgumentError) { Time.rfc2822("01 Oct 00 5:41:19 PM") }
|
692
|
-
assert_raise(ArgumentError) { Time.rfc2822("2 Jul 00 00:51:37 JST") }
|
693
|
-
assert_raise(ArgumentError) { Time.rfc2822("01 11 2001 06:55:57 -0500") }
|
694
|
-
assert_raise(ArgumentError) { Time.rfc2822("18 \343\366\356\341\370 2000") }
|
695
|
-
assert_raise(ArgumentError) { Time.rfc2822("Fri, Oct 2001 18:53:32") }
|
696
|
-
assert_raise(ArgumentError) { Time.rfc2822("Fri, 2 Nov 2001 03:47:54") }
|
697
|
-
assert_raise(ArgumentError) { Time.rfc2822("Fri, 27 Jul 2001 11.14.14 +0200") }
|
698
|
-
assert_raise(ArgumentError) { Time.rfc2822("Thu, 2 Nov 2000 04:13:53 -600") }
|
699
|
-
assert_raise(ArgumentError) { Time.rfc2822("Wed, 5 Apr 2000 22:57:09 JST") }
|
700
|
-
assert_raise(ArgumentError) { Time.rfc2822("Mon, 11 Sep 2000 19:47:33 00000") }
|
701
|
-
assert_raise(ArgumentError) { Time.rfc2822("Fri, 28 Apr 2000 20:40:47 +-900") }
|
702
|
-
assert_raise(ArgumentError) { Time.rfc2822("Fri, 19 Jan 2001 8:15:36 AM -0500") }
|
703
|
-
assert_raise(ArgumentError) { Time.rfc2822("Thursday, Sep 27 2001 7:42:35 AM EST") }
|
704
|
-
assert_raise(ArgumentError) { Time.rfc2822("3/11/2001 1:31:57 PM Pacific Daylight Time") }
|
705
|
-
assert_raise(ArgumentError) { Time.rfc2822("Mi, 28 Mrz 2001 11:51:36") }
|
706
|
-
assert_raise(ArgumentError) { Time.rfc2822("P, 30 sept 2001 23:03:14") }
|
707
|
-
assert_raise(ArgumentError) { Time.rfc2822("fr, 11 aug 2000 18:39:22") }
|
708
|
-
assert_raise(ArgumentError) { Time.rfc2822("Fr, 21 Sep 2001 17:44:03 -1000") }
|
709
|
-
assert_raise(ArgumentError) { Time.rfc2822("Mo, 18 Jun 2001 19:21:40 -1000") }
|
710
|
-
assert_raise(ArgumentError) { Time.rfc2822("l\366, 12 aug 2000 18:53:20") }
|
711
|
-
assert_raise(ArgumentError) { Time.rfc2822("l\366, 26 maj 2001 00:15:58") }
|
712
|
-
assert_raise(ArgumentError) { Time.rfc2822("Dom, 30 Sep 2001 17:36:30") }
|
713
|
-
assert_raise(ArgumentError) { Time.rfc2822("%&, 31 %2/ 2000 15:44:47 -0500") }
|
714
|
-
assert_raise(ArgumentError) { Time.rfc2822("dom, 26 ago 2001 03:57:07 -0300") }
|
715
|
-
assert_raise(ArgumentError) { Time.rfc2822("ter, 04 set 2001 16:27:58 -0300") }
|
716
|
-
assert_raise(ArgumentError) { Time.rfc2822("Wen, 3 oct 2001 23:17:49 -0400") }
|
717
|
-
assert_raise(ArgumentError) { Time.rfc2822("Wen, 3 oct 2001 23:17:49 -0400") }
|
718
|
-
assert_raise(ArgumentError) { Time.rfc2822("ele, 11 h: 2000 12:42:15 -0500") }
|
719
|
-
assert_raise(ArgumentError) { Time.rfc2822("Tue, 14 Aug 2001 3:55:3 +0200") }
|
720
|
-
assert_raise(ArgumentError) { Time.rfc2822("Fri, 25 Aug 2000 9:3:48 +0800") }
|
721
|
-
assert_raise(ArgumentError) { Time.rfc2822("Fri, 1 Dec 2000 0:57:50 EST") }
|
722
|
-
assert_raise(ArgumentError) { Time.rfc2822("Mon, 7 May 2001 9:39:51 +0200") }
|
723
|
-
assert_raise(ArgumentError) { Time.rfc2822("Wed, 1 Aug 2001 16:9:15 +0200") }
|
724
|
-
assert_raise(ArgumentError) { Time.rfc2822("Wed, 23 Aug 2000 9:17:36 +0800") }
|
725
|
-
assert_raise(ArgumentError) { Time.rfc2822("Fri, 11 Aug 2000 10:4:42 +0800") }
|
726
|
-
assert_raise(ArgumentError) { Time.rfc2822("Sat, 15 Sep 2001 13:22:2 +0300") }
|
727
|
-
assert_raise(ArgumentError) { Time.rfc2822("Wed,16 \276\305\324\302 2001 20:06:25 +0800") }
|
728
|
-
assert_raise(ArgumentError) { Time.rfc2822("Wed,7 \312\256\322\273\324\302 2001 23:47:22 +0800") }
|
729
|
-
assert_raise(ArgumentError) { Time.rfc2822("=?iso-8859-1?Q?(=C5=DA),?= 10 2 2001 23:32:26 +0900 (JST)") }
|
730
|
-
assert_raise(ArgumentError) { Time.rfc2822("\307\341\314\343\332\311, 30 \344\346\335\343\310\321 2001 10:01:06") }
|
731
|
-
assert_raise(ArgumentError) { Time.rfc2822("=?iso-8859-1?Q?(=BF=E5),?= 12 =?iso-8859-1?Q?9=B7=EE?= 2001 14:52:41\n+0900 (JST)") }
|
732
|
-
end
|
733
|
-
|
734
|
-
def test_zone_0000
|
735
|
-
assert_equal(true, Time.parse("2000-01-01T00:00:00Z").utc?)
|
736
|
-
assert_equal(true, Time.parse("2000-01-01T00:00:00-00:00").utc?)
|
737
|
-
assert_equal(false, Time.parse("2000-01-01T00:00:00+00:00").utc?)
|
738
|
-
assert_equal(false, Time.parse("Sat, 01 Jan 2000 00:00:00 GMT").utc?)
|
739
|
-
assert_equal(true, Time.parse("Sat, 01 Jan 2000 00:00:00 -0000").utc?)
|
740
|
-
assert_equal(false, Time.parse("Sat, 01 Jan 2000 00:00:00 +0000").utc?)
|
741
|
-
assert_equal(false, Time.rfc2822("Sat, 01 Jan 2000 00:00:00 GMT").utc?)
|
742
|
-
assert_equal(true, Time.rfc2822("Sat, 01 Jan 2000 00:00:00 -0000").utc?)
|
743
|
-
assert_equal(false, Time.rfc2822("Sat, 01 Jan 2000 00:00:00 +0000").utc?)
|
744
|
-
assert_equal(true, Time.rfc2822("Sat, 01 Jan 2000 00:00:00 UTC").utc?)
|
745
|
-
end
|
746
|
-
|
747
|
-
def test_parse_leap_second
|
748
|
-
t = Time.utc(1998,12,31,23,59,59)
|
749
|
-
assert_equal(t, Time.parse("Thu Dec 31 23:59:59 UTC 1998"))
|
750
|
-
assert_equal(t, Time.parse("Fri Dec 31 23:59:59 -0000 1998"));t.localtime
|
751
|
-
assert_equal(t, Time.parse("Fri Jan 1 08:59:59 +0900 1999"))
|
752
|
-
assert_equal(t, Time.parse("Fri Jan 1 00:59:59 +0100 1999"))
|
753
|
-
assert_equal(t, Time.parse("Fri Dec 31 23:59:59 +0000 1998"))
|
754
|
-
assert_equal(t, Time.parse("Fri Dec 31 22:59:59 -0100 1998"));t.utc
|
755
|
-
t += 1
|
756
|
-
assert_equal(t, Time.parse("Thu Dec 31 23:59:60 UTC 1998"))
|
757
|
-
assert_equal(t, Time.parse("Fri Dec 31 23:59:60 -0000 1998"));t.localtime
|
758
|
-
assert_equal(t, Time.parse("Fri Jan 1 08:59:60 +0900 1999"))
|
759
|
-
assert_equal(t, Time.parse("Fri Jan 1 00:59:60 +0100 1999"))
|
760
|
-
assert_equal(t, Time.parse("Fri Dec 31 23:59:60 +0000 1998"))
|
761
|
-
assert_equal(t, Time.parse("Fri Dec 31 22:59:60 -0100 1998"));t.utc
|
762
|
-
t += 1 if t.sec == 60
|
763
|
-
assert_equal(t, Time.parse("Thu Jan 1 00:00:00 UTC 1999"))
|
764
|
-
assert_equal(t, Time.parse("Fri Jan 1 00:00:00 -0000 1999"));t.localtime
|
765
|
-
assert_equal(t, Time.parse("Fri Jan 1 09:00:00 +0900 1999"))
|
766
|
-
assert_equal(t, Time.parse("Fri Jan 1 01:00:00 +0100 1999"))
|
767
|
-
assert_equal(t, Time.parse("Fri Jan 1 00:00:00 +0000 1999"))
|
768
|
-
assert_equal(t, Time.parse("Fri Dec 31 23:00:00 -0100 1998"))
|
769
|
-
end
|
770
|
-
|
771
|
-
def test_rfc2822_leap_second
|
772
|
-
t = Time.utc(1998,12,31,23,59,59)
|
773
|
-
assert_equal(t, Time.rfc2822("Thu, 31 Dec 1998 23:59:59 UTC"))
|
774
|
-
assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 23:59:59 -0000"));t.localtime
|
775
|
-
assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 08:59:59 +0900"))
|
776
|
-
assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 00:59:59 +0100"))
|
777
|
-
assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 23:59:59 +0000"))
|
778
|
-
assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 22:59:59 -0100"));t.utc
|
779
|
-
t += 1
|
780
|
-
assert_equal(t, Time.rfc2822("Thu, 31 Dec 1998 23:59:60 UTC"))
|
781
|
-
assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 23:59:60 -0000"));t.localtime
|
782
|
-
assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 08:59:60 +0900"))
|
783
|
-
assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 00:59:60 +0100"))
|
784
|
-
assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 23:59:60 +0000"))
|
785
|
-
assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 22:59:60 -0100"));t.utc
|
786
|
-
t += 1 if t.sec == 60
|
787
|
-
assert_equal(t, Time.rfc2822("Thu, 1 Jan 1999 00:00:00 UTC"))
|
788
|
-
assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 00:00:00 -0000"));t.localtime
|
789
|
-
assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 09:00:00 +0900"))
|
790
|
-
assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 01:00:00 +0100"))
|
791
|
-
assert_equal(t, Time.rfc2822("Fri, 1 Jan 1999 00:00:00 +0000"))
|
792
|
-
assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 23:00:00 -0100"))
|
793
|
-
end
|
794
|
-
|
795
|
-
def test_xmlschema_leap_second
|
796
|
-
t = Time.utc(1998,12,31,23,59,59)
|
797
|
-
assert_equal(t, Time.xmlschema("1998-12-31T23:59:59Z"))
|
798
|
-
assert_equal(t, Time.xmlschema("1998-12-31T23:59:59-00:00"));t.localtime
|
799
|
-
assert_equal(t, Time.xmlschema("1999-01-01T08:59:59+09:00"))
|
800
|
-
assert_equal(t, Time.xmlschema("1999-01-01T00:59:59+01:00"))
|
801
|
-
assert_equal(t, Time.xmlschema("1998-12-31T23:59:59+00:00"))
|
802
|
-
assert_equal(t, Time.xmlschema("1998-12-31T22:59:59-01:00"));t.utc
|
803
|
-
t += 1
|
804
|
-
assert_equal(t, Time.xmlschema("1998-12-31T23:59:60Z"))
|
805
|
-
assert_equal(t, Time.xmlschema("1998-12-31T23:59:60-00:00"));t.localtime
|
806
|
-
assert_equal(t, Time.xmlschema("1999-01-01T08:59:60+09:00"))
|
807
|
-
assert_equal(t, Time.xmlschema("1999-01-01T00:59:60+01:00"))
|
808
|
-
assert_equal(t, Time.xmlschema("1998-12-31T23:59:60+00:00"))
|
809
|
-
assert_equal(t, Time.xmlschema("1998-12-31T22:59:60-01:00"));t.utc
|
810
|
-
t += 1 if t.sec == 60
|
811
|
-
assert_equal(t, Time.xmlschema("1999-01-01T00:00:00Z"))
|
812
|
-
assert_equal(t, Time.xmlschema("1999-01-01T00:00:00-00:00"));t.localtime
|
813
|
-
assert_equal(t, Time.xmlschema("1999-01-01T09:00:00+09:00"))
|
814
|
-
assert_equal(t, Time.xmlschema("1999-01-01T01:00:00+01:00"))
|
815
|
-
assert_equal(t, Time.xmlschema("1999-01-01T00:00:00+00:00"))
|
816
|
-
assert_equal(t, Time.xmlschema("1998-12-31T23:00:00-01:00"))
|
817
|
-
end
|
818
|
-
|
819
|
-
def test_ruby_talk_152866
|
820
|
-
t = Time::xmlschema('2005-08-30T22:48:00-07:00')
|
821
|
-
assert_equal(31, t.day)
|
822
|
-
assert_equal(8, t.mon)
|
823
|
-
end
|
824
|
-
|
825
|
-
def test_parse_fraction
|
826
|
-
assert_equal(500000, Time.parse("2000-01-01T00:00:00.5+00:00").tv_usec)
|
827
|
-
end
|
828
|
-
end
|
829
|
-
end
|
data/lib/rubysl/time/version.rb
CHANGED
data/rubysl-time.gemspec
CHANGED
@@ -16,10 +16,10 @@ Gem::Specification.new do |spec|
|
|
16
16
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
17
|
spec.require_paths = ["lib"]
|
18
18
|
|
19
|
-
spec.
|
20
|
-
spec.add_runtime_dependency "rubysl-rational", "~> 1.0"
|
19
|
+
spec.required_ruby_version = "~> 2.0"
|
21
20
|
|
22
21
|
spec.add_development_dependency "bundler", "~> 1.3"
|
23
22
|
spec.add_development_dependency "rake", "~> 10.0"
|
24
23
|
spec.add_development_dependency "mspec", "~> 1.5"
|
24
|
+
spec.add_development_dependency "rubysl-prettyprint", "~> 2.0"
|
25
25
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'time'
|
2
|
+
|
3
|
+
describe "Time#strptime" do
|
4
|
+
|
5
|
+
it "parses number of seconds since Unix Epoch" do
|
6
|
+
Time.strptime('0', '%s').should == Time.at(0)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "parses number of seconds since Unix Epoch as UTC" do
|
10
|
+
Time.strptime('0', '%s').utc?.should == false
|
11
|
+
end
|
12
|
+
|
13
|
+
it "parses number of seconds since Unix Epoch with timezone" do
|
14
|
+
Time.strptime('0 +0100', '%s %z').to_s.should == Time.at(0).getlocal('+01:00').to_s
|
15
|
+
Time.strptime('0 +0100', '%s %z').strftime('%s %z').should == "0 +0100"
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
metadata
CHANGED
@@ -1,85 +1,71 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubysl-time
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Shirai
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-08
|
11
|
+
date: 2013-10-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: rubysl-parsedate
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ~>
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '1.0'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ~>
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '1.0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: rubysl-rational
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ~>
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '1.0'
|
34
|
-
type: :runtime
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ~>
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '1.0'
|
41
13
|
- !ruby/object:Gem::Dependency
|
42
14
|
name: bundler
|
43
15
|
requirement: !ruby/object:Gem::Requirement
|
44
16
|
requirements:
|
45
|
-
- - ~>
|
17
|
+
- - "~>"
|
46
18
|
- !ruby/object:Gem::Version
|
47
19
|
version: '1.3'
|
48
20
|
type: :development
|
49
21
|
prerelease: false
|
50
22
|
version_requirements: !ruby/object:Gem::Requirement
|
51
23
|
requirements:
|
52
|
-
- - ~>
|
24
|
+
- - "~>"
|
53
25
|
- !ruby/object:Gem::Version
|
54
26
|
version: '1.3'
|
55
27
|
- !ruby/object:Gem::Dependency
|
56
28
|
name: rake
|
57
29
|
requirement: !ruby/object:Gem::Requirement
|
58
30
|
requirements:
|
59
|
-
- - ~>
|
31
|
+
- - "~>"
|
60
32
|
- !ruby/object:Gem::Version
|
61
33
|
version: '10.0'
|
62
34
|
type: :development
|
63
35
|
prerelease: false
|
64
36
|
version_requirements: !ruby/object:Gem::Requirement
|
65
37
|
requirements:
|
66
|
-
- - ~>
|
38
|
+
- - "~>"
|
67
39
|
- !ruby/object:Gem::Version
|
68
40
|
version: '10.0'
|
69
41
|
- !ruby/object:Gem::Dependency
|
70
42
|
name: mspec
|
71
43
|
requirement: !ruby/object:Gem::Requirement
|
72
44
|
requirements:
|
73
|
-
- - ~>
|
45
|
+
- - "~>"
|
74
46
|
- !ruby/object:Gem::Version
|
75
47
|
version: '1.5'
|
76
48
|
type: :development
|
77
49
|
prerelease: false
|
78
50
|
version_requirements: !ruby/object:Gem::Requirement
|
79
51
|
requirements:
|
80
|
-
- - ~>
|
52
|
+
- - "~>"
|
81
53
|
- !ruby/object:Gem::Version
|
82
54
|
version: '1.5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubysl-prettyprint
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.0'
|
83
69
|
description: Ruby standard library time.
|
84
70
|
email:
|
85
71
|
- brixen@gmail.com
|
@@ -87,8 +73,8 @@ executables: []
|
|
87
73
|
extensions: []
|
88
74
|
extra_rdoc_files: []
|
89
75
|
files:
|
90
|
-
- .gitignore
|
91
|
-
- .travis.yml
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
92
78
|
- Gemfile
|
93
79
|
- LICENSE
|
94
80
|
- README.md
|
@@ -104,6 +90,7 @@ files:
|
|
104
90
|
- spec/rfc822_spec.rb
|
105
91
|
- spec/shared/rfc2822.rb
|
106
92
|
- spec/shared/xmlschema.rb
|
93
|
+
- spec/strptime_spec.rb
|
107
94
|
- spec/xmlschema_spec.rb
|
108
95
|
homepage: https://github.com/rubysl/rubysl-time
|
109
96
|
licenses:
|
@@ -115,12 +102,12 @@ require_paths:
|
|
115
102
|
- lib
|
116
103
|
required_ruby_version: !ruby/object:Gem::Requirement
|
117
104
|
requirements:
|
118
|
-
- -
|
105
|
+
- - "~>"
|
119
106
|
- !ruby/object:Gem::Version
|
120
|
-
version: '0'
|
107
|
+
version: '2.0'
|
121
108
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
109
|
requirements:
|
123
|
-
- -
|
110
|
+
- - ">="
|
124
111
|
- !ruby/object:Gem::Version
|
125
112
|
version: '0'
|
126
113
|
requirements: []
|
@@ -136,4 +123,6 @@ test_files:
|
|
136
123
|
- spec/rfc822_spec.rb
|
137
124
|
- spec/shared/rfc2822.rb
|
138
125
|
- spec/shared/xmlschema.rb
|
126
|
+
- spec/strptime_spec.rb
|
139
127
|
- spec/xmlschema_spec.rb
|
128
|
+
has_rdoc:
|