123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148(**************************************************************************)(* *)(* 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.Inttypesecond=inttypefield=[`Hour|`Minute|`Second](*S Conversions. *)letone_day=86400letconverttt1t2=t+3600*Time_Zone.gapt1t2letfrom_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(tmodone_day),t/one_dayinift<0thent_mod+one_day,t_div-1elset_mod,t_div(*S Constructors. *)letmakehms=to_gmt(h*3600+m*60+s)letlmake?(hour=0)?(minute=0)?(second=0)()=makehourminutesecondletmidnight()=to_gmt0letmidday()=to_gmt43200letnow()=letnow=Unix.gmtime(Unix.time())in3600*now.Unix.tm_hour+60*now.Unix.tm_min+now.Unix.tm_sec(*S Getters. *)lethourt=from_gmtt/3600letminutet=from_gmttmod3600/60letsecondt=from_gmttmod60letto_hourst=float(from_gmtt)/.3600.letto_minutest=float(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(int_of_float(t*.3600.))letfrom_minutest=to_gmt(int_of_float(t*.60.))letfrom_secondst=to_gmtt(*S Seconds. *)moduleSecond=structtypet=secondletfrom_intx=xletto_intx=xletfrom_float=Utils.Float.roundletto_float=floatend(*S Period. *)modulePeriod=structtype+'aperiod=intconstraint'a=[<Period.date_field]includeUtils.Intletmakehms=h*3600+m*60+sletlmake?(hour=0)?(minute=0)?(second=0)()=makehourminutesecondletlengthx=xlethourx=x*3600letminutex=x*60letsecondx=xletempty=0letadd=(+)letsub=(-)letmul=(*)letdiv=(/)letoppx=-xletto_secondsx=xletto_minutesx=floatx/.60.letto_hoursx=floatx/.3600.end(*S Arithmetic operations on times and periods. *)letadd=(+)letsub=(-)letrem=(-)letnextx=function|`Hour->x+3600|`Minute->x+60|`Second->x+1letprevx=function|`Hour->x-3600|`Minute->x-60|`Second->x-1