1 /**************************************************************************/
2 /*                                                                        */
3 /*                                 OCaml                                  */
4 /*                                                                        */
5 /*                  File contributed by Josh Berdine                      */
6 /*                                                                        */
7 /*   Copyright 2011 Institut National de Recherche en Informatique et     */
8 /*     en Automatique.                                                    */
9 /*                                                                        */
10 /*   All rights reserved.  This file is distributed under the terms of    */
11 /*   the GNU Lesser General Public License version 2.1, with the          */
12 /*   special exception on linking described in the file LICENSE.          */
13 /*                                                                        */
14 /**************************************************************************/
15 
16 #include <caml/mlvalues.h>
17 #include <caml/alloc.h>
18 #include "unixsupport.h"
19 #include <windows.h>
20 
21 
to_sec(FILETIME ft)22 double to_sec(FILETIME ft) {
23 #if defined(_MSC_VER) && _MSC_VER < 1300
24   /* See gettimeofday.c - it is not possible for these values to be 64-bit, so
25      there's no worry about using a signed struct in order to work around the
26      lack of support for casting int64_t to double.
27    */
28   LARGE_INTEGER tmp;
29 #else
30   ULARGE_INTEGER tmp;
31 #endif
32 
33   tmp.u.LowPart = ft.dwLowDateTime;
34   tmp.u.HighPart = ft.dwHighDateTime;
35 
36   /* convert to seconds:
37      GetProcessTimes returns number of 100-nanosecond intervals */
38   return tmp.QuadPart / 1e7;
39 }
40 
41 
unix_times(value unit)42 value unix_times(value unit) {
43   value res;
44   FILETIME creation, exit, stime, utime;
45 
46   if (!(GetProcessTimes(GetCurrentProcess(), &creation, &exit, &stime,
47                         &utime))) {
48     win32_maperr(GetLastError());
49     uerror("times", Nothing);
50   }
51 
52   res = caml_alloc_small(4 * Double_wosize, Double_array_tag);
53   Store_double_field(res, 0, to_sec(utime));
54   Store_double_field(res, 1, to_sec(stime));
55   Store_double_field(res, 2, 0);
56   Store_double_field(res, 3, 0);
57   return res;
58 }
59