2004-01-17 23:00:00 -05:00
|
|
|
require 'test/unit'
|
* configure.in: check struct timespec, clock_gettime, utimensat,
struct stat.st_atim,
struct stat.st_atimespec,
struct stat.st_atimensec,
struct stat.st_mtim,
struct stat.st_mtimespec,
struct stat.st_mtimensec,
struct stat.st_ctim,
struct stat.st_ctimespec,
struct stat.st_ctimensec.
* include/ruby/missing.h: provide struct timespec if not available.
* time.c: support nanosecond-resolution using struct timespec.
* include/ruby/intern.h: provide rb_time_nano_new.
* file.c (utime_internal): use utimensat if available.
(rb_file_s_utime): refactored.
(rb_f_test): use stat_atime, stat_mtime, stat_ctime.
(rb_stat_cmp): check tv_nsec.
(stat_atimespec): new function.
(stat_atime): ditto.
(stat_mtimespec): ditto.
(stat_mtime): ditto.
(stat_ctimespec): ditto.
(stat_ctime): ditto.
(rb_stat_atime): use stat_atime.
(rb_file_s_atime): ditto.
(rb_file_atime): ditto.
(rb_stat_mtime): use stat_mtime.
(rb_file_s_mtime): ditto.
(rb_file_mtime): ditto.
(rb_file_ctime): use stat_ctime.
(rb_file_s_ctime): ditto.
(rb_stat_ctime): ditto.
* variable.c (rb_copy_generic_ivar): clear clone's instance variables
if obj has no instance variable.
* marshal.c (w_object): dump instance variables of generated string
for TYPE_USERDEF, even if original object has instance variables.
* lib/time.rb (Time#xmlschema): use nsec instead of usec.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13968 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-19 04:09:38 -05:00
|
|
|
require 'rational'
|
2009-04-21 10:56:59 -04:00
|
|
|
require 'delegate'
|
2008-07-14 04:40:17 -04:00
|
|
|
require 'timeout'
|
2009-03-26 12:35:18 -04:00
|
|
|
require 'delegate'
|
2011-07-17 03:26:45 -04:00
|
|
|
require_relative 'envutil'
|
2004-01-17 23:00:00 -05:00
|
|
|
|
|
|
|
class TestTime < Test::Unit::TestCase
|
2008-08-28 10:55:53 -04:00
|
|
|
def setup
|
|
|
|
@verbose = $VERBOSE
|
|
|
|
$VERBOSE = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
$VERBOSE = @verbose
|
|
|
|
end
|
|
|
|
|
2009-05-12 14:00:25 -04:00
|
|
|
def test_new
|
|
|
|
assert_equal(Time.utc(2000,2,10), Time.new(2000,2,10, 11,0,0, 3600*11))
|
|
|
|
assert_equal(Time.utc(2000,2,10), Time.new(2000,2,9, 13,0,0, -3600*11))
|
|
|
|
assert_equal(Time.utc(2000,2,10), Time.new(2000,2,10, 11,0,0, "+11:00"))
|
|
|
|
assert_equal(Rational(1,2), Time.new(2000,2,10, 11,0,5.5, "+11:00").subsec)
|
2010-11-27 04:08:29 -05:00
|
|
|
bug4090 = '[ruby-dev:42631]'
|
|
|
|
tm = [2001,2,28,23,59,30]
|
|
|
|
t = Time.new(*tm, "-12:00")
|
|
|
|
assert_equal([2001,2,28,23,59,30,-43200], [t.year, t.month, t.mday, t.hour, t.min, t.sec, t.gmt_offset], bug4090)
|
2009-05-12 14:00:25 -04:00
|
|
|
end
|
|
|
|
|
2004-01-17 23:00:00 -05:00
|
|
|
def test_time_add()
|
|
|
|
assert_equal(Time.utc(2000, 3, 21, 3, 30) + 3 * 3600,
|
|
|
|
Time.utc(2000, 3, 21, 6, 30))
|
|
|
|
assert_equal(Time.utc(2000, 3, 21, 3, 30) + (-3 * 3600),
|
|
|
|
Time.utc(2000, 3, 21, 0, 30))
|
2004-01-19 02:32:47 -05:00
|
|
|
assert_equal(0, (Time.at(1.1) + 0.9).usec)
|
2010-02-15 08:21:10 -05:00
|
|
|
|
|
|
|
assert((Time.utc(2000, 4, 1) + 24).utc?)
|
|
|
|
assert(!(Time.local(2000, 4, 1) + 24).utc?)
|
|
|
|
|
|
|
|
t = Time.new(2000, 4, 1, 0, 0, 0, "+01:00") + 24
|
|
|
|
assert(!t.utc?)
|
|
|
|
assert_equal(3600, t.utc_offset)
|
|
|
|
t = Time.new(2000, 4, 1, 0, 0, 0, "+02:00") + 24
|
|
|
|
assert(!t.utc?)
|
|
|
|
assert_equal(7200, t.utc_offset)
|
2004-01-17 23:00:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_time_subt()
|
|
|
|
assert_equal(Time.utc(2000, 3, 21, 3, 30) - 3 * 3600,
|
|
|
|
Time.utc(2000, 3, 21, 0, 30))
|
|
|
|
assert_equal(Time.utc(2000, 3, 21, 3, 30) - (-3 * 3600),
|
|
|
|
Time.utc(2000, 3, 21, 6, 30))
|
2004-01-19 02:32:47 -05:00
|
|
|
assert_equal(900000, (Time.at(1.1) - 0.2).usec)
|
2004-01-17 23:00:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_time_time()
|
|
|
|
assert_equal(Time.utc(2000, 3, 21, 3, 30) \
|
|
|
|
-Time.utc(2000, 3, 21, 0, 30), 3*3600)
|
|
|
|
assert_equal(Time.utc(2000, 3, 21, 0, 30) \
|
|
|
|
-Time.utc(2000, 3, 21, 3, 30), -3*3600)
|
|
|
|
end
|
2004-03-30 06:31:44 -05:00
|
|
|
|
2004-03-31 05:40:22 -05:00
|
|
|
def negative_time_t?
|
2004-03-30 06:31:44 -05:00
|
|
|
begin
|
|
|
|
Time.at(-1)
|
2004-03-31 05:40:22 -05:00
|
|
|
true
|
2004-03-30 06:31:44 -05:00
|
|
|
rescue ArgumentError
|
2004-03-31 05:40:22 -05:00
|
|
|
false
|
2004-03-30 21:35:34 -05:00
|
|
|
end
|
2004-03-31 05:40:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_timegm
|
|
|
|
if negative_time_t?
|
2004-03-30 21:35:34 -05:00
|
|
|
assert_equal(-0x80000000, Time.utc(1901, 12, 13, 20, 45, 52).tv_sec)
|
|
|
|
assert_equal(-2, Time.utc(1969, 12, 31, 23, 59, 58).tv_sec)
|
|
|
|
assert_equal(-1, Time.utc(1969, 12, 31, 23, 59, 59).tv_sec)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal(0, Time.utc(1970, 1, 1, 0, 0, 0).tv_sec) # the Epoch
|
|
|
|
assert_equal(1, Time.utc(1970, 1, 1, 0, 0, 1).tv_sec)
|
|
|
|
assert_equal(31535999, Time.utc(1970, 12, 31, 23, 59, 59).tv_sec)
|
|
|
|
assert_equal(31536000, Time.utc(1971, 1, 1, 0, 0, 0).tv_sec)
|
|
|
|
assert_equal(78796799, Time.utc(1972, 6, 30, 23, 59, 59).tv_sec)
|
|
|
|
|
|
|
|
# 1972-06-30T23:59:60Z is the first leap second.
|
|
|
|
if Time.utc(1972, 7, 1, 0, 0, 0) - Time.utc(1972, 6, 30, 23, 59, 59) == 1
|
|
|
|
# no leap second.
|
|
|
|
assert_equal(78796800, Time.utc(1972, 7, 1, 0, 0, 0).tv_sec)
|
|
|
|
assert_equal(78796801, Time.utc(1972, 7, 1, 0, 0, 1).tv_sec)
|
|
|
|
assert_equal(946684800, Time.utc(2000, 1, 1, 0, 0, 0).tv_sec)
|
|
|
|
assert_equal(0x7fffffff, Time.utc(2038, 1, 19, 3, 14, 7).tv_sec)
|
|
|
|
else
|
|
|
|
# leap seconds supported.
|
2004-06-25 18:59:01 -04:00
|
|
|
assert_equal(2, Time.utc(1972, 7, 1, 0, 0, 0) - Time.utc(1972, 6, 30, 23, 59, 59))
|
2004-03-30 21:35:34 -05:00
|
|
|
assert_equal(78796800, Time.utc(1972, 6, 30, 23, 59, 60).tv_sec)
|
|
|
|
assert_equal(78796801, Time.utc(1972, 7, 1, 0, 0, 0).tv_sec)
|
|
|
|
assert_equal(78796802, Time.utc(1972, 7, 1, 0, 0, 1).tv_sec)
|
|
|
|
assert_equal(946684822, Time.utc(2000, 1, 1, 0, 0, 0).tv_sec)
|
2004-03-30 06:31:44 -05:00
|
|
|
end
|
|
|
|
end
|
2004-03-31 05:40:22 -05:00
|
|
|
|
2008-05-04 03:05:26 -04:00
|
|
|
def test_strtime
|
|
|
|
t = nil
|
|
|
|
assert_nothing_raised { t = Time.utc("2000", "1", "2" , "3", "4", "5") }
|
|
|
|
assert_equal(Time.utc(2000,1,2,3,4,5), t)
|
|
|
|
end
|
|
|
|
|
2007-11-18 02:18:56 -05:00
|
|
|
def test_huge_difference
|
2004-03-31 05:40:22 -05:00
|
|
|
if negative_time_t?
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_equal(Time.at(-0x80000000), Time.at(0x7fffffff) - 0xffffffff, "[ruby-dev:22619]")
|
2004-03-31 06:08:16 -05:00
|
|
|
assert_equal(Time.at(-0x80000000), Time.at(0x7fffffff) + (-0xffffffff))
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_equal(Time.at(0x7fffffff), Time.at(-0x80000000) + 0xffffffff, "[ruby-dev:22619]")
|
2004-03-31 06:08:16 -05:00
|
|
|
assert_equal(Time.at(0x7fffffff), Time.at(-0x80000000) - (-0xffffffff))
|
2004-03-31 05:40:22 -05:00
|
|
|
end
|
|
|
|
end
|
2007-11-18 04:30:17 -05:00
|
|
|
|
|
|
|
def test_big_minus
|
|
|
|
begin
|
|
|
|
bigtime0 = Time.at(2**60)
|
|
|
|
bigtime1 = Time.at(2**60+1)
|
|
|
|
rescue RangeError
|
|
|
|
return
|
|
|
|
end
|
|
|
|
assert_equal(1.0, bigtime1 - bigtime0)
|
|
|
|
end
|
* configure.in: check struct timespec, clock_gettime, utimensat,
struct stat.st_atim,
struct stat.st_atimespec,
struct stat.st_atimensec,
struct stat.st_mtim,
struct stat.st_mtimespec,
struct stat.st_mtimensec,
struct stat.st_ctim,
struct stat.st_ctimespec,
struct stat.st_ctimensec.
* include/ruby/missing.h: provide struct timespec if not available.
* time.c: support nanosecond-resolution using struct timespec.
* include/ruby/intern.h: provide rb_time_nano_new.
* file.c (utime_internal): use utimensat if available.
(rb_file_s_utime): refactored.
(rb_f_test): use stat_atime, stat_mtime, stat_ctime.
(rb_stat_cmp): check tv_nsec.
(stat_atimespec): new function.
(stat_atime): ditto.
(stat_mtimespec): ditto.
(stat_mtime): ditto.
(stat_ctimespec): ditto.
(stat_ctime): ditto.
(rb_stat_atime): use stat_atime.
(rb_file_s_atime): ditto.
(rb_file_atime): ditto.
(rb_stat_mtime): use stat_mtime.
(rb_file_s_mtime): ditto.
(rb_file_mtime): ditto.
(rb_file_ctime): use stat_ctime.
(rb_file_s_ctime): ditto.
(rb_stat_ctime): ditto.
* variable.c (rb_copy_generic_ivar): clear clone's instance variables
if obj has no instance variable.
* marshal.c (w_object): dump instance variables of generated string
for TYPE_USERDEF, even if original object has instance variables.
* lib/time.rb (Time#xmlschema): use nsec instead of usec.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13968 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-19 04:09:38 -05:00
|
|
|
|
|
|
|
def test_at
|
2009-07-02 07:04:11 -04:00
|
|
|
assert_equal(100000, Time.at("0.1".to_r).usec)
|
|
|
|
assert_equal(10000, Time.at("0.01".to_r).usec)
|
|
|
|
assert_equal(1000, Time.at("0.001".to_r).usec)
|
|
|
|
assert_equal(100, Time.at("0.0001".to_r).usec)
|
|
|
|
assert_equal(10, Time.at("0.00001".to_r).usec)
|
|
|
|
assert_equal(1, Time.at("0.000001".to_r).usec)
|
|
|
|
assert_equal(100000000, Time.at("0.1".to_r).nsec)
|
|
|
|
assert_equal(10000000, Time.at("0.01".to_r).nsec)
|
|
|
|
assert_equal(1000000, Time.at("0.001".to_r).nsec)
|
|
|
|
assert_equal(100000, Time.at("0.0001".to_r).nsec)
|
|
|
|
assert_equal(10000, Time.at("0.00001".to_r).nsec)
|
|
|
|
assert_equal(1000, Time.at("0.000001".to_r).nsec)
|
|
|
|
assert_equal(100, Time.at("0.0000001".to_r).nsec)
|
|
|
|
assert_equal(10, Time.at("0.00000001".to_r).nsec)
|
|
|
|
assert_equal(1, Time.at("0.000000001".to_r).nsec)
|
* configure.in: check struct timespec, clock_gettime, utimensat,
struct stat.st_atim,
struct stat.st_atimespec,
struct stat.st_atimensec,
struct stat.st_mtim,
struct stat.st_mtimespec,
struct stat.st_mtimensec,
struct stat.st_ctim,
struct stat.st_ctimespec,
struct stat.st_ctimensec.
* include/ruby/missing.h: provide struct timespec if not available.
* time.c: support nanosecond-resolution using struct timespec.
* include/ruby/intern.h: provide rb_time_nano_new.
* file.c (utime_internal): use utimensat if available.
(rb_file_s_utime): refactored.
(rb_f_test): use stat_atime, stat_mtime, stat_ctime.
(rb_stat_cmp): check tv_nsec.
(stat_atimespec): new function.
(stat_atime): ditto.
(stat_mtimespec): ditto.
(stat_mtime): ditto.
(stat_ctimespec): ditto.
(stat_ctime): ditto.
(rb_stat_atime): use stat_atime.
(rb_file_s_atime): ditto.
(rb_file_atime): ditto.
(rb_stat_mtime): use stat_mtime.
(rb_file_s_mtime): ditto.
(rb_file_mtime): ditto.
(rb_file_ctime): use stat_ctime.
(rb_file_s_ctime): ditto.
(rb_stat_ctime): ditto.
* variable.c (rb_copy_generic_ivar): clear clone's instance variables
if obj has no instance variable.
* marshal.c (w_object): dump instance variables of generated string
for TYPE_USERDEF, even if original object has instance variables.
* lib/time.rb (Time#xmlschema): use nsec instead of usec.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13968 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-19 04:09:38 -05:00
|
|
|
assert_equal(100000, Time.at(0.1).usec)
|
|
|
|
assert_equal(10000, Time.at(0.01).usec)
|
|
|
|
assert_equal(1000, Time.at(0.001).usec)
|
|
|
|
assert_equal(100, Time.at(0.0001).usec)
|
|
|
|
assert_equal(10, Time.at(0.00001).usec)
|
2009-04-21 10:56:59 -04:00
|
|
|
assert_equal(3, Time.at(0.000003).usec)
|
* configure.in: check struct timespec, clock_gettime, utimensat,
struct stat.st_atim,
struct stat.st_atimespec,
struct stat.st_atimensec,
struct stat.st_mtim,
struct stat.st_mtimespec,
struct stat.st_mtimensec,
struct stat.st_ctim,
struct stat.st_ctimespec,
struct stat.st_ctimensec.
* include/ruby/missing.h: provide struct timespec if not available.
* time.c: support nanosecond-resolution using struct timespec.
* include/ruby/intern.h: provide rb_time_nano_new.
* file.c (utime_internal): use utimensat if available.
(rb_file_s_utime): refactored.
(rb_f_test): use stat_atime, stat_mtime, stat_ctime.
(rb_stat_cmp): check tv_nsec.
(stat_atimespec): new function.
(stat_atime): ditto.
(stat_mtimespec): ditto.
(stat_mtime): ditto.
(stat_ctimespec): ditto.
(stat_ctime): ditto.
(rb_stat_atime): use stat_atime.
(rb_file_s_atime): ditto.
(rb_file_atime): ditto.
(rb_stat_mtime): use stat_mtime.
(rb_file_s_mtime): ditto.
(rb_file_mtime): ditto.
(rb_file_ctime): use stat_ctime.
(rb_file_s_ctime): ditto.
(rb_stat_ctime): ditto.
* variable.c (rb_copy_generic_ivar): clear clone's instance variables
if obj has no instance variable.
* marshal.c (w_object): dump instance variables of generated string
for TYPE_USERDEF, even if original object has instance variables.
* lib/time.rb (Time#xmlschema): use nsec instead of usec.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13968 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-19 04:09:38 -05:00
|
|
|
assert_equal(100000000, Time.at(0.1).nsec)
|
|
|
|
assert_equal(10000000, Time.at(0.01).nsec)
|
|
|
|
assert_equal(1000000, Time.at(0.001).nsec)
|
|
|
|
assert_equal(100000, Time.at(0.0001).nsec)
|
|
|
|
assert_equal(10000, Time.at(0.00001).nsec)
|
2009-04-21 10:56:59 -04:00
|
|
|
assert_equal(3000, Time.at(0.000003).nsec)
|
|
|
|
assert_equal(199, Time.at(0.0000002).nsec)
|
* configure.in: check struct timespec, clock_gettime, utimensat,
struct stat.st_atim,
struct stat.st_atimespec,
struct stat.st_atimensec,
struct stat.st_mtim,
struct stat.st_mtimespec,
struct stat.st_mtimensec,
struct stat.st_ctim,
struct stat.st_ctimespec,
struct stat.st_ctimensec.
* include/ruby/missing.h: provide struct timespec if not available.
* time.c: support nanosecond-resolution using struct timespec.
* include/ruby/intern.h: provide rb_time_nano_new.
* file.c (utime_internal): use utimensat if available.
(rb_file_s_utime): refactored.
(rb_f_test): use stat_atime, stat_mtime, stat_ctime.
(rb_stat_cmp): check tv_nsec.
(stat_atimespec): new function.
(stat_atime): ditto.
(stat_mtimespec): ditto.
(stat_mtime): ditto.
(stat_ctimespec): ditto.
(stat_ctime): ditto.
(rb_stat_atime): use stat_atime.
(rb_file_s_atime): ditto.
(rb_file_atime): ditto.
(rb_stat_mtime): use stat_mtime.
(rb_file_s_mtime): ditto.
(rb_file_mtime): ditto.
(rb_file_ctime): use stat_ctime.
(rb_file_s_ctime): ditto.
(rb_stat_ctime): ditto.
* variable.c (rb_copy_generic_ivar): clear clone's instance variables
if obj has no instance variable.
* marshal.c (w_object): dump instance variables of generated string
for TYPE_USERDEF, even if original object has instance variables.
* lib/time.rb (Time#xmlschema): use nsec instead of usec.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13968 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-19 04:09:38 -05:00
|
|
|
assert_equal(10, Time.at(0.00000001).nsec)
|
|
|
|
assert_equal(1, Time.at(0.000000001).nsec)
|
2009-04-21 10:56:59 -04:00
|
|
|
|
2008-06-27 12:10:46 -04:00
|
|
|
assert_equal(0, Time.at(1e-10).nsec)
|
|
|
|
assert_equal(0, Time.at(4e-10).nsec)
|
2009-04-21 10:56:59 -04:00
|
|
|
assert_equal(0, Time.at(6e-10).nsec)
|
2008-06-27 12:10:46 -04:00
|
|
|
assert_equal(1, Time.at(14e-10).nsec)
|
2009-04-21 10:56:59 -04:00
|
|
|
assert_equal(1, Time.at(16e-10).nsec)
|
2008-06-27 12:10:46 -04:00
|
|
|
if negative_time_t?
|
2009-04-21 10:56:59 -04:00
|
|
|
assert_equal(999999999, Time.at(-1e-10).nsec)
|
|
|
|
assert_equal(999999999, Time.at(-4e-10).nsec)
|
2008-06-27 12:10:46 -04:00
|
|
|
assert_equal(999999999, Time.at(-6e-10).nsec)
|
2009-04-21 10:56:59 -04:00
|
|
|
assert_equal(999999998, Time.at(-14e-10).nsec)
|
2008-06-27 12:10:46 -04:00
|
|
|
assert_equal(999999998, Time.at(-16e-10).nsec)
|
|
|
|
end
|
2010-03-27 13:14:13 -04:00
|
|
|
|
|
|
|
t = Time.at(-4611686019).utc
|
|
|
|
assert_equal(1823, t.year)
|
|
|
|
|
|
|
|
t = Time.at(4611686018, 999999).utc
|
|
|
|
assert_equal(2116, t.year)
|
|
|
|
assert_equal("0.999999".to_r, t.subsec)
|
|
|
|
|
|
|
|
t = Time.at(2**40 + "1/3".to_r, 9999999999999).utc
|
|
|
|
assert_equal(36812, t.year)
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2010-03-31 08:34:31 -04:00
|
|
|
t = Time.at(-0x3fff_ffff_ffff_ffff)
|
|
|
|
assert_equal(-146138510344, t.year)
|
|
|
|
t = Time.at(-0x4000_0000_0000_0000)
|
|
|
|
assert_equal(-146138510344, t.year)
|
|
|
|
t = Time.at(-0x4000_0000_0000_0001)
|
|
|
|
assert_equal(-146138510344, t.year)
|
|
|
|
t = Time.at(-0x5000_0000_0000_0001)
|
|
|
|
assert_equal(-182673138422, t.year)
|
2010-03-31 15:56:04 -04:00
|
|
|
t = Time.at(-0x6000_0000_0000_0000)
|
|
|
|
assert_equal(-219207766501, t.year)
|
2010-04-06 10:37:16 -04:00
|
|
|
|
|
|
|
t = Time.at(0).utc
|
|
|
|
assert_equal([1970,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
|
|
|
|
t = Time.at(-86400).utc
|
|
|
|
assert_equal([1969,12,31, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
|
|
|
|
t = Time.at(-86400 * (400 * 365 + 97)).utc
|
|
|
|
assert_equal([1970-400,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
|
|
|
|
t = Time.at(-86400 * (400 * 365 + 97)*1000).utc
|
|
|
|
assert_equal([1970-400*1000,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
|
|
|
|
t = Time.at(-86400 * (400 * 365 + 97)*2421).utc
|
|
|
|
assert_equal([1970-400*2421,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
|
|
|
|
t = Time.at(-86400 * (400 * 365 + 97)*1000000).utc
|
|
|
|
assert_equal([1970-400*1000000,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
|
|
|
|
|
|
|
|
t = Time.at(-30613683110400).utc
|
|
|
|
assert_equal([-968139,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
|
|
|
|
t = Time.at(-30613683110401).utc
|
|
|
|
assert_equal([-968140,12,31, 23,59,59], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
|
* configure.in: check struct timespec, clock_gettime, utimensat,
struct stat.st_atim,
struct stat.st_atimespec,
struct stat.st_atimensec,
struct stat.st_mtim,
struct stat.st_mtimespec,
struct stat.st_mtimensec,
struct stat.st_ctim,
struct stat.st_ctimespec,
struct stat.st_ctimensec.
* include/ruby/missing.h: provide struct timespec if not available.
* time.c: support nanosecond-resolution using struct timespec.
* include/ruby/intern.h: provide rb_time_nano_new.
* file.c (utime_internal): use utimensat if available.
(rb_file_s_utime): refactored.
(rb_f_test): use stat_atime, stat_mtime, stat_ctime.
(rb_stat_cmp): check tv_nsec.
(stat_atimespec): new function.
(stat_atime): ditto.
(stat_mtimespec): ditto.
(stat_mtime): ditto.
(stat_ctimespec): ditto.
(stat_ctime): ditto.
(rb_stat_atime): use stat_atime.
(rb_file_s_atime): ditto.
(rb_file_atime): ditto.
(rb_stat_mtime): use stat_mtime.
(rb_file_s_mtime): ditto.
(rb_file_mtime): ditto.
(rb_file_ctime): use stat_ctime.
(rb_file_s_ctime): ditto.
(rb_stat_ctime): ditto.
* variable.c (rb_copy_generic_ivar): clear clone's instance variables
if obj has no instance variable.
* marshal.c (w_object): dump instance variables of generated string
for TYPE_USERDEF, even if original object has instance variables.
* lib/time.rb (Time#xmlschema): use nsec instead of usec.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13968 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-19 04:09:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_at2
|
|
|
|
assert_equal(100, Time.at(0, 0.1).nsec)
|
|
|
|
assert_equal(10, Time.at(0, 0.01).nsec)
|
|
|
|
assert_equal(1, Time.at(0, 0.001).nsec)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_at_rational
|
|
|
|
assert_equal(1, Time.at(Rational(1,1) / 1000000000).nsec)
|
|
|
|
assert_equal(1, Time.at(1167609600 + Rational(1,1) / 1000000000).nsec)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_utc_subsecond
|
2009-04-21 10:56:59 -04:00
|
|
|
assert_equal(500000, Time.utc(2007,1,1,0,0,1.5).usec)
|
* configure.in: check struct timespec, clock_gettime, utimensat,
struct stat.st_atim,
struct stat.st_atimespec,
struct stat.st_atimensec,
struct stat.st_mtim,
struct stat.st_mtimespec,
struct stat.st_mtimensec,
struct stat.st_ctim,
struct stat.st_ctimespec,
struct stat.st_ctimensec.
* include/ruby/missing.h: provide struct timespec if not available.
* time.c: support nanosecond-resolution using struct timespec.
* include/ruby/intern.h: provide rb_time_nano_new.
* file.c (utime_internal): use utimensat if available.
(rb_file_s_utime): refactored.
(rb_f_test): use stat_atime, stat_mtime, stat_ctime.
(rb_stat_cmp): check tv_nsec.
(stat_atimespec): new function.
(stat_atime): ditto.
(stat_mtimespec): ditto.
(stat_mtime): ditto.
(stat_ctimespec): ditto.
(stat_ctime): ditto.
(rb_stat_atime): use stat_atime.
(rb_file_s_atime): ditto.
(rb_file_atime): ditto.
(rb_stat_mtime): use stat_mtime.
(rb_file_s_mtime): ditto.
(rb_file_mtime): ditto.
(rb_file_ctime): use stat_ctime.
(rb_file_s_ctime): ditto.
(rb_stat_ctime): ditto.
* variable.c (rb_copy_generic_ivar): clear clone's instance variables
if obj has no instance variable.
* marshal.c (w_object): dump instance variables of generated string
for TYPE_USERDEF, even if original object has instance variables.
* lib/time.rb (Time#xmlschema): use nsec instead of usec.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13968 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-19 04:09:38 -05:00
|
|
|
assert_equal(100000, Time.utc(2007,1,1,0,0,Rational(11,10)).usec)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_eq_nsec
|
|
|
|
assert_equal(Time.at(0, 0.123), Time.at(0, 0.123))
|
|
|
|
assert_not_equal(Time.at(0, 0.123), Time.at(0, 0.124))
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_marshal_roundtrip(t)
|
|
|
|
iv_names = t.instance_variables
|
|
|
|
iv_vals1 = iv_names.map {|n| t.instance_variable_get n }
|
|
|
|
m = Marshal.dump(t)
|
|
|
|
t2 = Marshal.load(m)
|
|
|
|
iv_vals2 = iv_names.map {|n| t2.instance_variable_get n }
|
|
|
|
assert_equal(t, t2)
|
|
|
|
assert_equal(iv_vals1, iv_vals2)
|
|
|
|
t2
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_marshal_nsec
|
|
|
|
assert_marshal_roundtrip(Time.at(0, 0.123))
|
|
|
|
assert_marshal_roundtrip(Time.at(0, 0.120))
|
|
|
|
end
|
|
|
|
|
2010-01-22 07:08:02 -05:00
|
|
|
def test_marshal_nsec_191
|
|
|
|
# generated by ruby 1.9.1p376
|
|
|
|
m = "\x04\bIu:\tTime\r \x80\x11\x80@\xE2\x01\x00\x06:\rsubmicro\"\ax\x90"
|
|
|
|
t = Marshal.load(m)
|
|
|
|
assert_equal(Time.at(Rational(123456789, 1000000000)), t, "[ruby-dev:40133]")
|
|
|
|
end
|
|
|
|
|
2010-01-13 14:04:17 -05:00
|
|
|
def test_marshal_rational
|
|
|
|
assert_marshal_roundtrip(Time.at(0, Rational(1,3)))
|
|
|
|
assert_not_match(/Rational/, Marshal.dump(Time.at(0, Rational(1,3))))
|
|
|
|
end
|
|
|
|
|
* configure.in: check struct timespec, clock_gettime, utimensat,
struct stat.st_atim,
struct stat.st_atimespec,
struct stat.st_atimensec,
struct stat.st_mtim,
struct stat.st_mtimespec,
struct stat.st_mtimensec,
struct stat.st_ctim,
struct stat.st_ctimespec,
struct stat.st_ctimensec.
* include/ruby/missing.h: provide struct timespec if not available.
* time.c: support nanosecond-resolution using struct timespec.
* include/ruby/intern.h: provide rb_time_nano_new.
* file.c (utime_internal): use utimensat if available.
(rb_file_s_utime): refactored.
(rb_f_test): use stat_atime, stat_mtime, stat_ctime.
(rb_stat_cmp): check tv_nsec.
(stat_atimespec): new function.
(stat_atime): ditto.
(stat_mtimespec): ditto.
(stat_mtime): ditto.
(stat_ctimespec): ditto.
(stat_ctime): ditto.
(rb_stat_atime): use stat_atime.
(rb_file_s_atime): ditto.
(rb_file_atime): ditto.
(rb_stat_mtime): use stat_mtime.
(rb_file_s_mtime): ditto.
(rb_file_mtime): ditto.
(rb_file_ctime): use stat_ctime.
(rb_file_s_ctime): ditto.
(rb_stat_ctime): ditto.
* variable.c (rb_copy_generic_ivar): clear clone's instance variables
if obj has no instance variable.
* marshal.c (w_object): dump instance variables of generated string
for TYPE_USERDEF, even if original object has instance variables.
* lib/time.rb (Time#xmlschema): use nsec instead of usec.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13968 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-19 04:09:38 -05:00
|
|
|
def test_marshal_ivar
|
|
|
|
t = Time.at(123456789, 987654.321)
|
|
|
|
t.instance_eval { @var = 135 }
|
|
|
|
assert_marshal_roundtrip(t)
|
|
|
|
assert_marshal_roundtrip(Marshal.load(Marshal.dump(t)))
|
|
|
|
end
|
|
|
|
|
2010-02-03 10:37:45 -05:00
|
|
|
def test_marshal_timezone
|
|
|
|
bug = '[ruby-dev:40063]'
|
|
|
|
|
|
|
|
t1 = Time.gm(2000)
|
|
|
|
m = Marshal.dump(t1.getlocal("-02:00"))
|
|
|
|
t2 = Marshal.load(m)
|
|
|
|
assert_equal(t1, t2)
|
|
|
|
assert_equal(-7200, t2.utc_offset, bug)
|
|
|
|
m = Marshal.dump(t1.getlocal("+08:15"))
|
|
|
|
t2 = Marshal.load(m)
|
|
|
|
assert_equal(t1, t2)
|
|
|
|
assert_equal(29700, t2.utc_offset, bug)
|
|
|
|
end
|
|
|
|
|
2011-11-19 05:47:03 -05:00
|
|
|
def test_marshal_to_s
|
|
|
|
t1 = Time.new(2011,11,8, 0,42,25, 9*3600)
|
|
|
|
t2 = Time.at(Marshal.load(Marshal.dump(t1)))
|
|
|
|
assert_equal("2011-11-08 00:42:25 +0900", t2.to_s,
|
|
|
|
"[ruby-dev:44827] [Bug #5586]")
|
|
|
|
end
|
|
|
|
|
2008-02-07 10:09:12 -05:00
|
|
|
# Sat Jan 01 00:00:00 UTC 2000
|
|
|
|
T2000 = Time.at(946684800).gmtime
|
|
|
|
|
|
|
|
def test_security_error
|
|
|
|
assert_raise(SecurityError) do
|
|
|
|
Thread.new do
|
|
|
|
t = Time.gm(2000)
|
|
|
|
$SAFE = 4
|
|
|
|
t.localtime
|
|
|
|
end.join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_at3
|
|
|
|
assert_equal(T2000, Time.at(T2000))
|
2009-04-21 10:56:59 -04:00
|
|
|
# assert_raise(RangeError) do
|
|
|
|
# Time.at(2**31-1, 1_000_000)
|
|
|
|
# Time.at(2**63-1, 1_000_000)
|
|
|
|
# end
|
|
|
|
# assert_raise(RangeError) do
|
|
|
|
# Time.at(-2**31, -1_000_000)
|
|
|
|
# Time.at(-2**63, -1_000_000)
|
|
|
|
# end
|
2008-02-07 10:09:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_utc_or_local
|
|
|
|
assert_equal(T2000, Time.gm(2000))
|
|
|
|
assert_equal(T2000, Time.gm(0, 0, 0, 1, 1, 2000, :foo, :bar, false, :baz))
|
|
|
|
assert_equal(T2000, Time.gm(2000, "jan"))
|
|
|
|
assert_equal(T2000, Time.gm(2000, "1"))
|
|
|
|
assert_equal(T2000, Time.gm(2000, 1, 1, 0, 0, 0, 0))
|
|
|
|
assert_equal(T2000, Time.gm(2000, 1, 1, 0, 0, 0, "0"))
|
|
|
|
assert_equal(T2000, Time.gm(2000, 1, 1, 0, 0, "0", :foo, :foo))
|
|
|
|
assert_raise(ArgumentError) { Time.gm(2000, 1, 1, 0, 0, -1, :foo, :foo) }
|
|
|
|
assert_raise(ArgumentError) { Time.gm(2000, 1, 1, 0, 0, -1.0, :foo, :foo) }
|
2008-02-10 16:52:25 -05:00
|
|
|
assert_raise(RangeError) do
|
|
|
|
Time.gm(2000, 1, 1, 0, 0, 10_000_000_000_000_000_001.0, :foo, :foo)
|
|
|
|
end
|
2008-02-07 10:09:12 -05:00
|
|
|
assert_raise(ArgumentError) { Time.gm(2000, 1, 1, 0, 0, -(2**31), :foo, :foo) }
|
|
|
|
o = Object.new
|
2009-04-21 10:56:59 -04:00
|
|
|
def o.to_r; nil; end
|
2008-02-07 10:09:12 -05:00
|
|
|
assert_raise(TypeError) { Time.gm(2000, 1, 1, 0, 0, o, :foo, :foo) }
|
2009-04-21 10:56:59 -04:00
|
|
|
def o.to_r; ""; end
|
|
|
|
assert_raise(TypeError) { Time.gm(2000, 1, 1, 0, 0, o, :foo, :foo) }
|
|
|
|
def o.to_r; Rational(11); end
|
|
|
|
assert_equal(11, Time.gm(2000, 1, 1, 0, 0, o).sec)
|
|
|
|
o = Object.new
|
|
|
|
def o.to_int; 10; end
|
|
|
|
assert_equal(10, Time.gm(2000, 1, 1, 0, 0, o).sec)
|
2008-02-07 10:09:12 -05:00
|
|
|
assert_raise(ArgumentError) { Time.gm(2000, 13) }
|
|
|
|
|
|
|
|
t = Time.local(2000)
|
|
|
|
assert_equal(t.gmt_offset, T2000 - t)
|
2010-03-27 13:14:13 -04:00
|
|
|
|
|
|
|
assert_equal(-4427700000, Time.utc(-4427700000,12,1).year)
|
|
|
|
assert_equal(-2**30+10, Time.utc(-2**30+10,1,1).year)
|
2008-02-07 10:09:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_time_interval
|
|
|
|
m = Mutex.new.lock
|
2008-07-14 04:40:17 -04:00
|
|
|
assert_nothing_raised {
|
|
|
|
Timeout.timeout(10) {
|
|
|
|
m.sleep(0)
|
|
|
|
}
|
|
|
|
}
|
2008-02-07 10:09:12 -05:00
|
|
|
assert_raise(ArgumentError) { m.sleep(-1) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_f
|
|
|
|
assert_equal(946684800.0, T2000.to_f)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cmp
|
|
|
|
assert_equal(-1, T2000 <=> Time.gm(2001))
|
|
|
|
assert_equal(1, T2000 <=> Time.gm(1999))
|
|
|
|
assert_nil(T2000 <=> 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_eql
|
|
|
|
assert(T2000.eql?(T2000))
|
|
|
|
assert(!T2000.eql?(Time.gm(2001)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_utc_p
|
|
|
|
assert(Time.gm(2000).gmt?)
|
|
|
|
assert(!Time.local(2000).gmt?)
|
|
|
|
assert(!Time.at(0).gmt?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_hash
|
|
|
|
assert_kind_of(Integer, T2000.hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_init_copy
|
|
|
|
assert_equal(T2000, T2000.dup)
|
|
|
|
assert_raise(TypeError) do
|
|
|
|
T2000.instance_eval { initialize_copy(nil) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_localtime_gmtime
|
|
|
|
assert_nothing_raised do
|
|
|
|
t = Time.gm(2000)
|
|
|
|
assert(t.gmt?)
|
|
|
|
t.localtime
|
|
|
|
assert(!t.gmt?)
|
|
|
|
t.localtime
|
|
|
|
assert(!t.gmt?)
|
|
|
|
t.gmtime
|
|
|
|
assert(t.gmt?)
|
|
|
|
t.gmtime
|
|
|
|
assert(t.gmt?)
|
|
|
|
end
|
|
|
|
|
|
|
|
t1 = Time.gm(2000)
|
|
|
|
t2 = t1.getlocal
|
|
|
|
assert_equal(t1, t2)
|
2009-05-12 14:00:25 -04:00
|
|
|
t3 = t1.getlocal("-02:00")
|
|
|
|
assert_equal(t1, t3)
|
|
|
|
assert_equal(-7200, t3.utc_offset)
|
2008-02-07 10:09:12 -05:00
|
|
|
t1.localtime
|
|
|
|
assert_equal(t1, t2)
|
|
|
|
assert_equal(t1.gmt?, t2.gmt?)
|
2009-05-12 14:00:25 -04:00
|
|
|
assert_equal(t1, t3)
|
2008-02-07 10:09:12 -05:00
|
|
|
|
|
|
|
t1 = Time.local(2000)
|
|
|
|
t2 = t1.getgm
|
|
|
|
assert_equal(t1, t2)
|
2009-05-12 14:00:25 -04:00
|
|
|
t3 = t1.getlocal("-02:00")
|
|
|
|
assert_equal(t1, t3)
|
|
|
|
assert_equal(-7200, t3.utc_offset)
|
2008-02-07 10:09:12 -05:00
|
|
|
t1.gmtime
|
|
|
|
assert_equal(t1, t2)
|
|
|
|
assert_equal(t1.gmt?, t2.gmt?)
|
2009-05-12 14:00:25 -04:00
|
|
|
assert_equal(t1, t3)
|
2008-02-07 10:09:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_asctime
|
|
|
|
assert_equal("Sat Jan 1 00:00:00 2000", T2000.asctime)
|
2011-08-27 05:18:18 -04:00
|
|
|
assert_equal(Encoding::US_ASCII, T2000.asctime.encoding)
|
2008-02-07 10:09:12 -05:00
|
|
|
assert_kind_of(String, Time.at(0).asctime)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_s
|
|
|
|
assert_equal("2000-01-01 00:00:00 UTC", T2000.to_s)
|
2011-08-27 05:18:18 -04:00
|
|
|
assert_equal(Encoding::US_ASCII, T2000.to_s.encoding)
|
2008-02-07 10:09:12 -05:00
|
|
|
assert_kind_of(String, Time.at(946684800).getlocal.to_s)
|
|
|
|
assert_equal(Time.at(946684800).getlocal.to_s, Time.at(946684800).to_s)
|
|
|
|
end
|
|
|
|
|
2011-08-27 05:18:18 -04:00
|
|
|
def test_zone
|
|
|
|
assert_equal(Encoding.find('locale'), Time.now.zone.encoding)
|
|
|
|
end
|
|
|
|
|
2008-02-07 10:09:12 -05:00
|
|
|
def test_plus_minus_succ
|
2008-02-10 16:52:25 -05:00
|
|
|
# assert_raise(RangeError) { T2000 + 10000000000 }
|
|
|
|
# assert_raise(RangeError) T2000 - 3094168449 }
|
|
|
|
# assert_raise(RangeError) { T2000 + 1200798848 }
|
2008-02-07 10:09:12 -05:00
|
|
|
assert_raise(TypeError) { T2000 + Time.now }
|
|
|
|
assert_equal(T2000 + 1, T2000.succ)
|
|
|
|
end
|
|
|
|
|
2009-12-22 09:02:13 -05:00
|
|
|
def test_plus_type
|
|
|
|
t0 = Time.utc(2000,1,1)
|
|
|
|
n0 = t0.to_i
|
|
|
|
n1 = n0+1
|
|
|
|
t1 = Time.at(n1)
|
|
|
|
assert_equal(t1, t0 + 1)
|
|
|
|
assert_equal(t1, t0 + 1.0)
|
|
|
|
assert_equal(t1, t0 + Rational(1,1))
|
|
|
|
assert_equal(t1, t0 + SimpleDelegator.new(1))
|
|
|
|
assert_equal(t1, t0 + SimpleDelegator.new(1.0))
|
|
|
|
assert_equal(t1, t0 + SimpleDelegator.new(Rational(1,1)))
|
|
|
|
assert_raise(TypeError) { t0 + nil }
|
|
|
|
assert_raise(TypeError) { t0 + "1" }
|
|
|
|
assert_raise(TypeError) { t0 + SimpleDelegator.new("1") }
|
2009-12-25 07:38:16 -05:00
|
|
|
assert_equal(0.5, (t0 + 1.5).subsec)
|
|
|
|
assert_equal(Rational(1,3), (t0 + Rational(4,3)).subsec)
|
|
|
|
assert_equal(0.5, (t0 + SimpleDelegator.new(1.5)).subsec)
|
|
|
|
assert_equal(Rational(1,3), (t0 + SimpleDelegator.new(Rational(4,3))).subsec)
|
2009-12-22 09:02:13 -05:00
|
|
|
end
|
|
|
|
|
2010-03-27 13:14:13 -04:00
|
|
|
def test_minus
|
|
|
|
t = Time.at(-4611686018).utc - 100
|
|
|
|
assert_equal(1823, t.year)
|
|
|
|
end
|
|
|
|
|
2008-02-07 10:09:12 -05:00
|
|
|
def test_readers
|
|
|
|
assert_equal(0, T2000.sec)
|
|
|
|
assert_equal(0, T2000.min)
|
|
|
|
assert_equal(0, T2000.hour)
|
|
|
|
assert_equal(1, T2000.mday)
|
|
|
|
assert_equal(1, T2000.mon)
|
|
|
|
assert_equal(2000, T2000.year)
|
|
|
|
assert_equal(6, T2000.wday)
|
|
|
|
assert_equal(1, T2000.yday)
|
|
|
|
assert_equal(false, T2000.isdst)
|
|
|
|
assert_equal("UTC", T2000.zone)
|
2010-11-22 00:25:15 -05:00
|
|
|
assert_equal(Encoding.find("locale"), T2000.zone.encoding)
|
2008-02-07 10:09:12 -05:00
|
|
|
assert_equal(0, T2000.gmt_offset)
|
|
|
|
assert(!T2000.sunday?)
|
|
|
|
assert(!T2000.monday?)
|
|
|
|
assert(!T2000.tuesday?)
|
|
|
|
assert(!T2000.wednesday?)
|
|
|
|
assert(!T2000.thursday?)
|
|
|
|
assert(!T2000.friday?)
|
|
|
|
assert(T2000.saturday?)
|
|
|
|
assert_equal([0, 0, 0, 1, 1, 2000, 6, 1, false, "UTC"], T2000.to_a)
|
|
|
|
|
|
|
|
t = Time.at(946684800).getlocal
|
|
|
|
assert_equal(t.sec, Time.at(946684800).sec)
|
|
|
|
assert_equal(t.min, Time.at(946684800).min)
|
|
|
|
assert_equal(t.hour, Time.at(946684800).hour)
|
|
|
|
assert_equal(t.mday, Time.at(946684800).mday)
|
|
|
|
assert_equal(t.mon, Time.at(946684800).mon)
|
|
|
|
assert_equal(t.year, Time.at(946684800).year)
|
|
|
|
assert_equal(t.wday, Time.at(946684800).wday)
|
|
|
|
assert_equal(t.yday, Time.at(946684800).yday)
|
|
|
|
assert_equal(t.isdst, Time.at(946684800).isdst)
|
|
|
|
assert_equal(t.zone, Time.at(946684800).zone)
|
2010-11-22 00:25:15 -05:00
|
|
|
assert_equal(Encoding.find("locale"), Time.at(946684800).zone.encoding)
|
2008-02-07 10:09:12 -05:00
|
|
|
assert_equal(t.gmt_offset, Time.at(946684800).gmt_offset)
|
|
|
|
assert_equal(t.sunday?, Time.at(946684800).sunday?)
|
|
|
|
assert_equal(t.monday?, Time.at(946684800).monday?)
|
|
|
|
assert_equal(t.tuesday?, Time.at(946684800).tuesday?)
|
|
|
|
assert_equal(t.wednesday?, Time.at(946684800).wednesday?)
|
|
|
|
assert_equal(t.thursday?, Time.at(946684800).thursday?)
|
|
|
|
assert_equal(t.friday?, Time.at(946684800).friday?)
|
|
|
|
assert_equal(t.saturday?, Time.at(946684800).saturday?)
|
|
|
|
assert_equal(t.to_a, Time.at(946684800).to_a)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_strftime
|
2008-02-28 13:32:17 -05:00
|
|
|
t = Time.at(946684800).getlocal
|
2008-02-07 10:09:12 -05:00
|
|
|
assert_equal("Sat", T2000.strftime("%a"))
|
|
|
|
assert_equal("Saturday", T2000.strftime("%A"))
|
|
|
|
assert_equal("Jan", T2000.strftime("%b"))
|
|
|
|
assert_equal("January", T2000.strftime("%B"))
|
2008-02-10 16:52:25 -05:00
|
|
|
assert_kind_of(String, T2000.strftime("%c"))
|
2008-02-07 10:09:12 -05:00
|
|
|
assert_equal("01", T2000.strftime("%d"))
|
|
|
|
assert_equal("00", T2000.strftime("%H"))
|
|
|
|
assert_equal("12", T2000.strftime("%I"))
|
|
|
|
assert_equal("001", T2000.strftime("%j"))
|
|
|
|
assert_equal("01", T2000.strftime("%m"))
|
|
|
|
assert_equal("00", T2000.strftime("%M"))
|
|
|
|
assert_equal("AM", T2000.strftime("%p"))
|
|
|
|
assert_equal("00", T2000.strftime("%S"))
|
|
|
|
assert_equal("00", T2000.strftime("%U"))
|
|
|
|
assert_equal("00", T2000.strftime("%W"))
|
|
|
|
assert_equal("6", T2000.strftime("%w"))
|
|
|
|
assert_equal("01/01/00", T2000.strftime("%x"))
|
|
|
|
assert_equal("00:00:00", T2000.strftime("%X"))
|
|
|
|
assert_equal("00", T2000.strftime("%y"))
|
|
|
|
assert_equal("2000", T2000.strftime("%Y"))
|
2008-08-20 20:30:28 -04:00
|
|
|
assert_equal("UTC", T2000.strftime("%Z"))
|
2008-02-07 10:09:12 -05:00
|
|
|
assert_equal("%", T2000.strftime("%%"))
|
2008-08-28 11:01:07 -04:00
|
|
|
assert_equal("0", T2000.strftime("%-S"))
|
2008-02-07 10:09:12 -05:00
|
|
|
|
|
|
|
assert_equal("", T2000.strftime(""))
|
|
|
|
assert_equal("foo\0bar\x0000\x0000\x0000", T2000.strftime("foo\0bar\0%H\0%M\0%S"))
|
|
|
|
assert_equal("foo" * 1000, T2000.strftime("foo" * 1000))
|
|
|
|
|
2008-08-28 10:51:30 -04:00
|
|
|
t = Time.mktime(2000, 1, 1)
|
|
|
|
assert_equal("Sat", t.strftime("%a"))
|
2012-05-15 19:36:00 -04:00
|
|
|
end
|
2008-08-20 11:30:28 -04:00
|
|
|
|
2012-05-15 19:36:00 -04:00
|
|
|
def test_strftime_subsec
|
2008-08-20 11:30:28 -04:00
|
|
|
t = Time.at(946684800, 123456.789)
|
2008-08-21 10:57:35 -04:00
|
|
|
assert_equal("123", t.strftime("%3N"))
|
|
|
|
assert_equal("123456", t.strftime("%6N"))
|
|
|
|
assert_equal("123456789", t.strftime("%9N"))
|
2008-08-22 01:28:08 -04:00
|
|
|
assert_equal("1234567890", t.strftime("%10N"))
|
2008-11-24 08:10:07 -05:00
|
|
|
assert_equal("123456789", t.strftime("%0N"))
|
2008-08-22 01:28:08 -04:00
|
|
|
assert_equal("000", t.strftime("%3S"))
|
2008-08-28 13:06:20 -04:00
|
|
|
assert_equal("946684800", t.strftime("%s"))
|
|
|
|
assert_equal("946684800", t.utc.strftime("%s"))
|
2012-05-15 19:36:00 -04:00
|
|
|
end
|
2008-08-21 10:57:35 -04:00
|
|
|
|
2012-05-15 19:36:00 -04:00
|
|
|
def test_strftime_zone
|
2008-08-21 10:57:35 -04:00
|
|
|
t = Time.mktime(2001, 10, 1)
|
|
|
|
assert_equal("2001-10-01", t.strftime("%F"))
|
2011-08-27 05:18:18 -04:00
|
|
|
assert_equal(Encoding::UTF_8, t.strftime("\u3042%Z").encoding)
|
|
|
|
assert_equal(true, t.strftime("\u3042%Z").valid_encoding?)
|
2012-05-15 19:36:00 -04:00
|
|
|
end
|
2008-08-28 11:54:44 -04:00
|
|
|
|
2012-05-15 19:36:00 -04:00
|
|
|
def test_strftime_flags
|
2008-08-28 11:54:44 -04:00
|
|
|
t = Time.mktime(2001, 10, 1, 2, 0, 0)
|
|
|
|
assert_equal("01", t.strftime("%d"))
|
|
|
|
assert_equal("01", t.strftime("%0d"))
|
|
|
|
assert_equal(" 1", t.strftime("%_d"))
|
|
|
|
assert_equal(" 1", t.strftime("%e"))
|
|
|
|
assert_equal("01", t.strftime("%0e"))
|
|
|
|
assert_equal(" 1", t.strftime("%_e"))
|
2008-08-28 13:06:20 -04:00
|
|
|
assert_equal("AM", t.strftime("%p"))
|
|
|
|
assert_equal("am", t.strftime("%#p"))
|
|
|
|
assert_equal("am", t.strftime("%P"))
|
|
|
|
assert_equal("AM", t.strftime("%#P"))
|
2008-08-28 11:54:44 -04:00
|
|
|
assert_equal("02", t.strftime("%H"))
|
|
|
|
assert_equal("02", t.strftime("%0H"))
|
|
|
|
assert_equal(" 2", t.strftime("%_H"))
|
|
|
|
assert_equal("02", t.strftime("%I"))
|
|
|
|
assert_equal("02", t.strftime("%0I"))
|
|
|
|
assert_equal(" 2", t.strftime("%_I"))
|
|
|
|
assert_equal(" 2", t.strftime("%k"))
|
|
|
|
assert_equal("02", t.strftime("%0k"))
|
|
|
|
assert_equal(" 2", t.strftime("%_k"))
|
|
|
|
assert_equal(" 2", t.strftime("%l"))
|
|
|
|
assert_equal("02", t.strftime("%0l"))
|
|
|
|
assert_equal(" 2", t.strftime("%_l"))
|
|
|
|
t = Time.mktime(2001, 10, 1, 14, 0, 0)
|
2008-08-28 13:06:20 -04:00
|
|
|
assert_equal("PM", t.strftime("%p"))
|
|
|
|
assert_equal("pm", t.strftime("%#p"))
|
|
|
|
assert_equal("pm", t.strftime("%P"))
|
|
|
|
assert_equal("PM", t.strftime("%#P"))
|
2008-08-28 11:54:44 -04:00
|
|
|
assert_equal("14", t.strftime("%H"))
|
|
|
|
assert_equal("14", t.strftime("%0H"))
|
|
|
|
assert_equal("14", t.strftime("%_H"))
|
|
|
|
assert_equal("02", t.strftime("%I"))
|
|
|
|
assert_equal("02", t.strftime("%0I"))
|
|
|
|
assert_equal(" 2", t.strftime("%_I"))
|
|
|
|
assert_equal("14", t.strftime("%k"))
|
|
|
|
assert_equal("14", t.strftime("%0k"))
|
|
|
|
assert_equal("14", t.strftime("%_k"))
|
|
|
|
assert_equal(" 2", t.strftime("%l"))
|
|
|
|
assert_equal("02", t.strftime("%0l"))
|
|
|
|
assert_equal(" 2", t.strftime("%_l"))
|
2012-05-15 19:36:00 -04:00
|
|
|
end
|
2008-11-24 07:42:45 -05:00
|
|
|
|
2012-05-15 19:36:00 -04:00
|
|
|
def test_strftime_year
|
2011-01-30 02:10:55 -05:00
|
|
|
t = Time.utc(1,1,4)
|
|
|
|
assert_equal("0001", t.strftime("%Y"))
|
|
|
|
assert_equal("0001", t.strftime("%G"))
|
|
|
|
|
|
|
|
t = Time.utc(0,1,4)
|
|
|
|
assert_equal("0000", t.strftime("%Y"))
|
|
|
|
assert_equal("0000", t.strftime("%G"))
|
|
|
|
|
|
|
|
t = Time.utc(-1,1,4)
|
|
|
|
assert_equal("-0001", t.strftime("%Y"))
|
|
|
|
assert_equal("-0001", t.strftime("%G"))
|
2012-05-15 19:36:00 -04:00
|
|
|
end
|
2011-01-30 02:10:55 -05:00
|
|
|
|
2012-05-15 19:36:00 -04:00
|
|
|
def test_strftime_weeknum
|
2008-11-24 07:42:45 -05:00
|
|
|
# [ruby-dev:37155]
|
|
|
|
t = Time.mktime(1970, 1, 18)
|
|
|
|
assert_equal("0", t.strftime("%w"))
|
|
|
|
assert_equal("7", t.strftime("%u"))
|
2012-05-15 19:36:00 -04:00
|
|
|
end
|
2008-11-24 09:01:47 -05:00
|
|
|
|
2012-05-15 19:36:00 -04:00
|
|
|
def test_strftime_ctrlchar
|
2008-11-24 09:01:47 -05:00
|
|
|
# [ruby-dev:37160]
|
|
|
|
assert_equal("\t", T2000.strftime("%t"))
|
|
|
|
assert_equal("\t", T2000.strftime("%0t"))
|
|
|
|
assert_equal("\t", T2000.strftime("%1t"))
|
|
|
|
assert_equal(" \t", T2000.strftime("%3t"))
|
|
|
|
assert_equal("00\t", T2000.strftime("%03t"))
|
|
|
|
assert_equal("\n", T2000.strftime("%n"))
|
|
|
|
assert_equal("\n", T2000.strftime("%0n"))
|
|
|
|
assert_equal("\n", T2000.strftime("%1n"))
|
|
|
|
assert_equal(" \n", T2000.strftime("%3n"))
|
|
|
|
assert_equal("00\n", T2000.strftime("%03n"))
|
2012-05-15 19:36:00 -04:00
|
|
|
end
|
2008-11-24 09:29:37 -05:00
|
|
|
|
2012-05-15 19:36:00 -04:00
|
|
|
def test_strftime_weekflags
|
2008-11-24 09:29:37 -05:00
|
|
|
# [ruby-dev:37162]
|
|
|
|
assert_equal("SAT", T2000.strftime("%#a"))
|
|
|
|
assert_equal("SATURDAY", T2000.strftime("%#A"))
|
|
|
|
assert_equal("JAN", T2000.strftime("%#b"))
|
|
|
|
assert_equal("JANUARY", T2000.strftime("%#B"))
|
|
|
|
assert_equal("JAN", T2000.strftime("%#h"))
|
2008-12-23 05:13:11 -05:00
|
|
|
assert_equal("FRIDAY", Time.local(2008,1,4).strftime("%#A"))
|
2012-05-15 19:36:00 -04:00
|
|
|
end
|
2010-03-31 08:34:31 -04:00
|
|
|
|
2012-05-15 19:36:00 -04:00
|
|
|
def test_strftime_rational
|
2010-03-31 08:34:31 -04:00
|
|
|
t = Time.utc(2000,3,14, 6,53,"58.979323846".to_r) # Pi Day
|
|
|
|
assert_equal("03/14/2000 6:53:58.97932384600000000000000000000",
|
|
|
|
t.strftime("%m/%d/%Y %l:%M:%S.%29N"))
|
|
|
|
assert_equal("03/14/2000 6:53:58.9793238460",
|
|
|
|
t.strftime("%m/%d/%Y %l:%M:%S.%10N"))
|
|
|
|
assert_equal("03/14/2000 6:53:58.979323846",
|
|
|
|
t.strftime("%m/%d/%Y %l:%M:%S.%9N"))
|
|
|
|
assert_equal("03/14/2000 6:53:58.97932384",
|
|
|
|
t.strftime("%m/%d/%Y %l:%M:%S.%8N"))
|
|
|
|
|
|
|
|
t = Time.utc(1592,3,14, 6,53,"58.97932384626433832795028841971".to_r) # Pi Day
|
|
|
|
assert_equal("03/14/1592 6:53:58.97932384626433832795028841971",
|
|
|
|
t.strftime("%m/%d/%Y %l:%M:%S.%29N"))
|
|
|
|
assert_equal("03/14/1592 6:53:58.9793238462",
|
|
|
|
t.strftime("%m/%d/%Y %l:%M:%S.%10N"))
|
|
|
|
assert_equal("03/14/1592 6:53:58.979323846",
|
|
|
|
t.strftime("%m/%d/%Y %l:%M:%S.%9N"))
|
|
|
|
assert_equal("03/14/1592 6:53:58.97932384",
|
|
|
|
t.strftime("%m/%d/%Y %l:%M:%S.%8N"))
|
2012-05-15 19:36:00 -04:00
|
|
|
end
|
2010-12-30 13:53:41 -05:00
|
|
|
|
2012-05-15 19:36:00 -04:00
|
|
|
def test_strftime_far_future
|
2010-12-30 13:53:41 -05:00
|
|
|
# [ruby-core:33985]
|
|
|
|
assert_equal("3000000000", Time.at(3000000000).strftime('%s'))
|
2012-05-15 19:36:00 -04:00
|
|
|
end
|
2011-08-07 10:38:17 -04:00
|
|
|
|
2012-05-15 19:36:00 -04:00
|
|
|
def test_strftime_too_wide
|
2011-08-07 10:38:17 -04:00
|
|
|
bug4457 = '[ruby-dev:43285]'
|
|
|
|
assert_raise(Errno::ERANGE, bug4457) {Time.now.strftime('%8192z')}
|
2012-05-15 19:36:00 -04:00
|
|
|
end
|
2012-04-17 09:18:51 -04:00
|
|
|
|
2012-05-15 19:36:00 -04:00
|
|
|
def test_strftime_padding
|
2012-04-17 09:18:51 -04:00
|
|
|
bug4458 = '[ruby-dev:43287]'
|
|
|
|
t = T2000.getlocal("+09:00")
|
|
|
|
assert_equal(" +900", t.strftime("%_10z"), bug4458)
|
|
|
|
assert_equal("+000000900", t.strftime("%10z"), bug4458)
|
|
|
|
assert_equal(" +9:00", t.strftime("%_:10z"), bug4458)
|
|
|
|
assert_equal("+000009:00", t.strftime("%:10z"), bug4458)
|
|
|
|
assert_equal(" +9:00:00", t.strftime("%_::10z"), bug4458)
|
|
|
|
assert_equal("+009:00:00", t.strftime("%::10z"), bug4458)
|
|
|
|
t = T2000.getlocal("-05:00")
|
|
|
|
assert_equal(" -500", t.strftime("%_10z"), bug4458)
|
|
|
|
assert_equal("-000000500", t.strftime("%10z"), bug4458)
|
|
|
|
assert_equal(" -5:00", t.strftime("%_:10z"), bug4458)
|
|
|
|
assert_equal("-000005:00", t.strftime("%:10z"), bug4458)
|
|
|
|
assert_equal(" -5:00:00", t.strftime("%_::10z"), bug4458)
|
|
|
|
assert_equal("-005:00:00", t.strftime("%::10z"), bug4458)
|
2012-04-19 03:33:55 -04:00
|
|
|
|
|
|
|
bug6323 = '[ruby-core:44447]'
|
|
|
|
t = T2000.getlocal("+00:36")
|
|
|
|
assert_equal(" +036", t.strftime("%_10z"), bug6323)
|
|
|
|
assert_equal("+000000036", t.strftime("%10z"), bug6323)
|
|
|
|
assert_equal(" +0:36", t.strftime("%_:10z"), bug6323)
|
|
|
|
assert_equal("+000000:36", t.strftime("%:10z"), bug6323)
|
|
|
|
assert_equal(" +0:36:00", t.strftime("%_::10z"), bug6323)
|
|
|
|
assert_equal("+000:36:00", t.strftime("%::10z"), bug6323)
|
|
|
|
t = T2000.getlocal("-00:55")
|
|
|
|
assert_equal(" -055", t.strftime("%_10z"), bug6323)
|
|
|
|
assert_equal("-000000055", t.strftime("%10z"), bug6323)
|
|
|
|
assert_equal(" -0:55", t.strftime("%_:10z"), bug6323)
|
|
|
|
assert_equal("-000000:55", t.strftime("%:10z"), bug6323)
|
|
|
|
assert_equal(" -0:55:00", t.strftime("%_::10z"), bug6323)
|
|
|
|
assert_equal("-000:55:00", t.strftime("%::10z"), bug6323)
|
2008-02-07 10:09:12 -05:00
|
|
|
end
|
2009-03-26 12:35:18 -04:00
|
|
|
|
|
|
|
def test_delegate
|
|
|
|
d1 = SimpleDelegator.new(t1 = Time.utc(2000))
|
|
|
|
d2 = SimpleDelegator.new(t2 = Time.utc(2001))
|
|
|
|
assert_equal(-1, t1 <=> t2)
|
|
|
|
assert_equal(1, t2 <=> t1)
|
|
|
|
assert_equal(-1, d1 <=> d2)
|
|
|
|
assert_equal(1, d2 <=> d1)
|
|
|
|
end
|
2010-01-13 18:51:16 -05:00
|
|
|
|
|
|
|
def test_to_r
|
|
|
|
assert_kind_of(Rational, Time.new(2000,1,1,0,0,Rational(4,3)).to_r)
|
|
|
|
assert_kind_of(Rational, Time.utc(1970).to_r)
|
|
|
|
end
|
2010-03-11 08:39:12 -05:00
|
|
|
|
|
|
|
def test_round
|
|
|
|
t = Time.utc(1999,12,31, 23,59,59)
|
|
|
|
t2 = (t+0.4).round
|
|
|
|
assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
|
|
|
|
assert_equal(0, t2.subsec)
|
|
|
|
t2 = (t+0.49).round
|
|
|
|
assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
|
|
|
|
assert_equal(0, t2.subsec)
|
|
|
|
t2 = (t+0.5).round
|
|
|
|
assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
|
|
|
|
assert_equal(0, t2.subsec)
|
|
|
|
t2 = (t+1.4).round
|
|
|
|
assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
|
|
|
|
assert_equal(0, t2.subsec)
|
|
|
|
t2 = (t+1.49).round
|
|
|
|
assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
|
|
|
|
assert_equal(0, t2.subsec)
|
|
|
|
t2 = (t+1.5).round
|
|
|
|
assert_equal([1,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
|
|
|
|
assert_equal(0, t2.subsec)
|
|
|
|
|
|
|
|
t2 = (t+0.123456789).round(4)
|
|
|
|
assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
|
|
|
|
assert_equal(Rational(1235,10000), t2.subsec)
|
|
|
|
|
|
|
|
off = 0.0
|
|
|
|
100.times {|i|
|
|
|
|
t2 = (t+off).round(1)
|
|
|
|
assert_equal(Rational(i % 10, 10), t2.subsec)
|
|
|
|
off += 0.1
|
|
|
|
}
|
|
|
|
end
|
2011-07-15 12:06:53 -04:00
|
|
|
|
|
|
|
def test_getlocal_dont_share_eigenclass
|
|
|
|
bug5012 = "[ruby-dev:44071]"
|
|
|
|
|
|
|
|
t0 = Time.now
|
2011-07-17 03:26:45 -04:00
|
|
|
class << t0; end
|
2011-07-15 12:06:53 -04:00
|
|
|
t1 = t0.getlocal
|
|
|
|
|
|
|
|
def t0.m
|
|
|
|
0
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise(NoMethodError, bug5012) { t1.m }
|
|
|
|
end
|
2011-07-17 03:26:45 -04:00
|
|
|
|
|
|
|
def test_time_subclass
|
|
|
|
bug5036 = '[ruby-dev:44122]'
|
|
|
|
tc = Class.new(Time)
|
|
|
|
tc.inspect
|
|
|
|
t = tc.now
|
|
|
|
error = assert_raise(SecurityError) do
|
|
|
|
proc do
|
|
|
|
$SAFE = 4
|
|
|
|
t.gmtime
|
|
|
|
end.call
|
|
|
|
end
|
|
|
|
assert_equal("Insecure: can't modify #{tc}", error.message, bug5036)
|
|
|
|
end
|
2012-03-24 09:22:22 -04:00
|
|
|
|
|
|
|
def test_sec_str
|
|
|
|
bug6193 = '[ruby-core:43569]'
|
|
|
|
t = nil
|
|
|
|
assert_nothing_raised(bug6193) {t = Time.new(2012, 1, 2, 3, 4, "5")}
|
|
|
|
assert_equal(Time.new(2012, 1, 2, 3, 4, 5), t, bug6193)
|
|
|
|
end
|
2004-01-17 23:00:00 -05:00
|
|
|
end
|