Time in OCaml

Our first example demonstrates how to work with time and durations in OCaml. Here’s the full source code:

open Unix

let p = Printf.printf "%s\n"

let () =
  (* We'll start by getting the current time *)
  let now = Unix.gettimeofday () in
  p (string_of_float now);

  (* You can build a time value by providing the
     year, month, day, etc. Times are always associated
     with a time zone. *)
  let then_time = Unix.mktime {
    tm_year = 2009 - 1900;
    tm_mon = 11 - 1;
    tm_mday = 17;
    tm_hour = 20;
    tm_min = 34;
    tm_sec = 58;
    tm_wday = 0;
    tm_yday = 0;
    tm_isdst = false;
  } in
  p (string_of_float (fst then_time));

  (* You can extract the various components of the time
     value as expected. *)
  let tm = Unix.localtime (fst then_time) in
  p (string_of_int (tm.tm_year + 1900));
  p (string_of_int (tm.tm_mon + 1));
  p (string_of_int tm.tm_mday);
  p (string_of_int tm.tm_hour);
  p (string_of_int tm.tm_min);
  p (string_of_int tm.tm_sec);

  (* The day of the week is also available. *)
  p (string_of_int tm.tm_wday);

  (* These functions compare two times, testing if the
     first occurs before, after, or at the same time
     as the second, respectively. *)
  p (string_of_bool (fst then_time < now));
  p (string_of_bool (fst then_time > now));
  p (string_of_bool (fst then_time = now));

  (* We can compute the difference between two times. *)
  let diff = now -. fst then_time in
  p (string_of_float diff);

  (* We can compute the length of the duration in
     various units. *)
  p (string_of_float (diff /. 3600.)); (* Hours *)
  p (string_of_float (diff /. 60.));   (* Minutes *)
  p (string_of_float diff);            (* Seconds *)

  (* You can use addition to advance a time by a given
     duration, or subtraction to move backwards. *)
  p (string_of_float (fst then_time +. diff));
  p (string_of_float (fst then_time -. diff))

To run the program, save it as time_example.ml and use ocamlc to compile it:

$ ocamlc unix.cma time_example.ml -o time_example
$ ./time_example

This will output the current time, a specific time in 2009, various components of that time, comparisons between times, and durations in different units.

Note that OCaml’s standard library doesn’t provide as extensive time manipulation capabilities as Go’s time package. For more advanced time operations, you might want to consider using a third-party library like calendar or ptime.

In this example, we’ve used OCaml’s Unix module for time operations. The Unix.gettimeofday() function returns the current time as a float representing seconds since the Unix epoch. The Unix.mktime function is used to create a time from individual components.

OCaml doesn’t have built-in types for durations, so we’ve represented durations as float values representing seconds. We’ve manually converted these to hours and minutes in the example.

This OCaml code provides similar functionality to the original Go code, demonstrating time manipulation, comparison, and duration calculations in OCaml.