123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155(**************************************************************************)(* *)(* This file is part of Calendar. *)(* *)(* Copyright (C) 2003-2011 Julien Signoles *)(* *)(* you can redistribute it and/or modify it under the terms of the GNU *)(* Lesser General Public License version 2.1 as published by the *)(* Free Software Foundation, with a special linking exception (usual *)(* for Objective Caml libraries). *)(* *)(* It is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR *)(* *)(* See the GNU Lesser General Public Licence version 2.1 for more *)(* details (enclosed in the file LGPL). *)(* *)(* The special linking exception is detailled in the enclosed file *)(* LICENSE. *)(**************************************************************************)(*S Introduction.
A time is represents by a number of seconds in UTC.
Outside this module, a time is interpreted in the current time zone.
So, each operations have to coerce a given time according to the current
time zone. *)(*S Datatypes. *)includeUtils.Floattypesecond=floattypefield=[`Hour|`Minute|`Second](*S Conversions. *)letone_day=86400letfone_day=86400.letconverttt1t2=t+.float(3600*Time_Zone.gapt1t2)letfrom_gmtt=converttTime_Zone.UTC(Time_Zone.current())letto_gmtt=convertt(Time_Zone.current())Time_Zone.UTC(* Coerce [t] into the interval $[0; 86400[$ (i.e. a one day interval). *)letnormalizet=lett=from_gmttinlett_mod,t_div=to_gmt(mod_floattfone_day),int_of_floatt/one_dayinift<0.thent_mod+.fone_day,t_div-1elset_mod,t_div(*S Constructors. *)letmakehms=to_gmt(float(h*3600+m*60)+.s)letlmake?(hour=0)?(minute=0)?(second=0.)()=makehourminutesecondletmidnight()=to_gmt0.letmidday()=to_gmt43200.letnow()=letnow=Unix.gettimeofday()inletgmnow=Unix.gmtimenowinletfrac,_=modfnowinfloat(3600*gmnow.Unix.tm_hour+60*gmnow.Unix.tm_min+gmnow.Unix.tm_sec)+.frac(*S Getters. *)lethourt=int_of_float(from_gmtt)/3600letminutet=int_of_float(from_gmtt)mod3600/60letsecondt=mod_float(from_gmtt)60.letto_hourst=from_gmtt/.3600.letto_minutest=from_gmtt/.60.letto_secondst=from_gmtt(*S Boolean operations. *)letis_pmt=lett,_=normalizetinletm,_=normalize(midday())int<mletis_amt=lett,_=normalizetinletm,_=normalize(midday())int>=m(*S Coercions. *)letfrom_hourst=to_gmt(t*.3600.)letfrom_minutest=to_gmt(t*.60.)letfrom_secondst=to_gmtt(*S Seconds. *)moduleSecond=structtypet=secondletfrom_int=floatletto_int=int_of_floatletfrom_floatx=xletto_floatx=xend(*S Period. *)modulePeriod=structtype+'aperiod=floatconstraint'a=[<Period.date_field]includeUtils.Floatletmakehms=float(h*3600+m*60)+.sletlmake?(hour=0)?(minute=0)?(second=0.)()=makehourminutesecondletlengthx=xlethourx=float(x*3600)letminutex=float(x*60)letsecondx=xletempty=0.letadd=(+.)letsub=(-.)letmul=(*.)letdiv=(/.)letoppx=-.xletto_secondsx=xletto_minutesx=x/.60.letto_hoursx=x/.3600.end(*S Arithmetic operations on times and periods. *)letadd=(+.)letsub=(-.)letrem=(-.)letnextx=function|`Hour->x+.3600.|`Minute->x+.60.|`Second->x+.1.letprevx=function|`Hour->x-.3600.|`Minute->x-.60.|`Second->x-.1.