Import Upstream version 20180207
[hcoop/debian/mlton.git] / lib / mlton / basic / time.sml
CommitLineData
7f918cf1
CE
1(* Copyright (C) 2009 Matthew Fluet.
2 * Copyright (C) 1999-2006 Henry Cejtin, Matthew Fluet, Suresh
3 * Jagannathan, and Stephen Weeks.
4 *
5 * MLton is released under a BSD-style license.
6 * See the file MLton-LICENSE for details.
7 *)
8
9structure Time: TIME =
10struct
11
12open Pervasive.LargeInt
13open Pervasive.Time
14structure LargeInt = Pervasive.LargeInt
15
16type t = time
17
18type times =
19 {
20 self: {utime: t, stime: t},
21 children: {utime: t, stime: t},
22 gc: {utime: t, stime: t}
23 }
24
25fun times (): times =
26 let
27 val {self, children, gc} = MLton.Rusage.rusage ()
28 fun doit ({utime, stime, ...} : MLton.Rusage.t)
29 = {utime = utime, stime = stime}
30 in
31 {self = doit self,
32 children = doit children,
33 gc = doit gc}
34 end
35
36val zero = fromReal 0.0
37
38val equals = op =
39
40val seconds = fromSeconds
41
42fun minutes m = seconds (m * fromInt 60)
43
44fun hours h = minutes (h * fromInt 60)
45
46fun days d = hours (d * LargeInt.fromInt 24)
47
48fun weeks w = days (w * LargeInt.fromInt 7)
49
50fun years y = days (y * LargeInt.fromInt 365)
51
52val {min, max, ...} = Relation.compare compare
53
54val layout = Layout.str o toString
55
56fun output (t, out) = Out.output (out, toString t)
57
58fun timeThunk (th: unit -> unit): t =
59 let
60 val {self = {utime, stime}, ...} = times ()
61 val t = utime + stime
62 val _ = th ()
63 val {self = {utime, stime}, ...} = times ()
64 val t' = utime + stime
65 in t' - t
66 end
67
68end