From 2e6247013b62e7355e5051d413f9e4b3732b29e3 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Thu, 11 Jun 2020 18:24:55 +0100 Subject: [PATCH] No longer need to mention bigarray, begin to replace Unix --- Makefile | 2 +- cpdftime.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 cpdftime.c diff --git a/Makefile b/Makefile index 7dbd270..6d3da1b 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ MODS = tjutil tjutf16 tjllist tjparserMonad tjjson \ xmlm \ cpdfwriteJSON cpdfstrftime cpdfcoord cpdf cpdfcommand -SOURCES = $(foreach x,$(MODS),$(x).ml $(x).mli) cpdfcommandrun.ml +SOURCES = cpdftime.c $(foreach x,$(MODS),$(x).ml $(x).mli) cpdfcommandrun.ml RESULT = cpdf ANNOTATE = true diff --git a/cpdftime.c b/cpdftime.c new file mode 100644 index 0000000..7f827a7 --- /dev/null +++ b/cpdftime.c @@ -0,0 +1,58 @@ +/**************************************************************************/ +/* */ +/* OCaml */ +/* */ +/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */ +/* */ +/* Copyright 1996 Institut National de Recherche en Informatique et */ +/* en Automatique. */ +/* */ +/* All rights reserved. This file is distributed under the terms of */ +/* the GNU Lesser General Public License version 2.1, with the */ +/* special exception on linking described in the file LICENSE. */ +/* */ +/**************************************************************************/ + +#include +#include +#include +#include +#include + +double cpdf_unix_gettimeofday_unboxed(value unit) +{ + struct timeval tp; + gettimeofday(&tp, NULL); + return ((double) tp.tv_sec + (double) tp.tv_usec / 1e6); +} + +CAMLprim value cpdf_unix_gettimeofday(value unit) +{ + return caml_copy_double(cpdf_unix_gettimeofday_unboxed(unit)); +} + +static value cpdf_alloc_tm(struct tm *tm) +{ + value res; + res = caml_alloc_small(9, 0); + Field(res,0) = Val_int(tm->tm_sec); + Field(res,1) = Val_int(tm->tm_min); + Field(res,2) = Val_int(tm->tm_hour); + Field(res,3) = Val_int(tm->tm_mday); + Field(res,4) = Val_int(tm->tm_mon); + Field(res,5) = Val_int(tm->tm_year); + Field(res,6) = Val_int(tm->tm_wday); + Field(res,7) = Val_int(tm->tm_yday); + Field(res,8) = tm->tm_isdst ? Val_true : Val_false; + return res; +} + +CAMLprim value cpdf_unix_localtime(value t) +{ + time_t clock; + struct tm * tm; + clock = (time_t) Double_val(t); + tm = localtime(&clock); + return cpdf_alloc_tm(tm); +} +