working_time_test.rb 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. # encoding: utf-8
  2. require 'test_helper'
  3. require 'time_calculation'
  4. class WorkingTimeTest < ActiveSupport::TestCase
  5. test 'working time' do
  6. tests = [
  7. # test 1
  8. {
  9. :start => '2012-12-17 08:00:00',
  10. :end => '2012-12-18 08:00:00',
  11. :diff => 600,
  12. :config => {
  13. 'Mon' => true,
  14. 'Tue' => true,
  15. 'Wed' => true,
  16. 'Thu' => true,
  17. 'Fri' => true,
  18. 'beginning_of_workday' => '8:00 am',
  19. 'end_of_workday' => '6:00 pm',
  20. },
  21. },
  22. # test 2
  23. {
  24. :start => '2012-12-17 08:00:00',
  25. :end => '2012-12-17 09:00:00',
  26. :diff => 60,
  27. :config => {
  28. 'Mon' => true,
  29. 'Tue' => true,
  30. 'Wed' => true,
  31. 'Thu' => true,
  32. 'Fri' => true,
  33. 'beginning_of_workday' => '8:00 am',
  34. 'end_of_workday' => '6:00 pm',
  35. },
  36. },
  37. # test 3
  38. {
  39. :start => '2012-12-17 08:00:00',
  40. :end => '2012-12-17 08:15:00',
  41. :diff => 15,
  42. :config => {
  43. 'Mon' => true,
  44. 'Tue' => true,
  45. 'Wed' => true,
  46. 'Thu' => true,
  47. 'Fri' => true,
  48. 'beginning_of_workday' => '8:00 am',
  49. 'end_of_workday' => '6:00 pm',
  50. },
  51. },
  52. # test 4
  53. {
  54. :start => '2012-12-23 08:00:00',
  55. :end => '2012-12-27 10:30:42',
  56. # :diff => 0,
  57. :diff => 151,
  58. :config => {
  59. 'Mon' => true,
  60. 'Tue' => true,
  61. 'Wed' => true,
  62. 'Thu' => true,
  63. 'Fri' => true,
  64. 'beginning_of_workday' => '8:00 am',
  65. 'end_of_workday' => '6:00 pm',
  66. 'holidays' => [
  67. '2012-12-24', '2012-12-25', '2012-12-26'
  68. ],
  69. },
  70. },
  71. # test 5
  72. {
  73. :start => '2013-02-28 17:00:00',
  74. :end => '2013-02-28 23:59:59',
  75. :diff => 60,
  76. :config => {
  77. 'Mon' => true,
  78. 'Tue' => true,
  79. 'Wed' => true,
  80. 'Thu' => true,
  81. 'Fri' => true,
  82. 'beginning_of_workday' => '8:00 am',
  83. 'end_of_workday' => '6:00 pm',
  84. },
  85. },
  86. # test 6
  87. {
  88. :start => '2013-02-28 17:00:00',
  89. :end => '2013-03-08 23:59:59',
  90. :diff => 3660,
  91. :config => {
  92. 'Mon' => true,
  93. 'Tue' => true,
  94. 'Wed' => true,
  95. 'Thu' => true,
  96. 'Fri' => true,
  97. 'beginning_of_workday' => '8:00 am',
  98. 'end_of_workday' => '6:00 pm',
  99. },
  100. },
  101. # test 7
  102. {
  103. :start => '2012-02-28 17:00:00',
  104. :end => '2013-03-08 23:59:59',
  105. :diff => 160860,
  106. :config => {
  107. 'Mon' => true,
  108. 'Tue' => true,
  109. 'Wed' => true,
  110. 'Thu' => true,
  111. 'Fri' => true,
  112. 'beginning_of_workday' => '8:00 am',
  113. 'end_of_workday' => '6:00 pm',
  114. },
  115. },
  116. # test 8
  117. {
  118. :start => '2013-02-28 17:01:00',
  119. :end => '2013-02-28 18:10:59',
  120. :diff => 61,
  121. :config => {
  122. 'Mon' => true,
  123. 'Tue' => true,
  124. 'Wed' => true,
  125. 'Thu' => true,
  126. 'Fri' => true,
  127. 'beginning_of_workday' => '8:00 am',
  128. 'end_of_workday' => '6:00 pm',
  129. },
  130. },
  131. # test 9
  132. {
  133. :start => '2013-02-28 18:01:00',
  134. :end => '2013-02-28 18:10:59',
  135. :diff => 0,
  136. :config => {
  137. 'Mon' => true,
  138. 'Tue' => true,
  139. 'Wed' => true,
  140. 'Thu' => true,
  141. 'Fri' => true,
  142. 'beginning_of_workday' => '8:00 am',
  143. 'end_of_workday' => '6:00 pm',
  144. },
  145. },
  146. # test 10 / summertime
  147. {
  148. :start => '2013-02-28 18:01:00',
  149. :end => '2013-02-28 18:10:59',
  150. :diff => 0,
  151. :timezone => 'Europe/Berlin',
  152. :config => {
  153. 'Mon' => true,
  154. 'Tue' => true,
  155. 'Wed' => true,
  156. 'Thu' => true,
  157. 'Fri' => true,
  158. 'beginning_of_workday' => '8:00 am',
  159. 'end_of_workday' => '6:00 pm',
  160. },
  161. },
  162. # test 11 / summertime
  163. {
  164. :start => '2013-02-28 17:01:00',
  165. :end => '2013-02-28 17:10:59',
  166. :diff => 0,
  167. :timezone => 'Europe/Berlin',
  168. :config => {
  169. 'Mon' => true,
  170. 'Tue' => true,
  171. 'Wed' => true,
  172. 'Thu' => true,
  173. 'Fri' => true,
  174. 'beginning_of_workday' => '8:00 am',
  175. 'end_of_workday' => '6:00 pm',
  176. },
  177. },
  178. # test 12 / wintertime
  179. {
  180. :start => '2013-08-29 17:01:00',
  181. :end => '2013-08-29 17:10:59',
  182. :diff => 0,
  183. :timezone => 'Europe/Berlin',
  184. :config => {
  185. 'Mon' => true,
  186. 'Tue' => true,
  187. 'Wed' => true,
  188. 'Thu' => true,
  189. 'Fri' => true,
  190. 'beginning_of_workday' => '8:00 am',
  191. 'end_of_workday' => '6:00 pm',
  192. },
  193. },
  194. # test 13 / summertime
  195. {
  196. :start => '2013-02-28 16:01:00',
  197. :end => '2013-02-28 16:10:59',
  198. :diff => 10,
  199. :timezone => 'Europe/Berlin',
  200. :config => {
  201. 'Mon' => true,
  202. 'Tue' => true,
  203. 'Wed' => true,
  204. 'Thu' => true,
  205. 'Fri' => true,
  206. 'beginning_of_workday' => '8:00 am',
  207. 'end_of_workday' => '6:00 pm',
  208. },
  209. },
  210. # test 14 / wintertime
  211. {
  212. :start => '2013-08-29 16:01:00',
  213. :end => '2013-08-29 16:10:59',
  214. :diff => 0,
  215. :timezone => 'Europe/Berlin',
  216. :config => {
  217. 'Mon' => true,
  218. 'Tue' => true,
  219. 'Wed' => true,
  220. 'Thu' => true,
  221. 'Fri' => true,
  222. 'beginning_of_workday' => '8:00 am',
  223. 'end_of_workday' => '6:00 pm',
  224. },
  225. },
  226. # test 15
  227. {
  228. :start => '2013-08-29 16:01:00',
  229. :end => '2013-08-29 16:10:59',
  230. :diff => 10,
  231. },
  232. ]
  233. tests.each { |test|
  234. diff = TimeCalculation.business_time_diff( test[:start], test[:end], test[:config], test[:timezone] )
  235. assert_equal( diff, test[:diff], 'diff' )
  236. }
  237. end
  238. test 'dest time' do
  239. tests = [
  240. # test 1
  241. {
  242. :start => '2012-12-17 08:00:00',
  243. :dest_time => '2012-12-17 18:00:00',
  244. :diff => 600,
  245. :config => {
  246. 'Mon' => true,
  247. 'Tue' => true,
  248. 'Wed' => true,
  249. 'Thu' => true,
  250. 'Fri' => true,
  251. 'beginning_of_workday' => '8:00 am',
  252. 'end_of_workday' => '6:00 pm',
  253. },
  254. },
  255. # test 2
  256. {
  257. :start => '2012-12-17 08:00:00',
  258. :dest_time => '2012-12-18 08:30:00',
  259. :diff => 630,
  260. :config => {
  261. 'Mon' => true,
  262. 'Tue' => true,
  263. 'Wed' => true,
  264. 'Thu' => true,
  265. 'Fri' => true,
  266. 'beginning_of_workday' => '8:00 am',
  267. 'end_of_workday' => '6:00 pm',
  268. },
  269. },
  270. # test 3
  271. {
  272. :start => '2012-12-17 08:00:00',
  273. :dest_time => '2012-12-18 18:00:00',
  274. :diff => 1200,
  275. :config => {
  276. 'Mon' => true,
  277. 'Tue' => true,
  278. 'Wed' => true,
  279. 'Thu' => true,
  280. 'Fri' => true,
  281. 'beginning_of_workday' => '8:00 am',
  282. 'end_of_workday' => '6:00 pm',
  283. },
  284. },
  285. # test 4
  286. {
  287. :start => '2012-12-17 08:00:00',
  288. :dest_time => '2012-12-19 08:30:00',
  289. :diff => 1230,
  290. :config => {
  291. 'Mon' => true,
  292. 'Tue' => true,
  293. 'Wed' => true,
  294. 'Thu' => true,
  295. 'Fri' => true,
  296. 'beginning_of_workday' => '8:00 am',
  297. 'end_of_workday' => '6:00 pm',
  298. },
  299. },
  300. # test 5
  301. {
  302. :start => '2012-12-17 08:00:00',
  303. :dest_time => '2012-12-21 18:00:00',
  304. :diff => 3000,
  305. :config => {
  306. 'Mon' => true,
  307. 'Tue' => true,
  308. 'Wed' => true,
  309. 'Thu' => true,
  310. 'Fri' => true,
  311. 'beginning_of_workday' => '8:00 am',
  312. 'end_of_workday' => '6:00 pm',
  313. },
  314. },
  315. # test 6
  316. {
  317. :start => '2012-12-17 08:00:00',
  318. :dest_time => '2012-12-24 08:05:00',
  319. :diff => 3005,
  320. :config => {
  321. 'Mon' => true,
  322. 'Tue' => true,
  323. 'Wed' => true,
  324. 'Thu' => true,
  325. 'Fri' => true,
  326. 'beginning_of_workday' => '8:00 am',
  327. 'end_of_workday' => '6:00 pm',
  328. },
  329. },
  330. # test 7
  331. {
  332. :start => '2012-12-17 08:00:00',
  333. :dest_time => '2012-12-31 08:05:00',
  334. :diff => 6005,
  335. :config => {
  336. 'Mon' => true,
  337. 'Tue' => true,
  338. 'Wed' => true,
  339. 'Thu' => true,
  340. 'Fri' => true,
  341. 'beginning_of_workday' => '8:00 am',
  342. 'end_of_workday' => '6:00 pm',
  343. },
  344. },
  345. # test 8
  346. {
  347. :start => '2012-12-17 08:00:00',
  348. :dest_time => '2012-12-31 13:30:00',
  349. :diff => 6330,
  350. :config => {
  351. 'Mon' => true,
  352. 'Tue' => true,
  353. 'Wed' => true,
  354. 'Thu' => true,
  355. 'Fri' => true,
  356. 'beginning_of_workday' => '8:00 am',
  357. 'end_of_workday' => '6:00 pm',
  358. },
  359. },
  360. # test 9
  361. {
  362. :start => '2013-04-12 21:20:15',
  363. :dest_time => '2013-04-15 10:00:00',
  364. :diff => 120,
  365. :config => {
  366. 'Mon' => true,
  367. 'Tue' => true,
  368. 'Wed' => true,
  369. 'Thu' => true,
  370. 'Fri' => true,
  371. 'beginning_of_workday' => '8:00 am',
  372. 'end_of_workday' => '6:00 pm',
  373. },
  374. },
  375. # test 11 / summertime 7am-5pm
  376. {
  377. :start => '2013-03-08 21:20:15',
  378. :dest_time => '2013-03-11 09:00:00',
  379. :diff => 120,
  380. :timezone => 'Europe/Berlin',
  381. :config => {
  382. 'Mon' => true,
  383. 'Tue' => true,
  384. 'Wed' => true,
  385. 'Thu' => true,
  386. 'Fri' => true,
  387. 'beginning_of_workday' => '8:00 am',
  388. 'end_of_workday' => '6:00 pm',
  389. },
  390. },
  391. # test 12 / wintertime 6am-4pm
  392. {
  393. :start => '2013-09-06 21:20:15',
  394. :dest_time => '2013-09-09 08:00:00',
  395. :diff => 120,
  396. :timezone => 'Europe/Berlin',
  397. :config => {
  398. 'Mon' => true,
  399. 'Tue' => true,
  400. 'Wed' => true,
  401. 'Thu' => true,
  402. 'Fri' => true,
  403. 'beginning_of_workday' => '8:00 am',
  404. 'end_of_workday' => '6:00 pm',
  405. },
  406. },
  407. # test 13 / wintertime - 7am-4pm
  408. {
  409. :start => '2013-10-21 06:30:00',
  410. :dest_time => '2013-10-21 09:00:00',
  411. :diff => 120,
  412. :timezone => 'Europe/Berlin',
  413. :config => {
  414. 'Mon' => true,
  415. 'Tue' => true,
  416. 'Wed' => true,
  417. 'Thu' => true,
  418. 'Fri' => true,
  419. 'beginning_of_workday' => '9:00 am',
  420. 'end_of_workday' => '6:00 pm',
  421. },
  422. },
  423. # test 14 / wintertime - 7am-4pm
  424. {
  425. :start => '2013-10-21 04:34:15',
  426. :dest_time => '2013-10-21 09:00:00',
  427. :diff => 120,
  428. :timezone => 'Europe/Berlin',
  429. :config => {
  430. 'Mon' => true,
  431. 'Tue' => true,
  432. 'Wed' => true,
  433. 'Thu' => true,
  434. 'Fri' => true,
  435. 'beginning_of_workday' => '9:00 am',
  436. 'end_of_workday' => '6:00 pm',
  437. },
  438. },
  439. # test 15 / wintertime - 7am-4pm
  440. {
  441. :start => '2013-10-20 22:34:15',
  442. :dest_time => '2013-10-21 09:00:00',
  443. :diff => 120,
  444. :timezone => 'Europe/Berlin',
  445. :config => {
  446. 'Mon' => true,
  447. 'Tue' => true,
  448. 'Wed' => true,
  449. 'Thu' => true,
  450. 'Fri' => true,
  451. 'beginning_of_workday' => '9:00 am',
  452. 'end_of_workday' => '6:00 pm',
  453. },
  454. },
  455. # test 16 / wintertime - 7am-4pm
  456. {
  457. :start => '2013-10-21 07:00:15',
  458. :dest_time => '2013-10-21 09:00:15',
  459. :diff => 120,
  460. :timezone => 'Europe/Berlin',
  461. :config => {
  462. 'Mon' => true,
  463. 'Tue' => true,
  464. 'Wed' => true,
  465. 'Thu' => true,
  466. 'Fri' => true,
  467. 'beginning_of_workday' => '9:00 am',
  468. 'end_of_workday' => '6:00 pm',
  469. },
  470. },
  471. # test 17
  472. {
  473. :start => '2013-10-21 04:01:00',
  474. :dest_time => '2013-10-21 06:00:00',
  475. :diff => 119,
  476. },
  477. # test 18
  478. {
  479. :start => '2013-10-21 04:01:00',
  480. :dest_time => '2013-10-21 04:01:00',
  481. :diff => 0,
  482. },
  483. # test 19
  484. {
  485. :start => '2013-04-12 21:20:15',
  486. :dest_time => '2013-04-12 21:20:15',
  487. :diff => 0,
  488. :config => {
  489. 'Mon' => true,
  490. 'Tue' => true,
  491. 'Wed' => true,
  492. 'Thu' => true,
  493. 'Fri' => true,
  494. 'beginning_of_workday' => '8:00 am',
  495. 'end_of_workday' => '6:00 pm',
  496. },
  497. },
  498. # test 20
  499. {
  500. :start => '2013-04-12 11:20:15',
  501. :dest_time => '2013-04-12 11:21:15',
  502. :diff => 1,
  503. :config => {
  504. 'Mon' => true,
  505. 'Tue' => true,
  506. 'Wed' => true,
  507. 'Thu' => true,
  508. 'Fri' => true,
  509. 'beginning_of_workday' => '8:00 am',
  510. 'end_of_workday' => '6:00 pm',
  511. },
  512. },
  513. ]
  514. tests.each { |test|
  515. dest_time = TimeCalculation.dest_time( test[:start] + ' UTC', test[:diff], test[:config], test[:timezone] )
  516. assert_equal( dest_time.gmtime, Time.parse( test[:dest_time] + ' UTC' ), "dest time - #{test[:dest_time].to_s}" )
  517. }
  518. end
  519. end