%!PS % begin included library code % see https://github.com/Firedrake/postscript-libraries/ /map { % array proc -> array 2 dict begin /p exch def [ exch { p } forall ] } bind def /jd2ymd { 15 dict begin /y 4716 def /v 3 def /j 1401 def /u 5 def /m 2 def /s 153 def /n 12 def /w 2 def /r 4 def /B 274277 def /p 1461 def /C -38 def dup 4 mul B add 146097 idiv 3 mul 4 idiv C add j add add /f exch def r f mul v add /e exch def e p mod r idiv u mul w add /h exch def /day h s mod u idiv 1 add def /month h s idiv m add n mod 1 add def /year e p idiv y sub n m add month sub n idiv add def [ year month day ] end } bind def /jd2dow { 1 add 7 mod } bind def /test { /test.count test.count 1 add def { /test.pass test.pass 1 add def } { ( ) print test.count (....) cvs print (-fail) print } ifelse } bind def /deepeq { 2 dict begin /a exch def /b exch def a type b type eq { a type /dicttype eq { a length b length eq { << a { pop true } forall b { pop true } forall >> true exch { pop dup a exch known { dup b exch known { dup a exch get exch b exch get deepeq not { pop false } if } { false } ifelse } { false } ifelse } forall } { false } ifelse } { a type dup /arraytype eq exch /stringtype eq or { a length b length eq { true 0 1 a length 1 sub { dup a exch get exch b exch get deepeq not { pop false exit } if } for } { false } ifelse } { a b eq } ifelse } ifelse } { false } ifelse end } bind def /test.end { ( ) print test.count 0 gt { (Passed ) print test.pass (...) cvs print (/) print test.count (...) cvs print ( \() print test.pass 100 mul test.count idiv (...) cvs print (%\)) print (\r\n) print } if } bind def /ymd2jd { 4 dict begin aload pop /d exch def /m exch def /y exch def /mn m 14 sub 12 idiv def y 4800 add mn add 1461 mul 4 idiv mn 12 mul neg 2 sub m add 367 mul 12 idiv add y 4900 add mn add 100 idiv 3 mul 4 idiv sub d add 32075 sub end } bind def /test.start { print (:) print /test.pass 0 def /test.count 0 def } bind def % end included library code /addbizhours { 9 dict begin /minutes exch 60 mul cvi def /instr exch def /current [ [ instr 0 4 getinterval cvi instr 5 2 getinterval cvi instr 8 2 getinterval cvi ] ymd2jd instr 11 2 getinterval cvi 60 mul instr 14 2 getinterval cvi add ] def /cdstart 9 60 mul def /cdend 18 60 mul def /bizdaylength cdend cdstart sub def current isbiz not { /current current nextbizstart def } if /ed current nextbizend def /remain ed 1 get current 1 get sub def remain minutes lt { /minutes minutes remain sub def /current ed nextbizstart def { minutes bizdaylength le { exit } if /current current nextbizstart def /minutes minutes bizdaylength sub def } loop } if current 1 current 1 get minutes add put /current current reconcile def /out (....-..-.. ..:..) def current 0 get jd2ymd dup dup 0 get 4 string cvs out exch 0 exch putinterval 1 get 2 string cvs dup length 1 eq { (0.) dup 1 4 -1 roll putinterval } if out exch 5 exch putinterval 2 get 2 string cvs dup length 1 eq { (0.) dup 1 4 -1 roll putinterval } if out exch 8 exch putinterval out 11 current 1 get 60 idiv 2 string cvs dup length 1 eq { (0.) dup 1 4 -1 roll putinterval } if putinterval out 14 current 1 get 60 mod 2 string cvs dup length 1 eq { (0.) dup 1 4 -1 roll putinterval } if putinterval out end } bind def /isbiz { dup 0 get jd2dow dup 0 eq exch 6 eq or { pop false } { 1 get 1 { dup cdstart lt { pop false exit } if cdend ge { false exit } if true } repeat } ifelse } bind def /nextbizstart { 1 dict begin { } map /tm exch def { tm 0 get jd2dow dup 0 ne exch 6 ne and { exit } if tm 0 tm 0 get 1 add put tm 1 cdstart put } loop tm 1 get cdstart lt { tm 1 cdstart put } { { tm 0 tm 0 get 1 add put tm 1 cdstart put tm 0 get jd2dow dup 0 ne exch 6 ne and { exit } if } loop } ifelse tm end } bind def /nextbizend { 1 dict begin { } map /tm exch def { tm 0 get jd2dow dup 0 ne exch 6 ne and { exit } if tm 0 tm 0 get 1 add put tm 1 cdend put } loop tm 1 get cdend ge { { tm 0 tm 0 get 1 add put tm 1 cdend put tm 0 get jd2dow dup 0 ne exch 6 ne and { exit } if } loop } { tm 1 cdend put } ifelse tm end } bind def /reconcile { 1 dict begin /c exch def { c 1 get 1440 lt { exit } if c 1 c 1 get 1440 sub put c 0 c 0 get 1 add put } loop c end } bind def (addbizhours) test.start (2022-08-01 10:30) 4 addbizhours (2022-08-01 14:30) deepeq test (2022-08-01 17:00) 3.5 addbizhours (2022-08-02 11:30) deepeq test (2022-08-01 18:00) 3.5 addbizhours (2022-08-02 12:30) deepeq test (2022-08-01 19:00) 3.5 addbizhours (2022-08-02 12:30) deepeq test (2022-08-05 17:00) 3.5 addbizhours (2022-08-08 11:30) deepeq test (2022-08-01 12:00) 18.0 addbizhours (2022-08-03 12:00) deepeq test test.end