This commit is contained in:
Matheus Richard 2025-08-15 11:07:28 +09:00 committed by GitHub
commit 4b0e89f1e9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 53 additions and 0 deletions

View file

@ -1515,4 +1515,18 @@ class TestTime < Test::Unit::TestCase
assert_equal("-10000-01-01T00:00:00Z", Time.utc(-10000).__send__(method))
end
end
def test_am
assert Time.new(2025, 8, 6, 0, 0, 0).am?
assert Time.new(2025, 8, 6, 11, 59, 59).am?
refute Time.new(2025, 8, 6, 12, 0, 0).am?
refute Time.new(2025, 8, 6, 23, 59, 0).am?
end
def test_pm
assert Time.new(2025, 8, 6, 12, 0, 0).pm?
assert Time.new(2025, 8, 6, 23, 59, 59).pm?
refute Time.new(2025, 8, 6, 0, 0, 0).pm?
refute Time.new(2025, 8, 6, 11, 59, 59).pm?
end
end

39
time.c
View file

@ -4984,6 +4984,42 @@ time_saturday(VALUE time)
wday_p(6);
}
/*
* call-seq:
* am? -> true or false
*
* Returns +true+ if +self+ is before noon, +false+ otherwise:
*
* t = Time.utc(2000, 1, 1, 11, 59, 59) # 2000-01-01 11:59:59 UTC
* t.am? # => true
*
* Related: Time#pm?
*/
static VALUE
time_am(VALUE time)
{
return RBOOL(time_hour(time) < INT2FIX(12));
}
/*
* call-seq:
* pm? -> true or false
*
* Returns +true+ if +self+ is after noon, +false+ otherwise:
*
* t = Time.utc(2000, 1, 1, 13, 59, 59) # 2000-01-01 13:59:59 UTC
* t.pm? # => true
*
* Related: Time#am?
*/
static VALUE
time_pm(VALUE time)
{
return RBOOL(time_hour(time) >= INT2FIX(12));
}
/*
* call-seq:
* yday -> integer
@ -6041,6 +6077,9 @@ Init_Time(void)
rb_define_method(rb_cTime, "friday?", time_friday, 0);
rb_define_method(rb_cTime, "saturday?", time_saturday, 0);
rb_define_method(rb_cTime, "am?", time_am, 0);
rb_define_method(rb_cTime, "pm?", time_pm, 0);
rb_define_method(rb_cTime, "tv_sec", time_to_i, 0);
rb_define_method(rb_cTime, "tv_usec", time_usec, 0);
rb_define_method(rb_cTime, "usec", time_usec, 0);