123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960open!StdLabelsletsprintf=Printf.sprintf(* Use ptime for time conversions. This is error prone as we fiddle with the environment *)letparse_iso8601_stringstr=(*
Scanf.sscanf str "%d-%d-%dT%d:%d:%d.%s"
(fun year month day hour min sec _frac ->
match Ptime.of_date_time ((year, month, day), ((hour, min, sec), 0)) with
| None -> failwith "Illegal time format"
| Some t -> Ptime.to_float_s t)
*)Ptime.of_rfc3339str|>function|Ok(t,_,_)->Ptime.to_float_st|Error_->failwith"Could not parse date string"let%test_=parse_iso8601_string"2018-02-27T13:39:35.000Z"=1519738775.0letparse_rcf1123_stringdate_str=letint_of_month=function|"Jan"->1|"Feb"->2|"Mar"->3|"Apr"->4|"May"->5|"Jun"->6|"Jul"->7|"Aug"->8|"Sep"->9|"Oct"->10|"Nov"->11|"Dec"->12|_->failwith"Unknown month"inScanf.sscanfdate_str"%s %d %s %d %d:%d:%d %s"(fun_daynamedaymonth_stryearhourminsec_zone->letmonth=int_of_monthmonth_strinmatchPtime.of_date_time((year,month,day),((hour,min,sec),0))with|None->failwith"Illegal time format"|Somet->Ptime.to_float_st)let%test_=parse_rcf1123_string"Mon, 16 Jul 2018 10:31:41 GMT"=1531737101.0letiso8601_of_timetime=lett=Ptime.of_float_stime|>functionSomet->t|None->failwith"Time out of range"inlet(year,month,day),((hour,min,sec),_)=Ptime.to_date_timetinletdate_str=sprintf"%.4d%.2d%.2d"yearmonthdayinlettime_str=sprintf"%.2d%.2d%.2d"hourminsecin(date_str,time_str)let%test_=lett=1369353600.0in("20130524","000000")=iso8601_of_timet