123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557 |
- # encoding: utf-8
- require 'test_helper'
- require 'time_calculation'
- class WorkingTimeTest < ActiveSupport::TestCase
- test 'working time' do
- tests = [
- # test 1
- {
- :start => '2012-12-17 08:00:00',
- :end => '2012-12-18 08:00:00',
- :diff => 600,
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 2
- {
- :start => '2012-12-17 08:00:00',
- :end => '2012-12-17 09:00:00',
- :diff => 60,
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 3
- {
- :start => '2012-12-17 08:00:00',
- :end => '2012-12-17 08:15:00',
- :diff => 15,
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 4
- {
- :start => '2012-12-23 08:00:00',
- :end => '2012-12-27 10:30:42',
- # :diff => 0,
- :diff => 151,
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- 'holidays' => [
- '2012-12-24', '2012-12-25', '2012-12-26'
- ],
- },
- },
- # test 5
- {
- :start => '2013-02-28 17:00:00',
- :end => '2013-02-28 23:59:59',
- :diff => 60,
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 6
- {
- :start => '2013-02-28 17:00:00',
- :end => '2013-03-08 23:59:59',
- :diff => 3660,
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 7
- {
- :start => '2012-02-28 17:00:00',
- :end => '2013-03-08 23:59:59',
- :diff => 160860,
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 8
- {
- :start => '2013-02-28 17:01:00',
- :end => '2013-02-28 18:10:59',
- :diff => 61,
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 9
- {
- :start => '2013-02-28 18:01:00',
- :end => '2013-02-28 18:10:59',
- :diff => 0,
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 10 / summertime
- {
- :start => '2013-02-28 18:01:00',
- :end => '2013-02-28 18:10:59',
- :diff => 0,
- :timezone => 'Europe/Berlin',
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 11 / summertime
- {
- :start => '2013-02-28 17:01:00',
- :end => '2013-02-28 17:10:59',
- :diff => 0,
- :timezone => 'Europe/Berlin',
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 12 / wintertime
- {
- :start => '2013-08-29 17:01:00',
- :end => '2013-08-29 17:10:59',
- :diff => 0,
- :timezone => 'Europe/Berlin',
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 13 / summertime
- {
- :start => '2013-02-28 16:01:00',
- :end => '2013-02-28 16:10:59',
- :diff => 10,
- :timezone => 'Europe/Berlin',
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 14 / wintertime
- {
- :start => '2013-08-29 16:01:00',
- :end => '2013-08-29 16:10:59',
- :diff => 0,
- :timezone => 'Europe/Berlin',
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 15
- {
- :start => '2013-08-29 16:01:00',
- :end => '2013-08-29 16:10:59',
- :diff => 10,
- },
- ]
- tests.each { |test|
- diff = TimeCalculation.business_time_diff( test[:start], test[:end], test[:config], test[:timezone] )
- assert_equal( diff, test[:diff], 'diff' )
- }
- end
- test 'dest time' do
- tests = [
- # test 1
- {
- :start => '2012-12-17 08:00:00',
- :dest_time => '2012-12-17 18:00:00',
- :diff => 600,
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 2
- {
- :start => '2012-12-17 08:00:00',
- :dest_time => '2012-12-18 08:30:00',
- :diff => 630,
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 3
- {
- :start => '2012-12-17 08:00:00',
- :dest_time => '2012-12-18 18:00:00',
- :diff => 1200,
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 4
- {
- :start => '2012-12-17 08:00:00',
- :dest_time => '2012-12-19 08:30:00',
- :diff => 1230,
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 5
- {
- :start => '2012-12-17 08:00:00',
- :dest_time => '2012-12-21 18:00:00',
- :diff => 3000,
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 6
- {
- :start => '2012-12-17 08:00:00',
- :dest_time => '2012-12-24 08:05:00',
- :diff => 3005,
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 7
- {
- :start => '2012-12-17 08:00:00',
- :dest_time => '2012-12-31 08:05:00',
- :diff => 6005,
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 8
- {
- :start => '2012-12-17 08:00:00',
- :dest_time => '2012-12-31 13:30:00',
- :diff => 6330,
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 9
- {
- :start => '2013-04-12 21:20:15',
- :dest_time => '2013-04-15 10:00:00',
- :diff => 120,
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 11 / summertime 7am-5pm
- {
- :start => '2013-03-08 21:20:15',
- :dest_time => '2013-03-11 09:00:00',
- :diff => 120,
- :timezone => 'Europe/Berlin',
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 12 / wintertime 6am-4pm
- {
- :start => '2013-09-06 21:20:15',
- :dest_time => '2013-09-09 08:00:00',
- :diff => 120,
- :timezone => 'Europe/Berlin',
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 13 / wintertime - 7am-4pm
- {
- :start => '2013-10-21 06:30:00',
- :dest_time => '2013-10-21 09:00:00',
- :diff => 120,
- :timezone => 'Europe/Berlin',
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '9:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 14 / wintertime - 7am-4pm
- {
- :start => '2013-10-21 04:34:15',
- :dest_time => '2013-10-21 09:00:00',
- :diff => 120,
- :timezone => 'Europe/Berlin',
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '9:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 15 / wintertime - 7am-4pm
- {
- :start => '2013-10-20 22:34:15',
- :dest_time => '2013-10-21 09:00:00',
- :diff => 120,
- :timezone => 'Europe/Berlin',
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '9:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 16 / wintertime - 7am-4pm
- {
- :start => '2013-10-21 07:00:15',
- :dest_time => '2013-10-21 09:00:15',
- :diff => 120,
- :timezone => 'Europe/Berlin',
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '9:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 17
- {
- :start => '2013-10-21 04:01:00',
- :dest_time => '2013-10-21 06:00:00',
- :diff => 119,
- },
- # test 18
- {
- :start => '2013-10-21 04:01:00',
- :dest_time => '2013-10-21 04:01:00',
- :diff => 0,
- },
- # test 19
- {
- :start => '2013-04-12 21:20:15',
- :dest_time => '2013-04-12 21:20:15',
- :diff => 0,
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- # test 20
- {
- :start => '2013-04-12 11:20:15',
- :dest_time => '2013-04-12 11:21:15',
- :diff => 1,
- :config => {
- 'Mon' => true,
- 'Tue' => true,
- 'Wed' => true,
- 'Thu' => true,
- 'Fri' => true,
- 'beginning_of_workday' => '8:00 am',
- 'end_of_workday' => '6:00 pm',
- },
- },
- ]
- tests.each { |test|
- dest_time = TimeCalculation.dest_time( test[:start] + ' UTC', test[:diff], test[:config], test[:timezone] )
- assert_equal( dest_time.gmtime, Time.parse( test[:dest_time] + ' UTC' ), "dest time - #{test[:dest_time].to_s}" )
- }
- end
- end
|