PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/activesupport/test/core_ext/time_ext_test.rb

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