working_time_test.rb 16 KB

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