PageRenderTime 212ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/activesupport/test/core_ext/date_time_ext_test.rb

https://github.com/ghar/rails
Ruby | 394 lines | 334 code | 57 blank | 3 comment | 0 complexity | c11e558b3f30b9dcbe01a666e03272e9 MD5 | raw file
  1. require 'abstract_unit'
  2. require 'active_support/time'
  3. class DateTimeExtCalculationsTest < Test::Unit::TestCase
  4. def test_to_s
  5. datetime = DateTime.new(2005, 2, 21, 14, 30, 0, 0)
  6. assert_equal "2005-02-21 14:30:00", datetime.to_s(:db)
  7. assert_equal "14:30", datetime.to_s(:time)
  8. assert_equal "21 Feb 14:30", datetime.to_s(:short)
  9. assert_equal "February 21, 2005 14:30", datetime.to_s(:long)
  10. assert_equal "Mon, 21 Feb 2005 14:30:00 +0000", datetime.to_s(:rfc822)
  11. assert_equal "February 21st, 2005 14:30", datetime.to_s(:long_ordinal)
  12. assert_match(/^2005-02-21T14:30:00(Z|\+00:00)$/, datetime.to_s)
  13. end
  14. def test_readable_inspect
  15. datetime = DateTime.new(2005, 2, 21, 14, 30, 0)
  16. assert_equal "Mon, 21 Feb 2005 14:30:00 +0000", datetime.readable_inspect
  17. assert_equal datetime.readable_inspect, datetime.inspect
  18. end
  19. def test_custom_date_format
  20. Time::DATE_FORMATS[:custom] = '%Y%m%d%H%M%S'
  21. assert_equal '20050221143000', DateTime.new(2005, 2, 21, 14, 30, 0).to_s(:custom)
  22. Time::DATE_FORMATS.delete(:custom)
  23. end
  24. def test_to_date
  25. assert_equal Date.new(2005, 2, 21), DateTime.new(2005, 2, 21, 14, 30, 0).to_date
  26. end
  27. def test_to_datetime
  28. assert_equal DateTime.new(2005, 2, 21, 14, 30, 0), DateTime.new(2005, 2, 21, 14, 30, 0).to_datetime
  29. end
  30. def test_to_time
  31. assert_equal Time.utc(2005, 2, 21, 10, 11, 12), DateTime.new(2005, 2, 21, 10, 11, 12, 0, 0).to_time
  32. assert_equal Time.utc_time(2039, 2, 21, 10, 11, 12), DateTime.new(2039, 2, 21, 10, 11, 12, 0, 0).to_time
  33. # DateTimes with offsets other than 0 are returned unaltered
  34. assert_equal DateTime.new(2005, 2, 21, 10, 11, 12, Rational(-5, 24)), DateTime.new(2005, 2, 21, 10, 11, 12, Rational(-5, 24)).to_time
  35. # Fractional seconds are preserved
  36. assert_equal Time.utc(2005, 2, 21, 10, 11, 12, 256), DateTime.new(2005, 2, 21, 10, 11, 12 + Rational(256, 1000000), 0).to_time
  37. end
  38. def test_civil_from_format
  39. assert_equal DateTime.civil(2010, 5, 4, 0, 0, 0, DateTime.local_offset), DateTime.civil_from_format(:local, 2010, 5, 4)
  40. assert_equal DateTime.civil(2010, 5, 4, 0, 0, 0, 0), DateTime.civil_from_format(:utc, 2010, 5, 4)
  41. end
  42. def test_seconds_since_midnight
  43. assert_equal 1,DateTime.civil(2005,1,1,0,0,1).seconds_since_midnight
  44. assert_equal 60,DateTime.civil(2005,1,1,0,1,0).seconds_since_midnight
  45. assert_equal 3660,DateTime.civil(2005,1,1,1,1,0).seconds_since_midnight
  46. assert_equal 86399,DateTime.civil(2005,1,1,23,59,59).seconds_since_midnight
  47. end
  48. def test_beginning_of_week
  49. assert_equal DateTime.civil(2005,1,31), DateTime.civil(2005,2,4,10,10,10).beginning_of_week
  50. assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,11,28,0,0,0).beginning_of_week #monday
  51. assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,11,29,0,0,0).beginning_of_week #tuesday
  52. assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,11,30,0,0,0).beginning_of_week #wednesday
  53. assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,01,0,0,0).beginning_of_week #thursday
  54. assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,02,0,0,0).beginning_of_week #friday
  55. assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,03,0,0,0).beginning_of_week #saturday
  56. assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,04,0,0,0).beginning_of_week #sunday
  57. end
  58. def test_beginning_of_day
  59. assert_equal DateTime.civil(2005,2,4,0,0,0), DateTime.civil(2005,2,4,10,10,10).beginning_of_day
  60. end
  61. def test_end_of_day
  62. assert_equal DateTime.civil(2005,2,4,23,59,59), DateTime.civil(2005,2,4,10,10,10).end_of_day
  63. end
  64. def test_beginning_of_month
  65. assert_equal DateTime.civil(2005,2,1,0,0,0), DateTime.civil(2005,2,22,10,10,10).beginning_of_month
  66. end
  67. def test_beginning_of_quarter
  68. assert_equal DateTime.civil(2005,1,1,0,0,0), DateTime.civil(2005,2,15,10,10,10).beginning_of_quarter
  69. assert_equal DateTime.civil(2005,1,1,0,0,0), DateTime.civil(2005,1,1,0,0,0).beginning_of_quarter
  70. assert_equal DateTime.civil(2005,10,1,0,0,0), DateTime.civil(2005,12,31,10,10,10).beginning_of_quarter
  71. assert_equal DateTime.civil(2005,4,1,0,0,0), DateTime.civil(2005,6,30,23,59,59).beginning_of_quarter
  72. end
  73. def test_end_of_month
  74. assert_equal DateTime.civil(2005,3,31,23,59,59), DateTime.civil(2005,3,20,10,10,10).end_of_month
  75. assert_equal DateTime.civil(2005,2,28,23,59,59), DateTime.civil(2005,2,20,10,10,10).end_of_month
  76. assert_equal DateTime.civil(2005,4,30,23,59,59), DateTime.civil(2005,4,20,10,10,10).end_of_month
  77. end
  78. def test_beginning_of_year
  79. assert_equal DateTime.civil(2005,1,1,0,0,0), DateTime.civil(2005,2,22,10,10,10).beginning_of_year
  80. end
  81. def test_weeks_ago
  82. assert_equal DateTime.civil(2005,5,29,10), DateTime.civil(2005,6,5,10,0,0).weeks_ago(1)
  83. assert_equal DateTime.civil(2005,5,1,10), DateTime.civil(2005,6,5,10,0,0).weeks_ago(5)
  84. assert_equal DateTime.civil(2005,4,24,10), DateTime.civil(2005,6,5,10,0,0).weeks_ago(6)
  85. assert_equal DateTime.civil(2005,2,27,10), DateTime.civil(2005,6,5,10,0,0).weeks_ago(14)
  86. assert_equal DateTime.civil(2004,12,25,10), DateTime.civil(2005,1,1,10,0,0).weeks_ago(1)
  87. end
  88. def test_months_ago
  89. assert_equal DateTime.civil(2005,5,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(1)
  90. assert_equal DateTime.civil(2004,11,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(7)
  91. assert_equal DateTime.civil(2004,12,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(6)
  92. assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(12)
  93. assert_equal DateTime.civil(2003,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(24)
  94. end
  95. def test_months_since
  96. assert_equal DateTime.civil(2005,7,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(1)
  97. assert_equal DateTime.civil(2006,1,5,10), DateTime.civil(2005,12,5,10,0,0).months_since(1)
  98. assert_equal DateTime.civil(2005,12,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(6)
  99. assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,12,5,10,0,0).months_since(6)
  100. assert_equal DateTime.civil(2006,1,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(7)
  101. assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(12)
  102. assert_equal DateTime.civil(2007,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(24)
  103. assert_equal DateTime.civil(2005,4,30,10), DateTime.civil(2005,3,31,10,0,0).months_since(1)
  104. assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2005,1,29,10,0,0).months_since(1)
  105. assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2005,1,30,10,0,0).months_since(1)
  106. assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2005,1,31,10,0,0).months_since(1)
  107. end
  108. def test_years_ago
  109. assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_ago(1)
  110. assert_equal DateTime.civil(1998,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_ago(7)
  111. assert_equal DateTime.civil(2003,2,28,10), DateTime.civil(2004,2,29,10,0,0).years_ago(1) # 1 year ago from leap day
  112. end
  113. def test_years_since
  114. assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_since(1)
  115. assert_equal DateTime.civil(2012,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_since(7)
  116. assert_equal DateTime.civil(2182,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_since(177)
  117. assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2004,2,29,10,0,0).years_since(1) # 1 year since leap day
  118. end
  119. def test_prev_year
  120. assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).prev_year
  121. end
  122. def test_next_year
  123. assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,6,5,10,0,0).next_year
  124. end
  125. def test_ago
  126. assert_equal DateTime.civil(2005,2,22,10,10,9), DateTime.civil(2005,2,22,10,10,10).ago(1)
  127. assert_equal DateTime.civil(2005,2,22,9,10,10), DateTime.civil(2005,2,22,10,10,10).ago(3600)
  128. assert_equal DateTime.civil(2005,2,20,10,10,10), DateTime.civil(2005,2,22,10,10,10).ago(86400*2)
  129. assert_equal DateTime.civil(2005,2,20,9,9,45), DateTime.civil(2005,2,22,10,10,10).ago(86400*2 + 3600 + 25)
  130. end
  131. def test_since
  132. assert_equal DateTime.civil(2005,2,22,10,10,11), DateTime.civil(2005,2,22,10,10,10).since(1)
  133. assert_equal DateTime.civil(2005,2,22,11,10,10), DateTime.civil(2005,2,22,10,10,10).since(3600)
  134. assert_equal DateTime.civil(2005,2,24,10,10,10), DateTime.civil(2005,2,22,10,10,10).since(86400*2)
  135. assert_equal DateTime.civil(2005,2,24,11,10,35), DateTime.civil(2005,2,22,10,10,10).since(86400*2 + 3600 + 25)
  136. assert_equal DateTime.civil(2005,2,22,10,10,11), DateTime.civil(2005,2,22,10,10,10).since(1.333)
  137. assert_equal DateTime.civil(2005,2,22,10,10,12), DateTime.civil(2005,2,22,10,10,10).since(1.667)
  138. end
  139. def test_yesterday
  140. assert_equal DateTime.civil(2005,2,21,10,10,10), DateTime.civil(2005,2,22,10,10,10).yesterday
  141. assert_equal DateTime.civil(2005,2,28,10,10,10), DateTime.civil(2005,3,2,10,10,10).yesterday.yesterday
  142. end
  143. def test_tomorrow
  144. assert_equal DateTime.civil(2005,2,23,10,10,10), DateTime.civil(2005,2,22,10,10,10).tomorrow
  145. assert_equal DateTime.civil(2005,3,2,10,10,10), DateTime.civil(2005,2,28,10,10,10).tomorrow.tomorrow
  146. end
  147. def test_change
  148. assert_equal DateTime.civil(2006,2,22,15,15,10), DateTime.civil(2005,2,22,15,15,10).change(:year => 2006)
  149. assert_equal DateTime.civil(2005,6,22,15,15,10), DateTime.civil(2005,2,22,15,15,10).change(:month => 6)
  150. assert_equal DateTime.civil(2012,9,22,15,15,10), DateTime.civil(2005,2,22,15,15,10).change(:year => 2012, :month => 9)
  151. assert_equal DateTime.civil(2005,2,22,16), DateTime.civil(2005,2,22,15,15,10).change(:hour => 16)
  152. assert_equal DateTime.civil(2005,2,22,16,45), DateTime.civil(2005,2,22,15,15,10).change(:hour => 16, :min => 45)
  153. assert_equal DateTime.civil(2005,2,22,15,45), DateTime.civil(2005,2,22,15,15,10).change(:min => 45)
  154. end
  155. def test_advance
  156. assert_equal DateTime.civil(2006,2,28,15,15,10), DateTime.civil(2005,2,28,15,15,10).advance(:years => 1)
  157. assert_equal DateTime.civil(2005,6,28,15,15,10), DateTime.civil(2005,2,28,15,15,10).advance(:months => 4)
  158. assert_equal DateTime.civil(2005,3,21,15,15,10), DateTime.civil(2005,2,28,15,15,10).advance(:weeks => 3)
  159. assert_equal DateTime.civil(2005,3,5,15,15,10), DateTime.civil(2005,2,28,15,15,10).advance(:days => 5)
  160. assert_equal DateTime.civil(2012,9,28,15,15,10), DateTime.civil(2005,2,28,15,15,10).advance(:years => 7, :months => 7)
  161. assert_equal DateTime.civil(2013,10,3,15,15,10), DateTime.civil(2005,2,28,15,15,10).advance(:years => 7, :months => 19, :days => 5)
  162. assert_equal DateTime.civil(2013,10,17,15,15,10), DateTime.civil(2005,2,28,15,15,10).advance(:years => 7, :months => 19, :weeks => 2, :days => 5)
  163. assert_equal DateTime.civil(2001,12,27,15,15,10), DateTime.civil(2005,2,28,15,15,10).advance(:years => -3, :months => -2, :days => -1)
  164. assert_equal DateTime.civil(2005,2,28,15,15,10), DateTime.civil(2004,2,29,15,15,10).advance(:years => 1) #leap day plus one year
  165. assert_equal DateTime.civil(2005,2,28,20,15,10), DateTime.civil(2005,2,28,15,15,10).advance(:hours => 5)
  166. assert_equal DateTime.civil(2005,2,28,15,22,10), DateTime.civil(2005,2,28,15,15,10).advance(:minutes => 7)
  167. assert_equal DateTime.civil(2005,2,28,15,15,19), DateTime.civil(2005,2,28,15,15,10).advance(:seconds => 9)
  168. assert_equal DateTime.civil(2005,2,28,20,22,19), DateTime.civil(2005,2,28,15,15,10).advance(:hours => 5, :minutes => 7, :seconds => 9)
  169. assert_equal DateTime.civil(2005,2,28,10,8,1), DateTime.civil(2005,2,28,15,15,10).advance(:hours => -5, :minutes => -7, :seconds => -9)
  170. assert_equal DateTime.civil(2013,10,17,20,22,19), DateTime.civil(2005,2,28,15,15,10).advance(:years => 7, :months => 19, :weeks => 2, :days => 5, :hours => 5, :minutes => 7, :seconds => 9)
  171. end
  172. def test_advanced_processes_first_the_date_deltas_and_then_the_time_deltas
  173. # If the time deltas were processed first, the following datetimes would be advanced to 2010/04/01 instead.
  174. assert_equal DateTime.civil(2010, 3, 29), DateTime.civil(2010, 2, 28, 23, 59, 59).advance(:months => 1, :seconds => 1)
  175. assert_equal DateTime.civil(2010, 3, 29), DateTime.civil(2010, 2, 28, 23, 59).advance(:months => 1, :minutes => 1)
  176. assert_equal DateTime.civil(2010, 3, 29), DateTime.civil(2010, 2, 28, 23).advance(:months => 1, :hours => 1)
  177. assert_equal DateTime.civil(2010, 3, 29), DateTime.civil(2010, 2, 28, 22, 58, 59).advance(:months => 1, :hours => 1, :minutes => 1, :seconds => 1)
  178. end
  179. def test_prev_week
  180. assert_equal DateTime.civil(2005,2,21), DateTime.civil(2005,3,1,15,15,10).prev_week
  181. assert_equal DateTime.civil(2005,2,22), DateTime.civil(2005,3,1,15,15,10).prev_week(:tuesday)
  182. assert_equal DateTime.civil(2005,2,25), DateTime.civil(2005,3,1,15,15,10).prev_week(:friday)
  183. assert_equal DateTime.civil(2006,10,30), DateTime.civil(2006,11,6,0,0,0).prev_week
  184. assert_equal DateTime.civil(2006,11,15), DateTime.civil(2006,11,23,0,0,0).prev_week(:wednesday)
  185. end
  186. def test_next_week
  187. assert_equal DateTime.civil(2005,2,28), DateTime.civil(2005,2,22,15,15,10).next_week
  188. assert_equal DateTime.civil(2005,3,4), DateTime.civil(2005,2,22,15,15,10).next_week(:friday)
  189. assert_equal DateTime.civil(2006,10,30), DateTime.civil(2006,10,23,0,0,0).next_week
  190. assert_equal DateTime.civil(2006,11,1), DateTime.civil(2006,10,23,0,0,0).next_week(:wednesday)
  191. end
  192. def test_next_month_on_31st
  193. assert_equal DateTime.civil(2005, 9, 30), DateTime.civil(2005, 8, 31).next_month
  194. end
  195. def test_prev_month_on_31st
  196. assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).prev_month
  197. end
  198. def test_xmlschema
  199. assert_match(/^1880-02-28T15:15:10\+00:?00$/, DateTime.civil(1880, 2, 28, 15, 15, 10).xmlschema)
  200. assert_match(/^1980-02-28T15:15:10\+00:?00$/, DateTime.civil(1980, 2, 28, 15, 15, 10).xmlschema)
  201. assert_match(/^2080-02-28T15:15:10\+00:?00$/, DateTime.civil(2080, 2, 28, 15, 15, 10).xmlschema)
  202. assert_match(/^1880-02-28T15:15:10-06:?00$/, DateTime.civil(1880, 2, 28, 15, 15, 10, -0.25).xmlschema)
  203. assert_match(/^1980-02-28T15:15:10-06:?00$/, DateTime.civil(1980, 2, 28, 15, 15, 10, -0.25).xmlschema)
  204. assert_match(/^2080-02-28T15:15:10-06:?00$/, DateTime.civil(2080, 2, 28, 15, 15, 10, -0.25).xmlschema)
  205. end
  206. def test_today_with_offset
  207. Date.stubs(:current).returns(Date.new(2000, 1, 1))
  208. assert_equal false, DateTime.civil(1999,12,31,23,59,59, Rational(-18000, 86400)).today?
  209. assert_equal true, DateTime.civil(2000,1,1,0,0,0, Rational(-18000, 86400)).today?
  210. assert_equal true, DateTime.civil(2000,1,1,23,59,59, Rational(-18000, 86400)).today?
  211. assert_equal false, DateTime.civil(2000,1,2,0,0,0, Rational(-18000, 86400)).today?
  212. end
  213. def test_today_without_offset
  214. Date.stubs(:current).returns(Date.new(2000, 1, 1))
  215. assert_equal false, DateTime.civil(1999,12,31,23,59,59).today?
  216. assert_equal true, DateTime.civil(2000,1,1,0).today?
  217. assert_equal true, DateTime.civil(2000,1,1,23,59,59).today?
  218. assert_equal false, DateTime.civil(2000,1,2,0).today?
  219. end
  220. def test_past_with_offset
  221. DateTime.stubs(:current).returns(DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)))
  222. assert_equal true, DateTime.civil(2005,2,10,15,30,44, Rational(-18000, 86400)).past?
  223. assert_equal false, DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)).past?
  224. assert_equal false, DateTime.civil(2005,2,10,15,30,46, Rational(-18000, 86400)).past?
  225. end
  226. def test_past_without_offset
  227. DateTime.stubs(:current).returns(DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)))
  228. assert_equal true, DateTime.civil(2005,2,10,20,30,44).past?
  229. assert_equal false, DateTime.civil(2005,2,10,20,30,45).past?
  230. assert_equal false, DateTime.civil(2005,2,10,20,30,46).past?
  231. end
  232. def test_future_with_offset
  233. DateTime.stubs(:current).returns(DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)))
  234. assert_equal false, DateTime.civil(2005,2,10,15,30,44, Rational(-18000, 86400)).future?
  235. assert_equal false, DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)).future?
  236. assert_equal true, DateTime.civil(2005,2,10,15,30,46, Rational(-18000, 86400)).future?
  237. end
  238. def test_future_without_offset
  239. DateTime.stubs(:current).returns(DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)))
  240. assert_equal false, DateTime.civil(2005,2,10,20,30,44).future?
  241. assert_equal false, DateTime.civil(2005,2,10,20,30,45).future?
  242. assert_equal true, DateTime.civil(2005,2,10,20,30,46).future?
  243. end
  244. def test_current_returns_date_today_when_zone_is_not_set
  245. with_env_tz 'US/Eastern' do
  246. Time.stubs(:now).returns Time.local(1999, 12, 31, 23, 59, 59)
  247. assert_equal DateTime.new(1999, 12, 31, 23, 59, 59, Rational(-18000, 86400)), DateTime.current
  248. end
  249. end
  250. def test_current_returns_time_zone_today_when_zone_is_set
  251. Time.zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
  252. with_env_tz 'US/Eastern' do
  253. Time.stubs(:now).returns Time.local(1999, 12, 31, 23, 59, 59)
  254. assert_equal DateTime.new(1999, 12, 31, 23, 59, 59, Rational(-18000, 86400)), DateTime.current
  255. end
  256. ensure
  257. Time.zone = nil
  258. end
  259. def test_current_without_time_zone
  260. assert_kind_of DateTime, DateTime.current
  261. end
  262. def test_current_with_time_zone
  263. with_env_tz 'US/Eastern' do
  264. assert_kind_of DateTime, DateTime.current
  265. end
  266. end
  267. def test_acts_like_time
  268. assert DateTime.new.acts_like_time?
  269. end
  270. def test_local_offset
  271. with_env_tz 'US/Eastern' do
  272. assert_equal Rational(-5, 24), DateTime.local_offset
  273. end
  274. with_env_tz 'US/Central' do
  275. assert_equal Rational(-6, 24), DateTime.local_offset
  276. end
  277. end
  278. def test_utc?
  279. assert_equal true, DateTime.civil(2005, 2, 21, 10, 11, 12).utc?
  280. assert_equal true, DateTime.civil(2005, 2, 21, 10, 11, 12, 0).utc?
  281. assert_equal false, DateTime.civil(2005, 2, 21, 10, 11, 12, 0.25).utc?
  282. assert_equal false, DateTime.civil(2005, 2, 21, 10, 11, 12, -0.25).utc?
  283. end
  284. def test_utc_offset
  285. assert_equal 0, DateTime.civil(2005, 2, 21, 10, 11, 12).utc_offset
  286. assert_equal 0, DateTime.civil(2005, 2, 21, 10, 11, 12, 0).utc_offset
  287. assert_equal 21600, DateTime.civil(2005, 2, 21, 10, 11, 12, 0.25).utc_offset
  288. assert_equal( -21600, DateTime.civil(2005, 2, 21, 10, 11, 12, -0.25).utc_offset )
  289. assert_equal( -18000, DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-5, 24)).utc_offset )
  290. end
  291. def test_utc
  292. assert_equal DateTime.civil(2005, 2, 21, 16, 11, 12, 0), DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)).utc
  293. assert_equal DateTime.civil(2005, 2, 21, 15, 11, 12, 0), DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-5, 24)).utc
  294. assert_equal DateTime.civil(2005, 2, 21, 10, 11, 12, 0), DateTime.civil(2005, 2, 21, 10, 11, 12, 0).utc
  295. assert_equal DateTime.civil(2005, 2, 21, 9, 11, 12, 0), DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(1, 24)).utc
  296. assert_equal DateTime.civil(2005, 2, 21, 9, 11, 12, 0), DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(1, 24)).getutc
  297. end
  298. def test_formatted_offset_with_utc
  299. assert_equal '+00:00', DateTime.civil(2000).formatted_offset
  300. assert_equal '+0000', DateTime.civil(2000).formatted_offset(false)
  301. assert_equal 'UTC', DateTime.civil(2000).formatted_offset(true, 'UTC')
  302. end
  303. def test_formatted_offset_with_local
  304. dt = DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-5, 24))
  305. assert_equal '-05:00', dt.formatted_offset
  306. assert_equal '-0500', dt.formatted_offset(false)
  307. end
  308. def test_compare_with_time
  309. assert_equal 1, DateTime.civil(2000) <=> Time.utc(1999, 12, 31, 23, 59, 59)
  310. assert_equal 0, DateTime.civil(2000) <=> Time.utc(2000, 1, 1, 0, 0, 0)
  311. assert_equal(-1, DateTime.civil(2000) <=> Time.utc(2000, 1, 1, 0, 0, 1))
  312. end
  313. def test_compare_with_datetime
  314. assert_equal 1, DateTime.civil(2000) <=> DateTime.civil(1999, 12, 31, 23, 59, 59)
  315. assert_equal 0, DateTime.civil(2000) <=> DateTime.civil(2000, 1, 1, 0, 0, 0)
  316. assert_equal(-1, DateTime.civil(2000) <=> DateTime.civil(2000, 1, 1, 0, 0, 1))
  317. end
  318. def test_compare_with_time_with_zone
  319. assert_equal 1, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59), ActiveSupport::TimeZone['UTC'] )
  320. assert_equal 0, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0), ActiveSupport::TimeZone['UTC'] )
  321. assert_equal(-1, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), ActiveSupport::TimeZone['UTC'] ))
  322. end
  323. def test_to_f
  324. assert_equal 946684800.0, DateTime.civil(2000).to_f
  325. assert_equal 946684800.0, DateTime.civil(1999,12,31,19,0,0,Rational(-5,24)).to_f
  326. end
  327. def test_to_i
  328. assert_equal 946684800, DateTime.civil(2000).to_i
  329. end
  330. protected
  331. def with_env_tz(new_tz = 'US/Eastern')
  332. old_tz, ENV['TZ'] = ENV['TZ'], new_tz
  333. yield
  334. ensure
  335. old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
  336. end
  337. end