PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/test/date/test_date_strftime.rb

http://github.com/ruby/ruby
Ruby | 439 lines | 385 code | 53 blank | 1 comment | 4 complexity | 9dc43788df72ab6d76fe7369353b2170 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # frozen_string_literal: true
  2. require 'test/unit'
  3. require 'date'
  4. class TestDateStrftime < Test::Unit::TestCase
  5. STRFTIME_2001_02_03 = {
  6. '%A'=>['Saturday',{:wday=>6}],
  7. '%a'=>['Sat',{:wday=>6}],
  8. '%B'=>['February',{:mon=>2}],
  9. '%b'=>['Feb',{:mon=>2}],
  10. '%c'=>['Sat Feb 3 00:00:00 2001',
  11. {:wday=>6,:mon=>2,:mday=>3,:hour=>0,:min=>0,:sec=>0,:year=>2001}],
  12. '%d'=>['03',{:mday=>3}],
  13. '%e'=>[' 3',{:mday=>3}],
  14. '%H'=>['00',{:hour=>0}],
  15. '%I'=>['12',{:hour=>0}],
  16. '%j'=>['034',{:yday=>34}],
  17. '%M'=>['00',{:min=>0}],
  18. '%m'=>['02',{:mon=>2}],
  19. '%p'=>['AM',{}],
  20. '%S'=>['00',{:sec=>0}],
  21. '%U'=>['04',{:wnum0=>4}],
  22. '%W'=>['05',{:wnum1=>5}],
  23. '%X'=>['00:00:00',{:hour=>0,:min=>0,:sec=>0}],
  24. '%x'=>['02/03/01',{:mon=>2,:mday=>3,:year=>2001}],
  25. '%Y'=>['2001',{:year=>2001}],
  26. '%y'=>['01',{:year=>2001}],
  27. '%Z'=>['+00:00',{:zone=>'+00:00',:offset=>0}],
  28. '%%'=>['%',{}],
  29. '%C'=>['20',{}],
  30. '%D'=>['02/03/01',{:mon=>2,:mday=>3,:year=>2001}],
  31. '%F'=>['2001-02-03',{:year=>2001,:mon=>2,:mday=>3}],
  32. '%G'=>['2001',{:cwyear=>2001}],
  33. '%g'=>['01',{:cwyear=>2001}],
  34. '%h'=>['Feb',{:mon=>2}],
  35. '%k'=>[' 0',{:hour=>0}],
  36. '%L'=>['000',{:sec_fraction=>0}],
  37. '%l'=>['12',{:hour=>0}],
  38. '%N'=>['000000000',{:sec_fraction=>0}],
  39. '%n'=>["\n",{}],
  40. '%P'=>['am',{}],
  41. '%Q'=>['981158400000',{:seconds=>981158400.to_r}],
  42. '%R'=>['00:00',{:hour=>0,:min=>0}],
  43. '%r'=>['12:00:00 AM',{:hour=>0,:min=>0,:sec=>0}],
  44. '%s'=>['981158400',{:seconds=>981158400}],
  45. '%T'=>['00:00:00',{:hour=>0,:min=>0,:sec=>0}],
  46. '%t'=>["\t",{}],
  47. '%u'=>['6',{:cwday=>6}],
  48. '%V'=>['05',{:cweek=>5}],
  49. '%v'=>[' 3-Feb-2001',{:mday=>3,:mon=>2,:year=>2001}],
  50. '%z'=>['+0000',{:zone=>'+0000',:offset=>0}],
  51. '%+'=>['Sat Feb 3 00:00:00 +00:00 2001',
  52. {:wday=>6,:mon=>2,:mday=>3,
  53. :hour=>0,:min=>0,:sec=>0,:zone=>'+00:00',:offset=>0,:year=>2001}],
  54. }
  55. STRFTIME_2001_02_03_CVS19 = {
  56. }
  57. STRFTIME_2001_02_03_GNUext = {
  58. '%:z'=>['+00:00',{:zone=>'+00:00',:offset=>0}],
  59. '%::z'=>['+00:00:00',{:zone=>'+00:00:00',:offset=>0}],
  60. '%:::z'=>['+00',{:zone=>'+00',:offset=>0}],
  61. }
  62. STRFTIME_2001_02_03.update(STRFTIME_2001_02_03_CVS19)
  63. STRFTIME_2001_02_03.update(STRFTIME_2001_02_03_GNUext)
  64. def test_strftime
  65. d = Date.new(2001,2,3)
  66. STRFTIME_2001_02_03.each do |f, s|
  67. assert_equal(s[0], d.strftime(f), [f, s].inspect)
  68. case f[-1,1]
  69. when 'c', 'C', 'x', 'X', 'y', 'Y'
  70. f2 = f.sub(/\A%/, '%E')
  71. assert_equal(s[0], d.strftime(f2), [f2, s].inspect)
  72. else
  73. f2 = f.sub(/\A%/, '%E')
  74. assert_equal(f2, d.strftime(f2), [f2, s].inspect)
  75. end
  76. case f[-1,1]
  77. when 'd', 'e', 'H', 'k', 'I', 'l', 'm', 'M', 'S', 'u', 'U', 'V', 'w', 'W', 'y'
  78. f2 = f.sub(/\A%/, '%O')
  79. assert_equal(s[0], d.strftime(f2), [f2, s].inspect)
  80. else
  81. f2 = f.sub(/\A%/, '%O')
  82. assert_equal(f2, d.strftime(f2), [f2, s].inspect)
  83. end
  84. end
  85. end
  86. def test_strftime__2
  87. d = Date.new(2001,2,3)
  88. assert_equal('2001-02-03', d.strftime)
  89. d = DateTime.new(2001,2,3)
  90. assert_equal('2001-02-03T00:00:00+00:00', d.strftime)
  91. assert_equal('', d.strftime(''))
  92. assert_equal("\s"*3, d.strftime("\s"*3))
  93. assert_equal("\tfoo\n\000\r", d.strftime("\tfoo\n\000\r"))
  94. assert_equal("%\n", d.strftime("%\n")) # gnu
  95. assert_equal('Saturday'*1024 + ',', d.strftime('%A'*1024 + ','))
  96. assert_equal('%%', d.strftime('%%%'))
  97. assert_equal('Anton von Webern', d.strftime('Anton von Webern'))
  98. d = DateTime.new(2001,2,3, 1,2,3)
  99. assert_equal('2001-02-03T01:02:03+00:00', d.strftime)
  100. assert_equal('AM', d.strftime('%p'))
  101. assert_equal('am', d.strftime('%P'))
  102. d = DateTime.new(2001,2,3, 13,14,15)
  103. assert_equal('2001-02-03T13:14:15+00:00', d.strftime)
  104. assert_equal('PM', d.strftime('%p'))
  105. assert_equal('pm', d.strftime('%P'))
  106. end
  107. def test_strftime__3_1
  108. (Date.new(1970,1,1)..Date.new(2037,12,31)).each do |d|
  109. t = Time.utc(d.year,d.mon,d.mday)
  110. assert_equal(t.strftime('%U'), d.strftime('%U'))
  111. assert_equal(t.strftime('%W'), d.strftime('%W'))
  112. end
  113. end
  114. def test_strftime__3_2
  115. s = Time.now.strftime('%G')
  116. skip if s.empty? || s == '%G'
  117. (Date.new(1970,1,1)..Date.new(2037,12,31)).each do |d|
  118. t = Time.utc(d.year,d.mon,d.mday)
  119. assert_equal(t.strftime('%G'), d.strftime('%G'))
  120. assert_equal(t.strftime('%g'), d.strftime('%g'))
  121. assert_equal(t.strftime('%V'), d.strftime('%V'))
  122. assert_equal(t.strftime('%u'), d.strftime('%u'))
  123. end
  124. end
  125. def test_strftime__4
  126. s = '2006-08-08T23:15:33.123456789'
  127. f = '%FT%T.%N'
  128. d = DateTime.parse(s)
  129. assert_equal(s, d.strftime(f))
  130. d = DateTime.strptime(s, f)
  131. assert_equal(s, d.strftime(f))
  132. s = '2006-08-08T23:15:33.123456789'
  133. f = '%FT%T.%N'
  134. d = DateTime.parse(s + '123456789')
  135. assert_equal(s, d.strftime(f))
  136. d = DateTime.strptime(s + '123456789', f)
  137. assert_equal(s, d.strftime(f))
  138. si = '2006-08-08T23:15:33.9'
  139. so = '2006-08-08T23:15:33.900000000'
  140. f = '%FT%T.%N'
  141. d = DateTime.parse(si)
  142. assert_equal(so, d.strftime(f))
  143. d = DateTime.strptime(si, f)
  144. assert_equal(so, d.strftime(f))
  145. s = '2006-08-08T23:15:33.123'
  146. f = '%FT%T.%L'
  147. d = DateTime.parse(s)
  148. assert_equal(s, d.strftime(f))
  149. d = DateTime.strptime(s, f)
  150. assert_equal(s, d.strftime(f))
  151. s = '2006-08-08T23:15:33.123'
  152. f = '%FT%T.%L'
  153. d = DateTime.parse(s + '123')
  154. assert_equal(s, d.strftime(f))
  155. d = DateTime.strptime(s + '123', f)
  156. assert_equal(s, d.strftime(f))
  157. si = '2006-08-08T23:15:33.9'
  158. so = '2006-08-08T23:15:33.900'
  159. f = '%FT%T.%L'
  160. d = DateTime.parse(si)
  161. assert_equal(so, d.strftime(f))
  162. d = DateTime.strptime(si, f)
  163. assert_equal(so, d.strftime(f))
  164. end
  165. def test_strftime__offset
  166. s = '2006-08-08T23:15:33'
  167. (-24..24).collect{|x| '%+.2d' % x}.each do |hh|
  168. %w(00 30).each do |mm|
  169. r = hh + mm
  170. next if r.end_with?('2430')
  171. d = DateTime.parse(s + hh + mm)
  172. assert_equal(r, d.strftime('%z'))
  173. end
  174. end
  175. %w[+2430 -2430].each do |r|
  176. assert_warning(/invalid offset/) do
  177. DateTime.parse(s + r)
  178. end
  179. end
  180. end
  181. def test_strftime_milli
  182. s = '1970-01-01T00:00:00.123456789'
  183. d = DateTime.parse(s)
  184. assert_equal('123', d.strftime('%Q'))
  185. s = '1970-01-02T02:03:04.123456789'
  186. d = DateTime.parse(s)
  187. assert_equal('93784123', d.strftime('%Q'))
  188. end
  189. def test_strftime__minus
  190. d = DateTime.new(1969, 12, 31, 23, 59, 59)
  191. assert_equal('-1', d.strftime('%s'))
  192. assert_equal('-1000', d.strftime('%Q'))
  193. end
  194. def test_strftime__gnuext # coreutils
  195. d = DateTime.new(2006,8,8,23,15,33,9.to_r/24)
  196. assert_equal('2006', d.strftime('%-Y'))
  197. assert_equal('2006', d.strftime('%-5Y'))
  198. assert_equal('02006', d.strftime('%5Y'))
  199. assert_equal('2006', d.strftime('%_Y'))
  200. assert_equal(' 2006', d.strftime('%_5Y'))
  201. assert_equal('02006', d.strftime('%05Y'))
  202. assert_equal('8', d.strftime('%-d'))
  203. assert_equal('8', d.strftime('%-3d'))
  204. assert_equal('008', d.strftime('%3d'))
  205. assert_equal(' 8', d.strftime('%_d'))
  206. assert_equal(' 8', d.strftime('%_3d'))
  207. assert_equal('008', d.strftime('%03d'))
  208. assert_equal('8', d.strftime('%-e'))
  209. assert_equal('8', d.strftime('%-3e'))
  210. assert_equal(' 8', d.strftime('%3e'))
  211. assert_equal(' 8', d.strftime('%_e'))
  212. assert_equal(' 8', d.strftime('%_3e'))
  213. assert_equal('008', d.strftime('%03e'))
  214. assert_equal('Tuesday', d.strftime('%-10A'))
  215. assert_equal(' Tuesday', d.strftime('%10A'))
  216. assert_equal(' Tuesday', d.strftime('%_10A'))
  217. assert_equal('000Tuesday', d.strftime('%010A'))
  218. assert_equal('TUESDAY', d.strftime('%^A'))
  219. assert_equal('TUESDAY', d.strftime('%#A'))
  220. assert_equal('Tue', d.strftime('%-6a'))
  221. assert_equal(' Tue', d.strftime('%6a'))
  222. assert_equal(' Tue', d.strftime('%_6a'))
  223. assert_equal('000Tue', d.strftime('%06a'))
  224. assert_equal('TUE', d.strftime('%^a'))
  225. assert_equal('TUE', d.strftime('%#a'))
  226. assert_equal(' TUE', d.strftime('%#6a'))
  227. assert_equal('August', d.strftime('%-10B'))
  228. assert_equal(' August', d.strftime('%10B'))
  229. assert_equal(' August', d.strftime('%_10B'))
  230. assert_equal('0000August', d.strftime('%010B'))
  231. assert_equal('AUGUST', d.strftime('%^B'))
  232. assert_equal('AUGUST', d.strftime('%#B'))
  233. assert_equal('Aug', d.strftime('%-6b'))
  234. assert_equal(' Aug', d.strftime('%6b'))
  235. assert_equal(' Aug', d.strftime('%_6b'))
  236. assert_equal('000Aug', d.strftime('%06b'))
  237. assert_equal('AUG', d.strftime('%^b'))
  238. assert_equal('AUG', d.strftime('%#b'))
  239. assert_equal(' AUG', d.strftime('%#6b'))
  240. assert_equal('Aug', d.strftime('%-6h'))
  241. assert_equal(' Aug', d.strftime('%6h'))
  242. assert_equal(' Aug', d.strftime('%_6h'))
  243. assert_equal('000Aug', d.strftime('%06h'))
  244. assert_equal('AUG', d.strftime('%^h'))
  245. assert_equal('AUG', d.strftime('%#h'))
  246. assert_equal(' AUG', d.strftime('%#6h'))
  247. assert_equal('PM', d.strftime('%^p'))
  248. assert_equal('pm', d.strftime('%#p'))
  249. assert_equal('PM', d.strftime('%^P'))
  250. assert_equal('PM', d.strftime('%#P'))
  251. assert_equal('+000900', d.strftime('%7z'))
  252. assert_equal(' +900', d.strftime('%_7z'))
  253. assert_equal('+09:00', d.strftime('%:z'))
  254. assert_equal('+0009:00', d.strftime('%8:z'))
  255. assert_equal(' +9:00', d.strftime('%_8:z'))
  256. assert_equal('+09:00:00', d.strftime('%::z'))
  257. assert_equal('+0009:00:00', d.strftime('%11::z'))
  258. assert_equal(' +9:00:00', d.strftime('%_11::z'))
  259. assert_equal('+09', d.strftime('%:::z'))
  260. assert_equal('+0009', d.strftime('%5:::z'))
  261. assert_equal(' +9', d.strftime('%_5:::z'))
  262. assert_equal('+9', d.strftime('%-:::z'))
  263. d = DateTime.new(-200,8,8,23,15,33,9.to_r/24)
  264. assert_equal('-0200', d.strftime('%Y'))
  265. assert_equal('-200', d.strftime('%-Y'))
  266. assert_equal('-200', d.strftime('%-5Y'))
  267. assert_equal('-0200', d.strftime('%5Y'))
  268. assert_equal(' -200', d.strftime('%_Y'))
  269. assert_equal(' -200', d.strftime('%_5Y'))
  270. assert_equal('-0200', d.strftime('%05Y'))
  271. d = DateTime.new(-2000,8,8,23,15,33,9.to_r/24)
  272. assert_equal('-2000', d.strftime('%Y'))
  273. assert_equal('-2000', d.strftime('%-Y'))
  274. assert_equal('-2000', d.strftime('%-5Y'))
  275. assert_equal('-2000', d.strftime('%5Y'))
  276. assert_equal('-2000', d.strftime('%_Y'))
  277. assert_equal('-2000', d.strftime('%_5Y'))
  278. assert_equal('-2000', d.strftime('%05Y'))
  279. end
  280. def test_strftime__gnuext_LN # coreutils
  281. d = DateTime.parse('2008-11-25T00:11:22.0123456789')
  282. assert_equal('012', d.strftime('%L'))
  283. assert_equal('012', d.strftime('%0L'))
  284. assert_equal('0', d.strftime('%1L'))
  285. assert_equal('01', d.strftime('%2L'))
  286. assert_equal('01234567890', d.strftime('%11L'))
  287. assert_equal('01234567890', d.strftime('%011L'))
  288. assert_equal('01234567890', d.strftime('%_11L'))
  289. assert_equal('012345678', d.strftime('%N'))
  290. assert_equal('012345678', d.strftime('%0N'))
  291. assert_equal('0', d.strftime('%1N'))
  292. assert_equal('01', d.strftime('%2N'))
  293. assert_equal('01234567890', d.strftime('%11N'))
  294. assert_equal('01234567890', d.strftime('%011N'))
  295. assert_equal('01234567890', d.strftime('%_11N'))
  296. end
  297. def test_strftime__gnuext_z # coreutils
  298. d = DateTime.parse('2006-08-08T23:15:33+09:08:07')
  299. assert_equal('+0908', d.strftime('%z'))
  300. assert_equal('+09:08', d.strftime('%:z'))
  301. assert_equal('+09:08:07', d.strftime('%::z'))
  302. assert_equal('+09:08:07', d.strftime('%:::z'))
  303. end
  304. def test_strftime__gnuext_complex
  305. d = DateTime.parse('2001-02-03T04:05:06+09:00')
  306. assert_equal('Sat Feb 3 04:05:06 2001', d.strftime('%-100c'))
  307. assert_equal('Sat Feb 3 04:05:06 2001'.rjust(100), d.strftime('%100c'))
  308. assert_equal('Sat Feb 3 04:05:06 2001'.rjust(100), d.strftime('%_100c'))
  309. assert_equal('Sat Feb 3 04:05:06 2001'.rjust(100, '0'), d.strftime('%0100c'))
  310. assert_equal('SAT FEB 3 04:05:06 2001', d.strftime('%^c'))
  311. assert_equal('Sat Feb 3 04:05:06 +09:00 2001', d.strftime('%-100+'))
  312. assert_equal('Sat Feb 3 04:05:06 +09:00 2001'.rjust(100), d.strftime('%100+'))
  313. assert_equal('Sat Feb 3 04:05:06 +09:00 2001'.rjust(100), d.strftime('%_100+'))
  314. assert_equal('Sat Feb 3 04:05:06 +09:00 2001'.rjust(100, '0'), d.strftime('%0100+'))
  315. assert_equal('SAT FEB 3 04:05:06 +09:00 2001', d.strftime('%^+'))
  316. end
  317. def test__different_format
  318. d = Date.new(2001,2,3)
  319. assert_equal('Sat Feb 3 00:00:00 2001', d.ctime)
  320. assert_equal(d.ctime, d.asctime)
  321. assert_equal('2001-02-03', d.iso8601)
  322. assert_equal(d.xmlschema, d.iso8601)
  323. assert_equal('2001-02-03T00:00:00+00:00', d.rfc3339)
  324. assert_equal('Sat, 3 Feb 2001 00:00:00 +0000', d.rfc2822)
  325. assert_equal(d.rfc822, d.rfc2822)
  326. assert_equal('Sat, 03 Feb 2001 00:00:00 GMT', d.httpdate)
  327. assert_equal('H13.02.03', d.jisx0301)
  328. d = DateTime.new(2001,2,3)
  329. assert_equal('Sat Feb 3 00:00:00 2001', d.ctime)
  330. assert_equal(d.ctime, d.asctime)
  331. assert_equal('2001-02-03T00:00:00+00:00', d.iso8601)
  332. assert_equal(d.rfc3339, d.iso8601)
  333. assert_equal(d.xmlschema, d.iso8601)
  334. assert_equal('Sat, 3 Feb 2001 00:00:00 +0000', d.rfc2822)
  335. assert_equal(d.rfc822, d.rfc2822)
  336. assert_equal('Sat, 03 Feb 2001 00:00:00 GMT', d.httpdate)
  337. assert_equal('H13.02.03T00:00:00+00:00', d.jisx0301)
  338. d2 = DateTime.parse('2001-02-03T04:05:06.123456')
  339. assert_equal('2001-02-03T04:05:06.123+00:00', d2.iso8601(3))
  340. assert_equal('2001-02-03T04:05:06.123+00:00', d2.rfc3339(3))
  341. assert_equal('H13.02.03T04:05:06.123+00:00', d2.jisx0301(3))
  342. assert_equal('2001-02-03T04:05:06.123+00:00', d2.iso8601(3.5))
  343. assert_equal('2001-02-03T04:05:06.123+00:00', d2.rfc3339(3.5))
  344. assert_equal('H13.02.03T04:05:06.123+00:00', d2.jisx0301(3.5))
  345. assert_equal('2001-02-03T04:05:06.123456000+00:00', d2.iso8601(9))
  346. assert_equal('2001-02-03T04:05:06.123456000+00:00', d2.rfc3339(9))
  347. assert_equal('H13.02.03T04:05:06.123456000+00:00', d2.jisx0301(9))
  348. assert_equal('2001-02-03T04:05:06.123456000+00:00', d2.iso8601(9.9))
  349. assert_equal('2001-02-03T04:05:06.123456000+00:00', d2.rfc3339(9.9))
  350. assert_equal('H13.02.03T04:05:06.123456000+00:00', d2.jisx0301(9.9))
  351. assert_equal('1800-01-01T00:00:00+00:00', DateTime.new(1800).jisx0301)
  352. assert_equal('1868-01-25', Date.parse('1868-01-25').jisx0301)
  353. assert_equal('1872-12-31', Date.parse('1872-12-31').jisx0301)
  354. assert_equal('M06.01.01', Date.parse('1873-01-01').jisx0301)
  355. assert_equal('M45.07.29', Date.parse('1912-07-29').jisx0301)
  356. assert_equal('T01.07.30', Date.parse('1912-07-30').jisx0301)
  357. assert_equal('T15.12.24', Date.parse('1926-12-24').jisx0301)
  358. assert_equal('S01.12.25', Date.parse('1926-12-25').jisx0301)
  359. assert_equal('S64.01.07', Date.parse('1989-01-07').jisx0301)
  360. assert_equal('H01.01.08', Date.parse('1989-01-08').jisx0301)
  361. assert_equal('H18.09.01', Date.parse('2006-09-01').jisx0301)
  362. assert_equal('H31.04.30', Date.parse('2019-04-30').jisx0301)
  363. assert_equal('R01.05.01', Date.parse('2019-05-01').jisx0301)
  364. %w(M06.01.01
  365. M45.07.29
  366. T01.07.30
  367. T15.12.24
  368. S01.12.25
  369. S64.01.07
  370. H01.01.08
  371. H18.09.01
  372. H31.04.30
  373. R01.05.01
  374. ).each do |s|
  375. assert_equal(s, Date.parse(s).jisx0301)
  376. end
  377. end
  378. def test_overflow
  379. assert_raise(ArgumentError, Errno::ERANGE) {
  380. Date.new(2000,1,1).strftime("%2147483647c")
  381. }
  382. assert_raise(ArgumentError, Errno::ERANGE) {
  383. DateTime.new(2000,1,1).strftime("%2147483647c")
  384. }
  385. end
  386. end