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'
|
2004-01-17 23:00:00 -05:00
|
|
|
|
|
|
|
class TestTime < Test::Unit::TestCase
|
|
|
|
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)
|
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
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
assert_equal(1, Time.at(0.000001).usec)
|
|
|
|
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)
|
|
|
|
assert_equal(1000, Time.at(0.000001).nsec)
|
|
|
|
assert_equal(100, Time.at(0.0000001).nsec)
|
|
|
|
assert_equal(10, Time.at(0.00000001).nsec)
|
|
|
|
assert_equal(1, Time.at(0.000000001).nsec)
|
|
|
|
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
|
|
|
|
assert_equal(100000, Time.utc(2007,1,1,0,0,1.1).usec)
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2004-01-17 23:00:00 -05:00
|
|
|
end
|