20 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto 3494b699ef time: implement nanosecond precision with zero memory overhead
Replace microsecond storage with nanosecond storage in struct mrb_time
while maintaining full backward compatibility and zero memory increase.

Changes:
- Replace 'usec' field with 'nsec' field in struct mrb_time
- Preserve full nanosecond precision from timespec_get/clock_gettime
- Add Time#nsec and Time#tv_nsec methods for Ruby spec compliance
- Update Time#usec to compute microseconds from nanoseconds
- Convert all arithmetic operations to handle nanosecond precision
- Add comprehensive tests for nanosecond functionality

Platform support:
- Modern systems: True nanosecond precision via timespec_get/clock_gettime
- Older systems: Microsecond precision converted to nanoseconds (gettimeofday)
- Minimal systems: Second precision with synthetic microseconds (time)

Benefits:
- Zero memory overhead (struct remains 80 bytes)
- 100% backward compatible (all existing tests pass)
- Better precision for time arithmetic and comparisons
- Ruby API compliant with standard nanosecond methods
- Automatic precision upgrade on capable systems

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-14 10:53:08 +09:00
Yukihiro "Matz" Matsumoto 16d3f31f82 time: add missing timezone offset methods and tests
implement gmt_offset, utc_offset, and gmtoff methods as aliases to
complete the ruby time api. all three methods return timezone offset
in seconds, with utc times returning 0 and local times returning the
appropriate offset value.

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-14 10:53:08 +09:00
Yukihiro "Matz" Matsumoto 171d32c007 time.c: 1969-12-31 23:59:59 is a valid time.
But `mktime(3)` for the date returns `-1` which happen to be the
error value.
2022-01-14 11:40:52 +09:00
Yukihiro "Matz" Matsumoto 4d249c28d9 Skip tests that use Float inside; ref #5421 2021-04-24 12:04:08 +09:00
KOBAYASHI Shuji 5116789aa0 Fix UTC offset representation in Time#to_s on some environments; ref #4604
Use own implementation to calculate UTC offset on Visual Studio 2015 or
earlier or MinGW because `strftime("%z")` on these environments does not
conform C99.
2019-07-31 20:34:27 +09:00
KOBAYASHI Shuji 240bbc39e1 Fix the order of "expected" and "actual" in mruby-time test 2019-05-24 21:02:22 +09:00
KOBAYASHI Shuji edf4b33c61 Refine Time#(to_s|inspect)
For the following reasons:

- Ruby compatibility.
- Add UTC offset (time zone informations was not included by #4433).
- More readable.

Example:

  Before this patch:

    p Time.gm(2003,4,5,6,7,8,9)        #=> Sat Apr  5 06:07:08 2003
    p Time.local(2013,10,28,16,27,48)  #=> Mon Oct 28 16:27:48 2013

  After this patch:

    p Time.gm(2003,4,5,6,7,8,9)        #=> 2003-04-05 06:07:08 UTC
    p Time.local(2013,10,28,16,27,48)  #=> 2013-10-28 16:27:48 +0900

Implementation:

I use `strftime(3)` because UTC offset can be added and program size become
smaller than the other implementations (using `sprintf(3)`, self conversion
etc) in my environment.
2019-05-14 22:50:36 +09:00
KOBAYASHI Shuji a2616db4dc Fix Time#(asctime|ctime) according to ISO Ruby
- A leading charactor for day is space.
- Time zone does not included.

Before this patch:

  Time.gm(1982,3,4,5,6,7).asctime  #=> "Thu Mar 04 05:06:07 UTC 1982"

After this patch:

  Time.gm(1982,3,4,5,6,7).asctime  #=> "Thu Mar  4 05:06:07 1982"
2019-05-13 19:46:26 +09:00
KOBAYASHI Shuji 2c2584138a Fix missing assertions in mruby-time test 2019-05-12 20:25:11 +09:00
Yukihiro "Matz" Matsumoto 3ff56a8e55 Time.new(1969,12,31,23,59,59) may or may not faile; ref #3932
On some platform and timezone it is a valid time spec.
2018-01-24 21:42:25 +09:00
ken-mu d398cd47d7 mruby-time: remove test case less than Dec 31 23:59:58 1969 2018-01-21 02:47:17 +00:00
ken-mu a1f36316d2 mruby-time: Fix mruby specific timegm() cannot return minus 2018-01-20 13:09:58 +00:00
ksss 397f1fc946 Fix infinity loop
And some cases should raise FloatDomainError
2017-03-28 10:58:18 +09:00
Yukihiro "Matz" Matsumoto 277e81a7c9 removed failing test for Time#initialize; ref #3295 2016-11-26 12:02:46 +09:00
Bouke van der Bijl b7f9a58757 Fix null pointer dereference in mrb_time_initialize
Reported by https://hackerone.com/raydot
2016-11-25 11:15:57 -05:00
Lukas Joeressen 95fa22a65c Rounding errors could make time_alloc imprecise 2015-06-15 15:03:44 +02:00
take_cheeze 77047b5ed4 Test all ISO defined classes direct superclass except Object class.
Move mrbgems ISO direct superclass test to `superclass.rb`.
Skips test if class isn't defined.
Close #2332.
2014-06-15 17:07:01 +09:00
take_cheeze f0154d6e3a Remove Time defined check in test. 2014-05-07 21:21:18 +09:00
take_cheeze 7bab3d0bb2 Implement day of week methods. 2014-05-05 22:41:16 +09:00
Yuichiro MASUI f4485ee1eb s/mgem/mrbgem/ 2013-02-28 11:55:22 +09:00