GitHub

@@ -0,0 +1,151 @@

1+

assert('Time#strftime') do

2+

t = Time.now

3+

assert_true t.respond_to?(:strftime)

4+

end

5+6+

assert('Time#strftime with basic formats') do

7+

t = Time.gm(2023, 12, 25, 10, 30, 45)

8+9+

assert_equal '2023', t.strftime('%Y')

10+

assert_equal '23', t.strftime('%y')

11+

assert_equal '12', t.strftime('%m')

12+

assert_equal '25', t.strftime('%d')

13+

assert_equal '10', t.strftime('%H')

14+

assert_equal '30', t.strftime('%M')

15+

assert_equal '45', t.strftime('%S')

16+

end

17+18+

assert('Time#strftime with combined formats') do

19+

t = Time.gm(2023, 12, 25, 10, 30, 45)

20+21+

assert_equal '2023-12-25', t.strftime('%Y-%m-%d')

22+

assert_equal '10:30:45', t.strftime('%H:%M:%S')

23+

assert_equal '2023-12-25 10:30:45', t.strftime('%Y-%m-%d %H:%M:%S')

24+

end

25+26+

assert('Time#strftime with weekday formats') do

27+

# 2023-12-25 is Monday

28+

t = Time.gm(2023, 12, 25)

29+30+

result = t.strftime('%A')

31+

assert_true result.include?('Mon') || result == 'Monday'

32+33+

result = t.strftime('%a')

34+

assert_true result.length >= 2

35+36+

assert_equal '1', t.strftime('%w') # Monday is day 1

37+

end

38+39+

assert('Time#strftime with month formats') do

40+

t = Time.gm(2023, 12, 25)

41+42+

result = t.strftime('%B')

43+

assert_true result.include?('Dec') || result == 'December'

44+45+

result = t.strftime('%b')

46+

assert_true result.length >= 2

47+

end

48+49+

assert('Time#strftime with literal percent') do

50+

t = Time.gm(2023, 12, 25)

51+52+

assert_equal '%', t.strftime('%%')

53+

assert_equal '100%', t.strftime('100%%')

54+

assert_equal '2023%12', t.strftime('%Y%%%m')

55+

end

56+57+

assert('Time#strftime with empty format') do

58+

t = Time.now

59+60+

assert_equal '', t.strftime('')

61+

end

62+63+

assert('Time#strftime with no format specifiers') do

64+

t = Time.now

65+66+

assert_equal 'hello', t.strftime('hello')

67+

assert_equal 'test123', t.strftime('test123')

68+

end

69+70+

assert('Time#strftime with NUL byte') do

71+

t = Time.gm(2023, 12, 25)

72+73+

result = t.strftime("foo\0bar")

74+

assert_equal 7, result.length

75+

assert_equal 'f', result[0]

76+

assert_equal 'o', result[1]

77+

assert_equal 'o', result[2]

78+

assert_equal "\0", result[3]

79+

assert_equal 'b', result[4]

80+

assert_equal 'a', result[5]

81+

assert_equal 'r', result[6]

82+

end

83+84+

assert('Time#strftime with NUL and format specifiers') do

85+

t = Time.gm(2023, 12, 25)

86+87+

result = t.strftime("year\0%Y")

88+

assert_true result.length >= 9 # "year\0" (5) + "2023" (4)

89+

assert_true result.include?("\0")

90+

# Check last 4 characters are "2023"

91+

assert_equal '2023', result[-4, 4]

92+

end

93+94+

assert('Time#strftime with multiple NULs') do

95+

t = Time.now

96+97+

result = t.strftime("\0\0")

98+

assert_equal 2, result.length

99+

assert_equal "\0\0", result

100+

end

101+102+

assert('Time#strftime with NUL at beginning') do

103+

t = Time.gm(2023, 1, 1)

104+105+

result = t.strftime("\0%Y")

106+

assert_equal 5, result.length

107+

assert_equal "\0", result[0]

108+

assert_equal '2023', result[1, 4]

109+

end

110+111+

assert('Time#strftime with NUL at end') do

112+

t = Time.gm(2023, 1, 1)

113+114+

result = t.strftime("%Y\0")

115+

assert_equal 5, result.length

116+

assert_equal '2023', result[0, 4]

117+

assert_equal "\0", result[4]

118+

end

119+120+

assert('Time#strftime preserves timezone') do

121+

t_utc = Time.utc(2023, 1, 1, 12, 0, 0)

122+

t_local = Time.local(2023, 1, 1, 12, 0, 0)

123+124+

# Both should format their time correctly

125+

assert_equal '12', t_utc.strftime('%H')

126+

assert_equal '12', t_local.strftime('%H')

127+

end

128+129+

assert('Time#strftime with various time components') do

130+

t = Time.gm(2023, 6, 15, 14, 23, 7)

131+132+

assert_equal '6', t.strftime('%-m') if t.strftime('%-m') != '' # Skip if platform doesn't support %-

133+

assert_equal '15', t.strftime('%d')

134+

assert_equal '14', t.strftime('%H')

135+

assert_equal '23', t.strftime('%M')

136+

assert_equal '07', t.strftime('%S')

137+

end

138+139+

assert('Time#strftime argument type error') do

140+

t = Time.now

141+142+

assert_raise(TypeError) { t.strftime(123) }

143+

assert_raise(TypeError) { t.strftime(nil) }

144+

end

145+146+

assert('Time#strftime argument count error') do

147+

t = Time.now

148+149+

assert_raise(ArgumentError) { t.strftime }

150+

assert_raise(ArgumentError) { t.strftime('%Y', '%m') }

151+

end

Read the original on github.com ↗