/vendor/rails/activesupport/test/core_ext/time_ext_test.rb

https://github.com/Epictetus/bookqueue · Ruby · 667 lines · 546 code · 79 blank · 42 comment · 0 complexity · 118d06a6f21e51633f89db45ccb6d8ab MD5 · raw file

  1. require 'abstract_unit'
  2. class TimeExtCalculationsTest < Test::Unit::TestCase
  3. def test_seconds_since_midnight
  4. assert_equal 1,Time.local(2005,1,1,0,0,1).seconds_since_midnight
  5. assert_equal 60,Time.local(2005,1,1,0,1,0).seconds_since_midnight
  6. assert_equal 3660,Time.local(2005,1,1,1,1,0).seconds_since_midnight
  7. assert_equal 86399,Time.local(2005,1,1,23,59,59).seconds_since_midnight
  8. assert_equal 60.00001,Time.local(2005,1,1,0,1,0,10).seconds_since_midnight
  9. end
  10. def test_seconds_since_midnight_at_daylight_savings_time_start
  11. with_env_tz 'US/Eastern' do
  12. # dt: US: 2005 April 3rd 2:00am ST => April 3rd 3:00am DT
  13. assert_equal 2*3600-1, Time.local(2005,4,3,1,59,59).seconds_since_midnight, 'just before DST start'
  14. assert_equal 2*3600+1, Time.local(2005,4,3,3, 0, 1).seconds_since_midnight, 'just after DST start'
  15. end
  16. with_env_tz 'NZ' do
  17. # dt: New Zealand: 2006 October 1st 2:00am ST => October 1st 3:00am DT
  18. assert_equal 2*3600-1, Time.local(2006,10,1,1,59,59).seconds_since_midnight, 'just before DST start'
  19. assert_equal 2*3600+1, Time.local(2006,10,1,3, 0, 1).seconds_since_midnight, 'just after DST start'
  20. end
  21. end
  22. def test_seconds_since_midnight_at_daylight_savings_time_end
  23. with_env_tz 'US/Eastern' do
  24. # st: US: 2005 October 30th 2:00am DT => October 30th 1:00am ST
  25. # avoid setting a time between 1:00 and 2:00 since that requires specifying whether DST is active
  26. assert_equal 1*3600-1, Time.local(2005,10,30,0,59,59).seconds_since_midnight, 'just before DST end'
  27. assert_equal 3*3600+1, Time.local(2005,10,30,2, 0, 1).seconds_since_midnight, 'just after DST end'
  28. # now set a time between 1:00 and 2:00 by specifying whether DST is active
  29. # uses: Time.local( sec, min, hour, day, month, year, wday, yday, isdst, tz )
  30. assert_equal 1*3600+30*60, Time.local(0,30,1,30,10,2005,0,0,true,ENV['TZ']).seconds_since_midnight, 'before DST end'
  31. assert_equal 2*3600+30*60, Time.local(0,30,1,30,10,2005,0,0,false,ENV['TZ']).seconds_since_midnight, 'after DST end'
  32. end
  33. with_env_tz 'NZ' do
  34. # st: New Zealand: 2006 March 19th 3:00am DT => March 19th 2:00am ST
  35. # avoid setting a time between 2:00 and 3:00 since that requires specifying whether DST is active
  36. assert_equal 2*3600-1, Time.local(2006,3,19,1,59,59).seconds_since_midnight, 'just before DST end'
  37. assert_equal 4*3600+1, Time.local(2006,3,19,3, 0, 1).seconds_since_midnight, 'just after DST end'
  38. # now set a time between 2:00 and 3:00 by specifying whether DST is active
  39. # uses: Time.local( sec, min, hour, day, month, year, wday, yday, isdst, tz )
  40. assert_equal 2*3600+30*60, Time.local(0,30,2,19,3,2006,0,0,true, ENV['TZ']).seconds_since_midnight, 'before DST end'
  41. assert_equal 3*3600+30*60, Time.local(0,30,2,19,3,2006,0,0,false,ENV['TZ']).seconds_since_midnight, 'after DST end'
  42. end
  43. end
  44. def test_beginning_of_week
  45. assert_equal Time.local(2005,1,31), Time.local(2005,2,4,10,10,10).beginning_of_week
  46. assert_equal Time.local(2005,11,28), Time.local(2005,11,28,0,0,0).beginning_of_week #monday
  47. assert_equal Time.local(2005,11,28), Time.local(2005,11,29,0,0,0).beginning_of_week #tuesday
  48. assert_equal Time.local(2005,11,28), Time.local(2005,11,30,0,0,0).beginning_of_week #wednesday
  49. assert_equal Time.local(2005,11,28), Time.local(2005,12,01,0,0,0).beginning_of_week #thursday
  50. assert_equal Time.local(2005,11,28), Time.local(2005,12,02,0,0,0).beginning_of_week #friday
  51. assert_equal Time.local(2005,11,28), Time.local(2005,12,03,0,0,0).beginning_of_week #saturday
  52. assert_equal Time.local(2005,11,28), Time.local(2005,12,04,0,0,0).beginning_of_week #sunday
  53. end
  54. def test_beginning_of_day
  55. assert_equal Time.local(2005,2,4,0,0,0), Time.local(2005,2,4,10,10,10).beginning_of_day
  56. with_env_tz 'US/Eastern' do
  57. assert_equal Time.local(2006,4,2,0,0,0), Time.local(2006,4,2,10,10,10).beginning_of_day, 'start DST'
  58. assert_equal Time.local(2006,10,29,0,0,0), Time.local(2006,10,29,10,10,10).beginning_of_day, 'ends DST'
  59. end
  60. with_env_tz 'NZ' do
  61. assert_equal Time.local(2006,3,19,0,0,0), Time.local(2006,3,19,10,10,10).beginning_of_day, 'ends DST'
  62. assert_equal Time.local(2006,10,1,0,0,0), Time.local(2006,10,1,10,10,10).beginning_of_day, 'start DST'
  63. end
  64. end
  65. def test_beginning_of_month
  66. assert_equal Time.local(2005,2,1,0,0,0), Time.local(2005,2,22,10,10,10).beginning_of_month
  67. end
  68. def test_beginning_of_quarter
  69. assert_equal Time.local(2005,1,1,0,0,0), Time.local(2005,2,15,10,10,10).beginning_of_quarter
  70. assert_equal Time.local(2005,1,1,0,0,0), Time.local(2005,1,1,0,0,0).beginning_of_quarter
  71. assert_equal Time.local(2005,10,1,0,0,0), Time.local(2005,12,31,10,10,10).beginning_of_quarter
  72. assert_equal Time.local(2005,4,1,0,0,0), Time.local(2005,6,30,23,59,59).beginning_of_quarter
  73. end
  74. def test_end_of_day
  75. assert_equal Time.local(2007,8,12,23,59,59), Time.local(2007,8,12,10,10,10).end_of_day
  76. with_env_tz 'US/Eastern' do
  77. assert_equal Time.local(2007,4,2,23,59,59), Time.local(2007,4,2,10,10,10).end_of_day, 'start DST'
  78. assert_equal Time.local(2007,10,29,23,59,59), Time.local(2007,10,29,10,10,10).end_of_day, 'ends DST'
  79. end
  80. with_env_tz 'NZ' do
  81. assert_equal Time.local(2006,3,19,23,59,59), Time.local(2006,3,19,10,10,10).end_of_day, 'ends DST'
  82. assert_equal Time.local(2006,10,1,23,59,59), Time.local(2006,10,1,10,10,10).end_of_day, 'start DST'
  83. end
  84. end
  85. def test_end_of_week
  86. assert_equal Time.local(2008,1,6,23,59,59), Time.local(2007,12,31,10,10,10).end_of_week
  87. assert_equal Time.local(2007,9,2,23,59,59), Time.local(2007,8,27,0,0,0).end_of_week #monday
  88. assert_equal Time.local(2007,9,2,23,59,59), Time.local(2007,8,28,0,0,0).end_of_week #tuesday
  89. assert_equal Time.local(2007,9,2,23,59,59), Time.local(2007,8,29,0,0,0).end_of_week #wednesday
  90. assert_equal Time.local(2007,9,2,23,59,59), Time.local(2007,8,30,0,0,0).end_of_week #thursday
  91. assert_equal Time.local(2007,9,2,23,59,59), Time.local(2007,8,31,0,0,0).end_of_week #friday
  92. assert_equal Time.local(2007,9,2,23,59,59), Time.local(2007,9,01,0,0,0).end_of_week #saturday
  93. assert_equal Time.local(2007,9,2,23,59,59), Time.local(2007,9,02,0,0,0).end_of_week #sunday
  94. end
  95. def test_end_of_month
  96. assert_equal Time.local(2005,3,31,23,59,59), Time.local(2005,3,20,10,10,10).end_of_month
  97. assert_equal Time.local(2005,2,28,23,59,59), Time.local(2005,2,20,10,10,10).end_of_month
  98. assert_equal Time.local(2005,4,30,23,59,59), Time.local(2005,4,20,10,10,10).end_of_month
  99. end
  100. def test_end_of_quarter
  101. assert_equal Time.local(2007,3,31,23,59,59), Time.local(2007,2,15,10,10,10).end_of_quarter
  102. assert_equal Time.local(2007,3,31,23,59,59), Time.local(2007,3,31,0,0,0).end_of_quarter
  103. assert_equal Time.local(2007,12,31,23,59,59), Time.local(2007,12,21,10,10,10).end_of_quarter
  104. assert_equal Time.local(2007,6,30,23,59,59), Time.local(2007,4,1,0,0,0).end_of_quarter
  105. end
  106. def test_end_of_year
  107. assert_equal Time.local(2007,12,31,23,59,59), Time.local(2007,2,22,10,10,10).end_of_year
  108. assert_equal Time.local(2007,12,31,23,59,59), Time.local(2007,12,31,10,10,10).end_of_year
  109. end
  110. def test_beginning_of_year
  111. assert_equal Time.local(2005,1,1,0,0,0), Time.local(2005,2,22,10,10,10).beginning_of_year
  112. end
  113. def test_months_ago
  114. assert_equal Time.local(2005,5,5,10), Time.local(2005,6,5,10,0,0).months_ago(1)
  115. assert_equal Time.local(2004,11,5,10), Time.local(2005,6,5,10,0,0).months_ago(7)
  116. assert_equal Time.local(2004,12,5,10), Time.local(2005,6,5,10,0,0).months_ago(6)
  117. assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).months_ago(12)
  118. assert_equal Time.local(2003,6,5,10), Time.local(2005,6,5,10,0,0).months_ago(24)
  119. end
  120. def test_months_since
  121. assert_equal Time.local(2005,7,5,10), Time.local(2005,6,5,10,0,0).months_since(1)
  122. assert_equal Time.local(2006,1,5,10), Time.local(2005,12,5,10,0,0).months_since(1)
  123. assert_equal Time.local(2005,12,5,10), Time.local(2005,6,5,10,0,0).months_since(6)
  124. assert_equal Time.local(2006,6,5,10), Time.local(2005,12,5,10,0,0).months_since(6)
  125. assert_equal Time.local(2006,1,5,10), Time.local(2005,6,5,10,0,0).months_since(7)
  126. assert_equal Time.local(2006,6,5,10), Time.local(2005,6,5,10,0,0).months_since(12)
  127. assert_equal Time.local(2007,6,5,10), Time.local(2005,6,5,10,0,0).months_since(24)
  128. assert_equal Time.local(2005,4,30,10), Time.local(2005,3,31,10,0,0).months_since(1)
  129. assert_equal Time.local(2005,2,28,10), Time.local(2005,1,29,10,0,0).months_since(1)
  130. assert_equal Time.local(2005,2,28,10), Time.local(2005,1,30,10,0,0).months_since(1)
  131. assert_equal Time.local(2005,2,28,10), Time.local(2005,1,31,10,0,0).months_since(1)
  132. end
  133. def test_years_ago
  134. assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).years_ago(1)
  135. assert_equal Time.local(1998,6,5,10), Time.local(2005,6,5,10,0,0).years_ago(7)
  136. assert_equal Time.local(2003,2,28,10), Time.local(2004,2,29,10,0,0).years_ago(1) # 1 year ago from leap day
  137. end
  138. def test_years_since
  139. assert_equal Time.local(2006,6,5,10), Time.local(2005,6,5,10,0,0).years_since(1)
  140. assert_equal Time.local(2012,6,5,10), Time.local(2005,6,5,10,0,0).years_since(7)
  141. assert_equal Time.local(2005,2,28,10), Time.local(2004,2,29,10,0,0).years_since(1) # 1 year since leap day
  142. # Failure because of size limitations of numeric?
  143. # assert_equal Time.local(2182,6,5,10), Time.local(2005,6,5,10,0,0).years_since(177)
  144. end
  145. def test_last_year
  146. assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).last_year
  147. end
  148. def test_next_year
  149. assert_equal Time.local(2006,6,5,10), Time.local(2005,6,5,10,0,0).next_year
  150. end
  151. def test_ago
  152. assert_equal Time.local(2005,2,22,10,10,9), Time.local(2005,2,22,10,10,10).ago(1)
  153. assert_equal Time.local(2005,2,22,9,10,10), Time.local(2005,2,22,10,10,10).ago(3600)
  154. assert_equal Time.local(2005,2,20,10,10,10), Time.local(2005,2,22,10,10,10).ago(86400*2)
  155. assert_equal Time.local(2005,2,20,9,9,45), Time.local(2005,2,22,10,10,10).ago(86400*2 + 3600 + 25)
  156. end
  157. def test_daylight_savings_time_crossings_backward_start
  158. with_env_tz 'US/Eastern' do
  159. # dt: US: 2005 April 3rd 4:18am
  160. assert_equal Time.local(2005,4,2,4,18,0), Time.local(2005,4,3,4,18,0).ago(86400), 'dt-1.day=>st'
  161. assert_equal Time.local(2005,4,1,4,18,0), Time.local(2005,4,2,4,18,0).ago(86400), 'st-1.day=>st'
  162. end
  163. with_env_tz 'NZ' do
  164. # dt: New Zealand: 2006 October 1st 4:18am
  165. assert_equal Time.local(2006,9,30,4,18,0), Time.local(2006,10,1,4,18,0).ago(86400), 'dt-1.day=>st'
  166. assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(86400), 'st-1.day=>st'
  167. end
  168. end
  169. def test_daylight_savings_time_crossings_backward_end
  170. with_env_tz 'US/Eastern' do
  171. # st: US: 2005 October 30th 4:03am
  172. assert_equal Time.local(2005,10,29,4,3), Time.local(2005,10,30,4,3,0).ago(86400), 'st-1.day=>dt'
  173. assert_equal Time.local(2005,10,28,4,3), Time.local(2005,10,29,4,3,0).ago(86400), 'dt-1.day=>dt'
  174. end
  175. with_env_tz 'NZ' do
  176. # st: New Zealand: 2006 March 19th 4:03am
  177. assert_equal Time.local(2006,3,18,4,3), Time.local(2006,3,19,4,3,0).ago(86400), 'st-1.day=>dt'
  178. assert_equal Time.local(2006,3,17,4,3), Time.local(2006,3,18,4,3,0).ago(86400), 'dt-1.day=>dt'
  179. end
  180. end
  181. def test_daylight_savings_time_crossings_backward_start_1day
  182. with_env_tz 'US/Eastern' do
  183. # dt: US: 2005 April 3rd 4:18am
  184. assert_equal Time.local(2005,4,2,4,18,0), Time.local(2005,4,3,4,18,0).ago(1.day), 'dt-1.day=>st'
  185. assert_equal Time.local(2005,4,1,4,18,0), Time.local(2005,4,2,4,18,0).ago(1.day), 'st-1.day=>st'
  186. end
  187. with_env_tz 'NZ' do
  188. # dt: New Zealand: 2006 October 1st 4:18am
  189. assert_equal Time.local(2006,9,30,4,18,0), Time.local(2006,10,1,4,18,0).ago(1.day), 'dt-1.day=>st'
  190. assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(1.day), 'st-1.day=>st'
  191. end
  192. end
  193. def test_daylight_savings_time_crossings_backward_end_1day
  194. with_env_tz 'US/Eastern' do
  195. # st: US: 2005 October 30th 4:03am
  196. assert_equal Time.local(2005,10,29,4,3), Time.local(2005,10,30,4,3,0).ago(1.day), 'st-1.day=>dt'
  197. assert_equal Time.local(2005,10,28,4,3), Time.local(2005,10,29,4,3,0).ago(1.day), 'dt-1.day=>dt'
  198. end
  199. with_env_tz 'NZ' do
  200. # st: New Zealand: 2006 March 19th 4:03am
  201. assert_equal Time.local(2006,3,18,4,3), Time.local(2006,3,19,4,3,0).ago(1.day), 'st-1.day=>dt'
  202. assert_equal Time.local(2006,3,17,4,3), Time.local(2006,3,18,4,3,0).ago(1.day), 'dt-1.day=>dt'
  203. end
  204. end
  205. def test_since
  206. assert_equal Time.local(2005,2,22,10,10,11), Time.local(2005,2,22,10,10,10).since(1)
  207. assert_equal Time.local(2005,2,22,11,10,10), Time.local(2005,2,22,10,10,10).since(3600)
  208. assert_equal Time.local(2005,2,24,10,10,10), Time.local(2005,2,22,10,10,10).since(86400*2)
  209. assert_equal Time.local(2005,2,24,11,10,35), Time.local(2005,2,22,10,10,10).since(86400*2 + 3600 + 25)
  210. # when out of range of Time, returns a DateTime
  211. assert_equal DateTime.civil(2038,1,20,11,59,59), Time.utc(2038,1,18,11,59,59).since(86400*2)
  212. end
  213. def test_daylight_savings_time_crossings_forward_start
  214. with_env_tz 'US/Eastern' do
  215. # st: US: 2005 April 2nd 7:27pm
  216. assert_equal Time.local(2005,4,3,19,27,0), Time.local(2005,4,2,19,27,0).since(86400), 'st+1.day=>dt'
  217. assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(86400), 'dt+1.day=>dt'
  218. end
  219. with_env_tz 'NZ' do
  220. # st: New Zealand: 2006 September 30th 7:27pm
  221. assert_equal Time.local(2006,10,1,19,27,0), Time.local(2006,9,30,19,27,0).since(86400), 'st+1.day=>dt'
  222. assert_equal Time.local(2006,10,2,19,27,0), Time.local(2006,10,1,19,27,0).since(86400), 'dt+1.day=>dt'
  223. end
  224. end
  225. def test_daylight_savings_time_crossings_forward_start_1day
  226. with_env_tz 'US/Eastern' do
  227. # st: US: 2005 April 2nd 7:27pm
  228. assert_equal Time.local(2005,4,3,19,27,0), Time.local(2005,4,2,19,27,0).since(1.day), 'st+1.day=>dt'
  229. assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(1.day), 'dt+1.day=>dt'
  230. end
  231. with_env_tz 'NZ' do
  232. # st: New Zealand: 2006 September 30th 7:27pm
  233. assert_equal Time.local(2006,10,1,19,27,0), Time.local(2006,9,30,19,27,0).since(1.day), 'st+1.day=>dt'
  234. assert_equal Time.local(2006,10,2,19,27,0), Time.local(2006,10,1,19,27,0).since(1.day), 'dt+1.day=>dt'
  235. end
  236. end
  237. def test_daylight_savings_time_crossings_forward_start_tomorrow
  238. with_env_tz 'US/Eastern' do
  239. # st: US: 2005 April 2nd 7:27pm
  240. assert_equal Time.local(2005,4,3,19,27,0), Time.local(2005,4,2,19,27,0).tomorrow, 'st+1.day=>dt'
  241. assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).tomorrow, 'dt+1.day=>dt'
  242. end
  243. with_env_tz 'NZ' do
  244. # st: New Zealand: 2006 September 30th 7:27pm
  245. assert_equal Time.local(2006,10,1,19,27,0), Time.local(2006,9,30,19,27,0).tomorrow, 'st+1.day=>dt'
  246. assert_equal Time.local(2006,10,2,19,27,0), Time.local(2006,10,1,19,27,0).tomorrow, 'dt+1.day=>dt'
  247. end
  248. end
  249. def test_daylight_savings_time_crossings_backward_start_yesterday
  250. with_env_tz 'US/Eastern' do
  251. # st: US: 2005 April 2nd 7:27pm
  252. assert_equal Time.local(2005,4,2,19,27,0), Time.local(2005,4,3,19,27,0).yesterday, 'dt-1.day=>st'
  253. assert_equal Time.local(2005,4,3,19,27,0), Time.local(2005,4,4,19,27,0).yesterday, 'dt-1.day=>dt'
  254. end
  255. with_env_tz 'NZ' do
  256. # st: New Zealand: 2006 September 30th 7:27pm
  257. assert_equal Time.local(2006,9,30,19,27,0), Time.local(2006,10,1,19,27,0).yesterday, 'dt-1.day=>st'
  258. assert_equal Time.local(2006,10,1,19,27,0), Time.local(2006,10,2,19,27,0).yesterday, 'dt-1.day=>dt'
  259. end
  260. end
  261. def test_daylight_savings_time_crossings_forward_end
  262. with_env_tz 'US/Eastern' do
  263. # dt: US: 2005 October 30th 12:45am
  264. assert_equal Time.local(2005,10,31,0,45,0), Time.local(2005,10,30,0,45,0).since(86400), 'dt+1.day=>st'
  265. assert_equal Time.local(2005,11, 1,0,45,0), Time.local(2005,10,31,0,45,0).since(86400), 'st+1.day=>st'
  266. end
  267. with_env_tz 'NZ' do
  268. # dt: New Zealand: 2006 March 19th 1:45am
  269. assert_equal Time.local(2006,3,20,1,45,0), Time.local(2006,3,19,1,45,0).since(86400), 'dt+1.day=>st'
  270. assert_equal Time.local(2006,3,21,1,45,0), Time.local(2006,3,20,1,45,0).since(86400), 'st+1.day=>st'
  271. end
  272. end
  273. def test_daylight_savings_time_crossings_forward_end_1day
  274. with_env_tz 'US/Eastern' do
  275. # dt: US: 2005 October 30th 12:45am
  276. assert_equal Time.local(2005,10,31,0,45,0), Time.local(2005,10,30,0,45,0).since(1.day), 'dt+1.day=>st'
  277. assert_equal Time.local(2005,11, 1,0,45,0), Time.local(2005,10,31,0,45,0).since(1.day), 'st+1.day=>st'
  278. end
  279. with_env_tz 'NZ' do
  280. # dt: New Zealand: 2006 March 19th 1:45am
  281. assert_equal Time.local(2006,3,20,1,45,0), Time.local(2006,3,19,1,45,0).since(1.day), 'dt+1.day=>st'
  282. assert_equal Time.local(2006,3,21,1,45,0), Time.local(2006,3,20,1,45,0).since(1.day), 'st+1.day=>st'
  283. end
  284. end
  285. def test_daylight_savings_time_crossings_forward_end_tomorrow
  286. with_env_tz 'US/Eastern' do
  287. # dt: US: 2005 October 30th 12:45am
  288. assert_equal Time.local(2005,10,31,0,45,0), Time.local(2005,10,30,0,45,0).tomorrow, 'dt+1.day=>st'
  289. assert_equal Time.local(2005,11, 1,0,45,0), Time.local(2005,10,31,0,45,0).tomorrow, 'st+1.day=>st'
  290. end
  291. with_env_tz 'NZ' do
  292. # dt: New Zealand: 2006 March 19th 1:45am
  293. assert_equal Time.local(2006,3,20,1,45,0), Time.local(2006,3,19,1,45,0).tomorrow, 'dt+1.day=>st'
  294. assert_equal Time.local(2006,3,21,1,45,0), Time.local(2006,3,20,1,45,0).tomorrow, 'st+1.day=>st'
  295. end
  296. end
  297. def test_daylight_savings_time_crossings_backward_end_yesterday
  298. with_env_tz 'US/Eastern' do
  299. # dt: US: 2005 October 30th 12:45am
  300. assert_equal Time.local(2005,10,30,0,45,0), Time.local(2005,10,31,0,45,0).yesterday, 'st-1.day=>dt'
  301. assert_equal Time.local(2005,10, 31,0,45,0), Time.local(2005,11,1,0,45,0).yesterday, 'st-1.day=>st'
  302. end
  303. with_env_tz 'NZ' do
  304. # dt: New Zealand: 2006 March 19th 1:45am
  305. assert_equal Time.local(2006,3,19,1,45,0), Time.local(2006,3,20,1,45,0).yesterday, 'st-1.day=>dt'
  306. assert_equal Time.local(2006,3,20,1,45,0), Time.local(2006,3,21,1,45,0).yesterday, 'st-1.day=>st'
  307. end
  308. end
  309. def test_yesterday
  310. assert_equal Time.local(2005,2,21,10,10,10), Time.local(2005,2,22,10,10,10).yesterday
  311. assert_equal Time.local(2005,2,28,10,10,10), Time.local(2005,3,2,10,10,10).yesterday.yesterday
  312. end
  313. def test_tomorrow
  314. assert_equal Time.local(2005,2,23,10,10,10), Time.local(2005,2,22,10,10,10).tomorrow
  315. assert_equal Time.local(2005,3,2,10,10,10), Time.local(2005,2,28,10,10,10).tomorrow.tomorrow
  316. end
  317. def test_change
  318. assert_equal Time.local(2006,2,22,15,15,10), Time.local(2005,2,22,15,15,10).change(:year => 2006)
  319. assert_equal Time.local(2005,6,22,15,15,10), Time.local(2005,2,22,15,15,10).change(:month => 6)
  320. assert_equal Time.local(2012,9,22,15,15,10), Time.local(2005,2,22,15,15,10).change(:year => 2012, :month => 9)
  321. assert_equal Time.local(2005,2,22,16), Time.local(2005,2,22,15,15,10).change(:hour => 16)
  322. assert_equal Time.local(2005,2,22,16,45), Time.local(2005,2,22,15,15,10).change(:hour => 16, :min => 45)
  323. assert_equal Time.local(2005,2,22,15,45), Time.local(2005,2,22,15,15,10).change(:min => 45)
  324. assert_equal Time.local(2005,1,2, 5, 0, 0, 0), Time.local(2005,1,2,11,22,33,44).change(:hour => 5)
  325. assert_equal Time.local(2005,1,2,11, 6, 0, 0), Time.local(2005,1,2,11,22,33,44).change(:min => 6)
  326. assert_equal Time.local(2005,1,2,11,22, 7, 0), Time.local(2005,1,2,11,22,33,44).change(:sec => 7)
  327. assert_equal Time.local(2005,1,2,11,22,33, 8), Time.local(2005,1,2,11,22,33,44).change(:usec => 8)
  328. end
  329. def test_utc_change
  330. assert_equal Time.utc(2006,2,22,15,15,10), Time.utc(2005,2,22,15,15,10).change(:year => 2006)
  331. assert_equal Time.utc(2005,6,22,15,15,10), Time.utc(2005,2,22,15,15,10).change(:month => 6)
  332. assert_equal Time.utc(2012,9,22,15,15,10), Time.utc(2005,2,22,15,15,10).change(:year => 2012, :month => 9)
  333. assert_equal Time.utc(2005,2,22,16), Time.utc(2005,2,22,15,15,10).change(:hour => 16)
  334. assert_equal Time.utc(2005,2,22,16,45), Time.utc(2005,2,22,15,15,10).change(:hour => 16, :min => 45)
  335. assert_equal Time.utc(2005,2,22,15,45), Time.utc(2005,2,22,15,15,10).change(:min => 45)
  336. end
  337. def test_advance
  338. assert_equal Time.local(2006,2,28,15,15,10), Time.local(2005,2,28,15,15,10).advance(:years => 1)
  339. assert_equal Time.local(2005,6,28,15,15,10), Time.local(2005,2,28,15,15,10).advance(:months => 4)
  340. assert_equal Time.local(2005,3,21,15,15,10), Time.local(2005,2,28,15,15,10).advance(:weeks => 3)
  341. assert_equal Time.local(2005,3,5,15,15,10), Time.local(2005,2,28,15,15,10).advance(:days => 5)
  342. assert_equal Time.local(2012,9,28,15,15,10), Time.local(2005,2,28,15,15,10).advance(:years => 7, :months => 7)
  343. assert_equal Time.local(2013,10,3,15,15,10), Time.local(2005,2,28,15,15,10).advance(:years => 7, :months => 19, :days => 5)
  344. assert_equal Time.local(2013,10,17,15,15,10), Time.local(2005,2,28,15,15,10).advance(:years => 7, :months => 19, :weeks => 2, :days => 5)
  345. assert_equal Time.local(2001,12,27,15,15,10), Time.local(2005,2,28,15,15,10).advance(:years => -3, :months => -2, :days => -1)
  346. assert_equal Time.local(2005,2,28,15,15,10), Time.local(2004,2,29,15,15,10).advance(:years => 1) #leap day plus one year
  347. assert_equal Time.local(2005,2,28,20,15,10), Time.local(2005,2,28,15,15,10).advance(:hours => 5)
  348. assert_equal Time.local(2005,2,28,15,22,10), Time.local(2005,2,28,15,15,10).advance(:minutes => 7)
  349. assert_equal Time.local(2005,2,28,15,15,19), Time.local(2005,2,28,15,15,10).advance(:seconds => 9)
  350. assert_equal Time.local(2005,2,28,20,22,19), Time.local(2005,2,28,15,15,10).advance(:hours => 5, :minutes => 7, :seconds => 9)
  351. assert_equal Time.local(2005,2,28,10,8,1), Time.local(2005,2,28,15,15,10).advance(:hours => -5, :minutes => -7, :seconds => -9)
  352. assert_equal Time.local(2013,10,17,20,22,19), Time.local(2005,2,28,15,15,10).advance(:years => 7, :months => 19, :weeks => 2, :days => 5, :hours => 5, :minutes => 7, :seconds => 9)
  353. end
  354. def test_utc_advance
  355. assert_equal Time.utc(2006,2,22,15,15,10), Time.utc(2005,2,22,15,15,10).advance(:years => 1)
  356. assert_equal Time.utc(2005,6,22,15,15,10), Time.utc(2005,2,22,15,15,10).advance(:months => 4)
  357. assert_equal Time.utc(2005,3,21,15,15,10), Time.utc(2005,2,28,15,15,10).advance(:weeks => 3)
  358. assert_equal Time.utc(2005,3,5,15,15,10), Time.utc(2005,2,28,15,15,10).advance(:days => 5)
  359. assert_equal Time.utc(2012,9,22,15,15,10), Time.utc(2005,2,22,15,15,10).advance(:years => 7, :months => 7)
  360. assert_equal Time.utc(2013,10,3,15,15,10), Time.utc(2005,2,22,15,15,10).advance(:years => 7, :months => 19, :days => 11)
  361. assert_equal Time.utc(2013,10,17,15,15,10), Time.utc(2005,2,28,15,15,10).advance(:years => 7, :months => 19, :weeks => 2, :days => 5)
  362. assert_equal Time.utc(2001,12,27,15,15,10), Time.utc(2005,2,28,15,15,10).advance(:years => -3, :months => -2, :days => -1)
  363. assert_equal Time.utc(2005,2,28,15,15,10), Time.utc(2004,2,29,15,15,10).advance(:years => 1) #leap day plus one year
  364. assert_equal Time.utc(2005,2,28,20,15,10), Time.utc(2005,2,28,15,15,10).advance(:hours => 5)
  365. assert_equal Time.utc(2005,2,28,15,22,10), Time.utc(2005,2,28,15,15,10).advance(:minutes => 7)
  366. assert_equal Time.utc(2005,2,28,15,15,19), Time.utc(2005,2,28,15,15,10).advance(:seconds => 9)
  367. assert_equal Time.utc(2005,2,28,20,22,19), Time.utc(2005,2,28,15,15,10).advance(:hours => 5, :minutes => 7, :seconds => 9)
  368. assert_equal Time.utc(2005,2,28,10,8,1), Time.utc(2005,2,28,15,15,10).advance(:hours => -5, :minutes => -7, :seconds => -9)
  369. assert_equal Time.utc(2013,10,17,20,22,19), Time.utc(2005,2,28,15,15,10).advance(:years => 7, :months => 19, :weeks => 2, :days => 5, :hours => 5, :minutes => 7, :seconds => 9)
  370. end
  371. def test_next_week
  372. with_env_tz 'US/Eastern' do
  373. assert_equal Time.local(2005,2,28), Time.local(2005,2,22,15,15,10).next_week
  374. assert_equal Time.local(2005,3,1), Time.local(2005,2,22,15,15,10).next_week(:tuesday)
  375. assert_equal Time.local(2005,3,4), Time.local(2005,2,22,15,15,10).next_week(:friday)
  376. assert_equal Time.local(2006,10,30), Time.local(2006,10,23,0,0,0).next_week
  377. assert_equal Time.local(2006,11,1), Time.local(2006,10,23,0,0,0).next_week(:wednesday)
  378. end
  379. end
  380. def test_next_week_near_daylight_start
  381. with_env_tz 'US/Eastern' do
  382. assert_equal Time.local(2006,4,3), Time.local(2006,4,2,23,1,0).next_week, 'just crossed standard => daylight'
  383. end
  384. with_env_tz 'NZ' do
  385. assert_equal Time.local(2006,10,2), Time.local(2006,10,1,23,1,0).next_week, 'just crossed standard => daylight'
  386. end
  387. end
  388. def test_next_week_near_daylight_end
  389. with_env_tz 'US/Eastern' do
  390. assert_equal Time.local(2006,10,30), Time.local(2006,10,29,23,1,0).next_week, 'just crossed daylight => standard'
  391. end
  392. with_env_tz 'NZ' do
  393. assert_equal Time.local(2006,3,20), Time.local(2006,3,19,23,1,0).next_week, 'just crossed daylight => standard'
  394. end
  395. end
  396. def test_to_s
  397. time = Time.utc(2005, 2, 21, 17, 44, 30)
  398. assert_equal time.to_default_s, time.to_s
  399. assert_equal time.to_default_s, time.to_s(:doesnt_exist)
  400. assert_equal "2005-02-21 17:44:30", time.to_s(:db)
  401. assert_equal "21 Feb 17:44", time.to_s(:short)
  402. assert_equal "17:44", time.to_s(:time)
  403. assert_equal "February 21, 2005 17:44", time.to_s(:long)
  404. assert_equal "February 21st, 2005 17:44", time.to_s(:long_ordinal)
  405. with_env_tz "UTC" do
  406. assert_equal "Mon, 21 Feb 2005 17:44:30 +0000", time.to_s(:rfc822)
  407. end
  408. end
  409. def test_custom_date_format
  410. Time::DATE_FORMATS[:custom] = '%Y%m%d%H%M%S'
  411. assert_equal '20050221143000', Time.local(2005, 2, 21, 14, 30, 0).to_s(:custom)
  412. Time::DATE_FORMATS.delete(:custom)
  413. end
  414. def test_to_date
  415. assert_equal Date.new(2005, 2, 21), Time.local(2005, 2, 21, 17, 44, 30).to_date
  416. end
  417. def test_to_datetime
  418. assert_equal Time.utc(2005, 2, 21, 17, 44, 30).to_datetime, DateTime.civil(2005, 2, 21, 17, 44, 30, 0, 0)
  419. with_env_tz 'US/Eastern' do
  420. assert_equal Time.local(2005, 2, 21, 17, 44, 30).to_datetime, DateTime.civil(2005, 2, 21, 17, 44, 30, Rational(Time.local(2005, 2, 21, 17, 44, 30).utc_offset, 86400), 0)
  421. end
  422. with_env_tz 'NZ' do
  423. assert_equal Time.local(2005, 2, 21, 17, 44, 30).to_datetime, DateTime.civil(2005, 2, 21, 17, 44, 30, Rational(Time.local(2005, 2, 21, 17, 44, 30).utc_offset, 86400), 0)
  424. end
  425. assert_equal ::Date::ITALY, Time.utc(2005, 2, 21, 17, 44, 30).to_datetime.start # use Ruby's default start value
  426. end
  427. def test_to_time
  428. assert_equal Time.local(2005, 2, 21, 17, 44, 30), Time.local(2005, 2, 21, 17, 44, 30).to_time
  429. end
  430. # NOTE: this test seems to fail (changeset 1958) only on certain platforms,
  431. # like OSX, and FreeBSD 5.4.
  432. def test_fp_inaccuracy_ticket_1836
  433. midnight = Time.local(2005, 2, 21, 0, 0, 0)
  434. assert_equal midnight.midnight, (midnight + 1.hour + 0.000001).midnight
  435. end
  436. def test_days_in_month_with_year
  437. assert_equal 31, Time.days_in_month(1, 2005)
  438. assert_equal 28, Time.days_in_month(2, 2005)
  439. assert_equal 29, Time.days_in_month(2, 2004)
  440. assert_equal 29, Time.days_in_month(2, 2000)
  441. assert_equal 28, Time.days_in_month(2, 1900)
  442. assert_equal 31, Time.days_in_month(3, 2005)
  443. assert_equal 30, Time.days_in_month(4, 2005)
  444. assert_equal 31, Time.days_in_month(5, 2005)
  445. assert_equal 30, Time.days_in_month(6, 2005)
  446. assert_equal 31, Time.days_in_month(7, 2005)
  447. assert_equal 31, Time.days_in_month(8, 2005)
  448. assert_equal 30, Time.days_in_month(9, 2005)
  449. assert_equal 31, Time.days_in_month(10, 2005)
  450. assert_equal 30, Time.days_in_month(11, 2005)
  451. assert_equal 31, Time.days_in_month(12, 2005)
  452. end
  453. uses_mocha 'TestTimeDaysInMonthWithoutYearArg' do
  454. def test_days_in_month_feb_in_common_year_without_year_arg
  455. Time.stubs(:now).returns(Time.utc(2007))
  456. assert_equal 28, Time.days_in_month(2)
  457. end
  458. def test_days_in_month_feb_in_leap_year_without_year_arg
  459. Time.stubs(:now).returns(Time.utc(2008))
  460. assert_equal 29, Time.days_in_month(2)
  461. end
  462. end
  463. def test_time_with_datetime_fallback
  464. assert_equal Time.time_with_datetime_fallback(:utc, 2005, 2, 21, 17, 44, 30), Time.utc(2005, 2, 21, 17, 44, 30)
  465. assert_equal Time.time_with_datetime_fallback(:local, 2005, 2, 21, 17, 44, 30), Time.local(2005, 2, 21, 17, 44, 30)
  466. assert_equal Time.time_with_datetime_fallback(:utc, 2039, 2, 21, 17, 44, 30), DateTime.civil(2039, 2, 21, 17, 44, 30, 0, 0)
  467. assert_equal Time.time_with_datetime_fallback(:local, 2039, 2, 21, 17, 44, 30), DateTime.civil(2039, 2, 21, 17, 44, 30, DateTime.local_offset, 0)
  468. assert_equal Time.time_with_datetime_fallback(:utc, 1900, 2, 21, 17, 44, 30), DateTime.civil(1900, 2, 21, 17, 44, 30, 0, 0)
  469. assert_equal Time.time_with_datetime_fallback(:utc, 2005), Time.utc(2005)
  470. assert_equal Time.time_with_datetime_fallback(:utc, 2039), DateTime.civil(2039, 1, 1, 0, 0, 0, 0, 0)
  471. assert_equal Time.time_with_datetime_fallback(:utc, 2005, 2, 21, 17, 44, 30, 1), Time.utc(2005, 2, 21, 17, 44, 30, 1) #with usec
  472. # This won't overflow on 64bit linux
  473. unless time_is_64bits?
  474. assert_equal Time.time_with_datetime_fallback(:local, 1900, 2, 21, 17, 44, 30), DateTime.civil(1900, 2, 21, 17, 44, 30, DateTime.local_offset, 0)
  475. assert_equal Time.time_with_datetime_fallback(:utc, 2039, 2, 21, 17, 44, 30, 1),
  476. DateTime.civil(2039, 2, 21, 17, 44, 30, 0, 0)
  477. assert_equal ::Date::ITALY, Time.time_with_datetime_fallback(:utc, 2039, 2, 21, 17, 44, 30, 1).start # use Ruby's default start value
  478. end
  479. end
  480. def test_utc_time
  481. assert_equal Time.utc_time(2005, 2, 21, 17, 44, 30), Time.utc(2005, 2, 21, 17, 44, 30)
  482. assert_equal Time.utc_time(2039, 2, 21, 17, 44, 30), DateTime.civil(2039, 2, 21, 17, 44, 30, 0, 0)
  483. assert_equal Time.utc_time(1901, 2, 21, 17, 44, 30), DateTime.civil(1901, 2, 21, 17, 44, 30, 0, 0)
  484. end
  485. def test_local_time
  486. assert_equal Time.local_time(2005, 2, 21, 17, 44, 30), Time.local(2005, 2, 21, 17, 44, 30)
  487. assert_equal Time.local_time(2039, 2, 21, 17, 44, 30), DateTime.civil(2039, 2, 21, 17, 44, 30, DateTime.local_offset, 0)
  488. unless time_is_64bits?
  489. assert_equal Time.local_time(1901, 2, 21, 17, 44, 30), DateTime.civil(1901, 2, 21, 17, 44, 30, DateTime.local_offset, 0)
  490. end
  491. end
  492. def test_next_month_on_31st
  493. assert_equal Time.local(2005, 9, 30), Time.local(2005, 8, 31).next_month
  494. end
  495. def test_last_month_on_31st
  496. assert_equal Time.local(2004, 2, 29), Time.local(2004, 3, 31).last_month
  497. end
  498. def test_xmlschema_is_available
  499. assert_nothing_raised { Time.now.xmlschema }
  500. end
  501. def test_acts_like_time
  502. assert Time.new.acts_like_time?
  503. end
  504. def test_formatted_offset_with_utc
  505. assert_equal '+00:00', Time.utc(2000).formatted_offset
  506. assert_equal '+0000', Time.utc(2000).formatted_offset(false)
  507. assert_equal 'UTC', Time.utc(2000).formatted_offset(true, 'UTC')
  508. end
  509. def test_formatted_offset_with_local
  510. with_env_tz 'US/Eastern' do
  511. assert_equal '-05:00', Time.local(2000).formatted_offset
  512. assert_equal '-0500', Time.local(2000).formatted_offset(false)
  513. assert_equal '-04:00', Time.local(2000, 7).formatted_offset
  514. assert_equal '-0400', Time.local(2000, 7).formatted_offset(false)
  515. end
  516. end
  517. def test_compare_with_time
  518. assert_equal 1, Time.utc(2000) <=> Time.utc(1999, 12, 31, 23, 59, 59, 999)
  519. assert_equal 0, Time.utc(2000) <=> Time.utc(2000, 1, 1, 0, 0, 0)
  520. assert_equal(-1, Time.utc(2000) <=> Time.utc(2000, 1, 1, 0, 0, 0, 001))
  521. end
  522. def test_compare_with_datetime
  523. assert_equal 1, Time.utc(2000) <=> DateTime.civil(1999, 12, 31, 23, 59, 59)
  524. assert_equal 0, Time.utc(2000) <=> DateTime.civil(2000, 1, 1, 0, 0, 0)
  525. assert_equal(-1, Time.utc(2000) <=> DateTime.civil(2000, 1, 1, 0, 0, 1))
  526. end
  527. def test_compare_with_time_with_zone
  528. assert_equal 1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59), ActiveSupport::TimeZone['UTC'] )
  529. assert_equal 0, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0), ActiveSupport::TimeZone['UTC'] )
  530. assert_equal(-1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), ActiveSupport::TimeZone['UTC'] ))
  531. end
  532. def test_minus_with_time_with_zone
  533. assert_equal 86_400.0, Time.utc(2000, 1, 2) - ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), ActiveSupport::TimeZone['UTC'] )
  534. end
  535. def test_time_created_with_local_constructor_cannot_represent_times_during_hour_skipped_by_dst
  536. with_env_tz 'US/Eastern' do
  537. # On Apr 2 2006 at 2:00AM in US, clocks were moved forward to 3:00AM.
  538. # Therefore, 2AM EST doesn't exist for this date; Time.local fails over to 3:00AM EDT
  539. assert_equal Time.local(2006, 4, 2, 3), Time.local(2006, 4, 2, 2)
  540. assert Time.local(2006, 4, 2, 2).dst?
  541. end
  542. end
  543. def test_case_equality
  544. assert Time === Time.utc(2000)
  545. assert Time === ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone['UTC'])
  546. assert_equal false, Time === DateTime.civil(2000)
  547. end
  548. protected
  549. def with_env_tz(new_tz = 'US/Eastern')
  550. old_tz, ENV['TZ'] = ENV['TZ'], new_tz
  551. yield
  552. ensure
  553. old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
  554. end
  555. def time_is_64bits?
  556. Time.time_with_datetime_fallback(:utc, 2039, 2, 21, 17, 44, 30, 1).is_a?(Time)
  557. end
  558. end
  559. class TimeExtMarshalingTest < Test::Unit::TestCase
  560. def test_marshaling_with_utc_instance
  561. t = Time.utc(2000)
  562. marshaled = Marshal.dump t
  563. unmarshaled = Marshal.load marshaled
  564. assert_equal t, unmarshaled
  565. assert_equal t.zone, unmarshaled.zone
  566. end
  567. def test_marshaling_with_local_instance
  568. t = Time.local(2000)
  569. marshaled = Marshal.dump t
  570. unmarshaled = Marshal.load marshaled
  571. assert_equal t, unmarshaled
  572. assert_equal t.zone, unmarshaled.zone
  573. end
  574. def test_marshaling_with_frozen_utc_instance
  575. t = Time.utc(2000).freeze
  576. marshaled = Marshal.dump t
  577. unmarshaled = Marshal.load marshaled
  578. assert_equal t, unmarshaled
  579. assert_equal t.zone, unmarshaled.zone
  580. end
  581. def test_marshaling_with_frozen_local_instance
  582. t = Time.local(2000).freeze
  583. marshaled = Marshal.dump t
  584. unmarshaled = Marshal.load marshaled
  585. assert_equal t, unmarshaled
  586. assert_equal t.zone, unmarshaled.zone
  587. end
  588. end