Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file time.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* Copyright (c) 2019 Nomadic Labs, <contact@nomadic-labs.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)letmax_daysL=(* [= 2932896L] which is less than [Stdlib.max_int] even on 32-bit
architecture. This ensures [Int64.to_int] is accurate no matter what. *)letmax_days,_=Ptime.(Span.to_d_ps(to_spanmax))inInt64.of_intmax_daysletmin_daysL=(* Same as [max_daysL] but min. *)letmin_days,_=Ptime.(Span.to_d_ps(to_spanmin))inInt64.of_intmin_daysmoduleProtocol=structtypet=int64include(Compare.Int64:Compare.Swithtypet:=int64)letepoch=0Lletdiff=Int64.subletadd=Int64.addletof_ptimet=letdays,ps=Ptime.Span.to_d_ps(Ptime.to_spant)inlets_days=Int64.mul(Int64.of_intdays)86_400LinInt64.adds_days(Int64.divps1_000_000_000_000L)letto_ptimet=letdaysL=Int64.divt86_400Linletps=Int64.mul(Int64.remt86_400L)1_000_000_000_000LinletdaysL,ps=ifps<0Lthen(* [Ptime.Span.of_d_ps] only accepts picoseconds in the range 0L-86_399_999_999_999_999L. Subtract a day and add a day's worth of picoseconds if need be. *)(Int64.preddaysL,Int64.(addps(mul86_400L1_000_000_000_000L)))else(daysL,ps)inifCompare.Int64.(daysL>max_daysL||daysL<min_daysL)theninvalid_arg"Time.Protocol.to_ptime"(* already out of range *)elseletdays=Int64.to_intdaysLinmatchOption.bind(Ptime.Span.of_d_ps(days,ps))Ptime.of_spanwith|None->invalid_arg"Time.Protocol.to_ptime"|Someptime->ptimeletof_notations=matchPtime.of_rfc3339swith|Ok(t,_,_)->Some(of_ptimet)|Error_->Noneletof_notation_exns=matchPtime.(rfc3339_error_to_msg(of_rfc3339s))with|Error(`Msgmsg)->invalid_arg("Time.Protocol.of_notation: "^msg)|Ok(t,_,_)->of_ptimetletto_notationt=Ptime.to_rfc3339~frac_s:0~tz_offset_s:0(to_ptimet)letof_secondsx=xletto_secondsx=xletrfc_encoding=letopenData_encodingindef"timestamp.rfc"~title:"RFC 3339 formatted timestamp"~description:"A date in RFC 3339 notation."@@convto_notation(funs->matchof_notationswith|Somes->s|None->Data_encoding.Json.cannot_destruct"Time.Protocol.of_notation")stringletmax_rfc3339=of_ptimePtime.maxletmin_rfc3339=of_ptimePtime.minletas_string_encoding=letopenData_encodinginconv(funi->ifmin_rfc3339<=i&&i<=max_rfc3339thento_notationielseInt64.to_stringi)(Json.wrap_error(* NOTE: this encoding is only used as a building block for a json
encoding so we can raise the json exception directly. *)@@funs->matchof_notationswith|Somei->i|None->(matchInt64.of_string_optswith|Somei->i|None->raise(Invalid_argument"Time.Protocol.decoding")))stringletencoding=letopenData_encodingindef"timestamp.protocol"~description:"A timestamp as seen by the protocol: second-level precision, epoch \
based."@@splitted~binary:int64~json:as_string_encodingletrpc_arg=Tezos_rpc.Arg.make~name:"date"~descr:"A date in seconds from epoch"~destruct:(function|"none"|"epoch"->Okepoch|s->(matchInt64.of_string_optswith|Somet->Okt|None->Error(Format.asprintf"failed to parse time (epoch): %S"s)))~construct:Int64.to_string()letppppft=Format.fprintfppf"%Ld"tletpp_humppft=Ptime.pp_rfc3339()ppf(to_ptimet)endmoduleSystem=structletfrac_s=3(* sub-second fractional precision for pretty-printing *)typet=Ptime.tletnow()=Ptime_clock.now()includeCompare.Make(Ptime)letepoch=Ptime.epochmoduleSpan=structtypet=Ptime.Span.tletmultiply_exnfs=letopenPtime.SpaninWithExceptions.Option.to_exn~none:(Failure"Time.System.Span.multiply_exn")(of_float_s(f*.Ptime.Span.to_float_ss))letof_seconds_exnf=matchPtime.Span.of_float_sfwith|None->invalid_arg"Time.System.Span.of_seconds_exn"|Somes->sletpp_hum=Ptime.Span.ppletencoding=letopenData_encodingindef"timespan.system"~description:"A span of time, as seen by the local computer."@@convPtime.Span.to_float_s(funf->matchPtime.Span.of_float_sfwith|None->invalid_arg"Time.System.Span.encoding"|Somes->s)floatletrpc_arg=Tezos_rpc.Arg.make~name:"timespan"~descr:"A span of time in seconds"~destruct:(funs->matchfloat_of_stringswith|exceptionFailure_->Error(Format.asprintf"failed to parse timespan: %S"s)|f->(matchPtime.Span.of_float_sfwith|Somet->Okt|None->Error(Format.asprintf"failed to parse timespan: %S"s)))~construct:(funs->string_of_float(Ptime.Span.to_float_ss))()endletof_seconds_optseconds=letx=Int64.abssecondsinletdaysL=Int64.divx86_400LinifCompare.Int64.(daysL>max_daysL||daysL<min_daysL)thenNone(* already out of range *)elseletdays=Int64.to_intdaysLinletps=Int64.mul(Int64.remx86_400L)1_000_000_000_000LinmatchPtime.Span.of_d_ps(days,ps)with|None->None|Somespan->letspan=ifCompare.Int64.(seconds<0L)thenPtime.Span.negspanelsespaninPtime.of_spanspanletof_seconds_exnx=matchof_seconds_optxwith|Somet->t|None->invalid_arg"Time.of_seconds"letto_secondsx=letdays,ps=Ptime.(Span.to_d_ps(to_spanx))inlets_days=Int64.mul(Int64.of_intdays)86_400LinInt64.adds_days(Int64.divps1_000_000_000_000L)letof_protocol_exn=of_seconds_exnletof_protocol_opt=of_seconds_optletto_protocol=to_secondsletof_notation_opts=matchPtime.of_rfc3339swithOk(t,_,_)->Somet|Error_->Noneletof_notation_exns=matchPtime.(rfc3339_error_to_msg(of_rfc3339s))with|Ok(t,_,_)->t|Error(`Msgmsg)->invalid_arg("Time.of_notation: "^msg)letto_notationt=Ptime.to_rfc3339~frac_stletrfc_encoding=letopenData_encodingindef"timestamp.rfc"~title:"RFC 3339 formatted timestamp"~description:"A date in RFC 3339 notation."@@convto_notation(funs->matchof_notation_optswith|Somes->s|None->Data_encoding.Json.cannot_destruct"Time.of_notation")stringletencoding=letopenData_encodinginletbinary=convto_secondsof_seconds_exnint64inletjson=union[caseJson_only~title:"RFC encoding"rfc_encoding(funi->Somei)(funi->i);caseJson_only~title:"Second since epoch"int64(fun_->None)(funi->of_seconds_exni);]indef"timestamp.system"~description:"A timestamp as seen by the underlying, local computer: \
subsecond-level precision, epoch or rfc3339 based."@@splitted~binary~jsonletrpc_arg=Tezos_rpc.Arg.make~name:"date"~descr:"A date in seconds from epoch"~destruct:(function|"none"|"epoch"->OkPtime.epoch|s->(matchof_notation_optswith|Somet->Okt|None->(matchof_seconds_exn(Int64.of_strings)with|t->Okt|(exceptionFailure_)|(exceptionInvalid_argument_)->Error(Format.asprintf"failed to parse time (epoch): %S"s))))~construct:to_notation()letpp_humppft=Ptime.pp_rfc3339~frac_s()ppfttype'astamped={data:'a;stamp:Ptime.t}letstamped_encodingarg_encoding=letopenData_encodinginconv(fun{stamp;data}->(stamp,data))(fun(stamp,data)->{stamp;data})(tup2encodingarg_encoding)letpp_stampedppfmt{data;stamp}=Format.fprintffmt"%a(%a)"ppdatapp_humstampletstamp~timedata={data;stamp=time}letrecenta1a2=match(a1,a2)with|None,None->None|None,(Some_asa)|(Some_asa),None->a|Some(_,t1),Some(_,t2)->ift1<t2thena2elsea1lethasht=Int64.to_int(to_secondst)moduleSet=Set.Make(Ptime)moduleMap=Map.Make(Ptime)moduleTable=Hashtbl.Make(structincludePtimelethash=hashend)endlet()=Data_encoding.Registration.register~pp:Protocol.pp_humProtocol.encoding;Data_encoding.Registration.register~pp:System.pp_humSystem.encoding;Data_encoding.Registration.registerSystem.Span.encoding