Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file date.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153(*********************************************************************************)(* Stog *)(* *)(* Copyright (C) 2012-2015 INRIA All rights reserved. *)(* Author: Maxence Guesdon, INRIA Saclay *)(* *)(* This program is free software; you can redistribute it and/or modify *)(* it under the terms of the GNU General Public License as *)(* published by the Free Software Foundation, version 3 of the License. *)(* *)(* This program 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 PURPOSE. See the *)(* GNU General Public License for more details. *)(* *)(* You should have received a copy of the GNU General Public *)(* License along with this program; if not, write to the Free Software *)(* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA *)(* 02111-1307 USA *)(* *)(* As a special exception, you have permission to link this program *)(* with the OCaml compiler and distribute executables, as long as you *)(* follow the requirements of the GNU GPL in regard to all of the *)(* software in the executable aside from the OCaml compiler. *)(* *)(* Contact: Maxence.Guesdon@inria.fr *)(* *)(*********************************************************************************)(** *)typet={stamp:Ptime.t;tz:Ptime.tz_offset_soption;}letof_string?locstr=matchPtime.of_rfc3339strwithOk(stamp,tz,_)->{stamp;tz}|Error(`RFC3339((p1,p2),e))->letb=Buffer.create256inletfmt=Format.formatter_of_bufferbinifp2>p1thenFormat.fprintffmt"Characters %d-%d: "p1p2elseFormat.fprintffmt"Character %d: "p1;Ptime.pp_rfc3339_errorfmte;Format.pp_print_flushfmt();leterr=Buffer.contentsbinError.invalid_date?locstrerrletof_string_date?locstr=tryof_string?loc(str^"T00:00:00Z")withe->(* backward compatibility handling of YYYY/MM/DD *)tryScanf.sscanfstr"%d/%d/%d"(funymd->of_string?loc(Printf.sprintf"%04d-%02d-%02dT00:00:00Z"ymd))with_->raiseeletto_stringt=Ptime.to_rfc3339?tz_offset_s:t.tzt.stampletof_floatt=matchPtime.of_float_stwith|Somestamp->{stamp;tz=None}|None->failwith(Printf.sprintf"Could not create date from %f"t)letnow()=of_float(Unix.time())letto_date_timet=Ptime.to_date_time?tz_offset_s:t.tzt.stampletweekdayt=Ptime.weekday?tz_offset_s:t.tzt.stamp(* FIXME: give tz_offset_s parameter when weekday accepts it *)letto_rfc_822t=letwd=matchweekdaytwith|`Sun->"Sun"|`Mon->"Mon"|`Tue->"Tue"|`Wed->"Wed"|`Thu->"Thu"|`Fri->"Fri"|`Sat->"Sat"inlet((y,m,d),((h,mi,s),tz))=to_date_timetinletmon=matchmwith1->"Jan"|2->"Feb"|3->"Mar"|4->"Apr"|5->"May"|6->"Jun"|7->"Jul"|8->"Aug"|9->"Sep"|10->"Oct"|11->"Nov"|_->"Dec"inletabs_tz=abstzinPrintf.sprintf"%s, %02d %s %04d %02d:%02d:%02d %c%02d%02d"wddmonyhmis(iftz<0then'-'else'+')(abs_tzmod3600)(abs_tzmod60)letcp_percent=Uchar.of_char'%'letformattfmt=matchfmtwith"rfc3339"->to_stringt|"rfc822"->to_rfc_822t|_->letb=Buffer.create256inlet((y,m,d),((h,mi,s),tz))=to_date_timetinletfprev_cpi=function`Malformedstr->beginmatchprev_cpwithNone->()|Someprev_cp->Uutf.Buffer.add_utf_8bprev_cpend;Buffer.add_stringbstr;None|`Ucharcp->matchprev_cpwith|Someprev_cpwhenUchar.equalprev_cpcp_percent->let()=matchUchar.to_intcpwith|89(* Y *)->Buffer.add_stringb(string_of_inty)|77(* M *)->Printf.bprintfb"%02d"m|68(* D *)->Printf.bprintfb"%02d"d|104(* h *)->Printf.bprintfb"%02d"h|109(* m *)->Printf.bprintfb"%02d"mi|115(* s *)->Printf.bprintfb"%02d"s|_whenUchar.equalcpcp_percent->Buffer.add_charb'%'|_->Uutf.Buffer.add_utf_8bprev_cp;Uutf.Buffer.add_utf_8bcpinNone|Someprev_cp->Uutf.Buffer.add_utf_8bprev_cp;Somecp|None->Somecpin(matchUutf.String.fold_utf_8fNonefmtwithNone->()|Someremain->Uutf.Buffer.add_utf_8bremain);Buffer.contentsb