Skip to content

Commit eb7016b

Browse files
committed
Implement Time#strptime
1 parent 9a98b70 commit eb7016b

File tree

4 files changed

+915
-78
lines changed

4 files changed

+915
-78
lines changed

internal/numeric.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ double ruby_float_step_size(double beg, double end, double unit, int excl);
6262
int ruby_float_step(VALUE from, VALUE to, VALUE step, int excl, int allow_endless);
6363
int rb_num_negative_p(VALUE);
6464
VALUE rb_int_succ(VALUE num);
65+
VALUE rb_int_pred(VALUE num);
6566
VALUE rb_float_uminus(VALUE num);
6667
VALUE rb_int_plus(VALUE x, VALUE y);
6768
VALUE rb_float_plus(VALUE x, VALUE y);

numeric.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3817,7 +3817,7 @@ rb_int_succ(VALUE num)
38173817
*
38183818
*/
38193819

3820-
static VALUE
3820+
VALUE
38213821
rb_int_pred(VALUE num)
38223822
{
38233823
if (FIXNUM_P(num)) {

test/ruby/test_time.rb

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,4 +1515,133 @@ def test_xmlschema_encode
15151515
assert_equal("-10000-01-01T00:00:00Z", Time.utc(-10000).__send__(method))
15161516
end
15171517
end
1518+
1519+
def test_strptime
1520+
t0 = Time.at(0)
1521+
t = t0.strptime("28/Aug/2005:06:54:20 +0000", "%d/%b/%Y:%T %z")
1522+
assert_equal(Time.new(2005, 8, 28, 06, 54, 20, "+0000"), t)
1523+
assert_equal(t0, t0.strptime("", ""))
1524+
end
1525+
1526+
def test_strptime_s_z
1527+
t2000 = Time.utc(2000)
1528+
1529+
assert_equal(Time.at(1).localtime, t2000.strptime("1", "%s"))
1530+
assert_not_predicate(t2000.strptime("0", "%s"), :utc?)
1531+
1532+
t = t2000.strptime("0 +0100", "%s %z")
1533+
assert_equal(0, t.to_r)
1534+
assert_equal(3600, t.utc_offset)
1535+
1536+
t = t2000.strptime("0 UTC", "%s %z")
1537+
assert_equal(0, t.to_r)
1538+
assert_equal(0, t.utc_offset)
1539+
assert_predicate(t, :utc?)
1540+
1541+
t = t2000.strptime("+05:43:21", "%z")
1542+
assert_equal(5*3600+43*60+21, t.utc_offset)
1543+
t = t2000.strptime("+054321", "%z")
1544+
assert_equal(5*3600+43*60+21, t.utc_offset)
1545+
t = t2000.strptime("+05:43", "%z")
1546+
assert_equal(5*3600+43*60, t.utc_offset)
1547+
t = t2000.strptime("+0543", "%z")
1548+
assert_equal(5*3600+43*60, t.utc_offset)
1549+
t = t2000.strptime("+05", "%z")
1550+
assert_equal(5*3600, t.utc_offset)
1551+
1552+
t = t2000.strptime("-05:43:21", "%z")
1553+
assert_equal(-5*3600-43*60-21, t.utc_offset)
1554+
t = t2000.strptime("-054321", "%z")
1555+
assert_equal(-5*3600-43*60-21, t.utc_offset)
1556+
t = t2000.strptime("-05:43", "%z")
1557+
assert_equal(-5*3600-43*60, t.utc_offset)
1558+
t = t2000.strptime("-0543", "%z")
1559+
assert_equal(-5*3600-43*60, t.utc_offset)
1560+
t = t2000.strptime("-05", "%z")
1561+
assert_equal(-5*3600, t.utc_offset)
1562+
1563+
assert_raise(ArgumentError) {t2000.strptime("+05:4321", "%z")}
1564+
assert_raise(ArgumentError) {t2000.strptime("+0543:21", "%z")}
1565+
assert_raise(ArgumentError) {t2000.strptime("+6543", "%z")}
1566+
end
1567+
1568+
def test_strptime_s_N
1569+
t0 = Time.at(0)
1570+
assert_equal(Time.at(1, 500000), t0.strptime("1.5", "%s.%N"))
1571+
assert_equal(Time.at(-2, 500000), t0.strptime("-1.5", "%s.%N"))
1572+
t = t0.strptime("1.000000000001", "%s.%N")
1573+
assert_equal(1, t.to_i)
1574+
assert_equal(Rational("0.000000000001"), t.subsec)
1575+
t = t0.strptime("-1.000000000001", "%s.%N")
1576+
assert_equal(-2, t.to_i)
1577+
assert_equal(1-Rational("0.000000000001"), t.subsec)
1578+
end
1579+
1580+
def test_strptime_Ymd_z
1581+
t0 = Time.at(0)
1582+
t = t0.strptime("20010203 -0200", "%Y%m%d %z")
1583+
assert_equal(2001, t.year)
1584+
assert_equal(2, t.mon)
1585+
assert_equal(3, t.day)
1586+
assert_equal(0, t.hour)
1587+
assert_equal(0, t.min)
1588+
assert_equal(0, t.sec)
1589+
assert_equal(-7200, t.utc_offset)
1590+
t = t0.strptime("20010203 UTC", "%Y%m%d %z")
1591+
assert_equal(2001, t.year)
1592+
assert_equal(2, t.mon)
1593+
assert_equal(3, t.day)
1594+
assert_equal(0, t.hour)
1595+
assert_equal(0, t.min)
1596+
assert_equal(0, t.sec)
1597+
assert_equal(0, t.utc_offset)
1598+
assert_equal(true, t.utc?)
1599+
end
1600+
1601+
def test_strptime_j
1602+
t0 = Time.at(0)
1603+
t = t0.strptime("2018-365", "%Y-%j")
1604+
assert_equal(2018, t.year)
1605+
assert_equal(12, t.mon)
1606+
assert_equal(31, t.day)
1607+
assert_equal(0, t.hour)
1608+
assert_equal(0, t.min)
1609+
assert_equal(0, t.sec)
1610+
t = t0.strptime("2018-091", "%Y-%j")
1611+
assert_equal(2018, t.year)
1612+
assert_equal(4, t.mon)
1613+
assert_equal(1, t.day)
1614+
end
1615+
1616+
def test_strptime_p
1617+
t0 = Time.at(0)
1618+
t = t0.strptime("3am", "%I%p")
1619+
assert_equal(3, t.hour)
1620+
t = t0.strptime("3pm", "%I%p")
1621+
assert_equal(15, t.hour)
1622+
t = t0.strptime("3a.m.", "%I%p")
1623+
assert_equal(3, t.hour)
1624+
t = t0.strptime("3p.m.", "%I%p")
1625+
assert_equal(15, t.hour)
1626+
t = t0.strptime("3AM", "%I%p")
1627+
assert_equal(3, t.hour)
1628+
t = t0.strptime("3PM", "%I%p")
1629+
assert_equal(15, t.hour)
1630+
t = t0.strptime("3A.M.", "%I%p")
1631+
assert_equal(3, t.hour)
1632+
t = t0.strptime("3P.M.", "%I%p")
1633+
assert_equal(15, t.hour)
1634+
end
1635+
1636+
def test_strptime_wuvg
1637+
t0 = Time.at(0)
1638+
assert_equal(Time.local(2019, 1, 30), t0.strptime("3 4 2019", "%w %W %Y"))
1639+
assert_equal(Time.local(2019, 2, 7), t0.strptime("4 5 2019", "%u %U %Y"))
1640+
assert_equal(Time.local(2019, 1, 28), t0.strptime("4 2019", "%W %Y"))
1641+
assert_equal(Time.local(2019, 2, 3), t0.strptime("5 2019", "%U %Y"))
1642+
assert_equal(Time.local(2019, 1, 1), t0.strptime("1 2 2019", "%V %w %G"))
1643+
assert_equal(Time.local(2016, 1, 1), t0.strptime("53 5 15", "%V %w %g"))
1644+
assert_equal(Time.local(2018, 12, 31), t0.strptime("1 2019", "%V %G"))
1645+
assert_equal(Time.local(2015, 12, 28), t0.strptime("53 15", "%V %g"))
1646+
end
15181647
end

0 commit comments

Comments
 (0)