Time in Standard ML

(* Standard ML offers support for times and durations; here are some examples. *)

(* We'll start by getting the current time. *)
val now = Time.now()
val _ = print (Time.toString now ^ "\n")

(* You can build a time value by providing the year, month, day, etc. 
   Times in Standard ML are typically represented as seconds since the epoch. *)
val then = Time.fromReal (Time.toReal (Time.fromTimeUniv (Date.date {
    year = 2009, month = Date.Nov, day = 17, 
    hour = 20, minute = 34, second = 58
})))
val _ = print (Time.toString then ^ "\n")

(* You can extract various components of the time value. *)
val date = Date.fromTimeUniv (Time.toTimeUniv then)
val _ = print (Int.toString (Date.year date) ^ "\n")
val _ = print (Date.monthName date ^ "\n")
val _ = print (Int.toString (Date.day date) ^ "\n")
val _ = print (Int.toString (Date.hour date) ^ "\n")
val _ = print (Int.toString (Date.minute date) ^ "\n")
val _ = print (Int.toString (Date.second date) ^ "\n")

(* The day of the week is also available. *)
val _ = print (Date.weekDay date ^ "\n")

(* These functions compare two times, testing if the first occurs before, 
   after, or at the same time as the second, respectively. *)
val _ = print (Bool.toString (Time.< (then, now)) ^ "\n")
val _ = print (Bool.toString (Time.> (then, now)) ^ "\n")
val _ = print (Bool.toString (Time.= (then, now)) ^ "\n")

(* The '-' operator returns a time span representing the interval between two times. *)
val diff = Time.- (now, then)
val _ = print (Time.toString diff ^ "\n")

(* We can compute the length of the duration in various units. *)
val _ = print (Real.toString (Time.toReal diff / 3600.0) ^ "\n")  (* Hours *)
val _ = print (Real.toString (Time.toReal diff / 60.0) ^ "\n")    (* Minutes *)
val _ = print (Real.toString (Time.toReal diff) ^ "\n")           (* Seconds *)

(* You can use '+' to advance a time by a given duration, 
   or '-' to move backwards by a duration. *)
val _ = print (Time.toString (Time.+ (then, diff)) ^ "\n")
val _ = print (Time.toString (Time.- (then, diff)) ^ "\n")

This Standard ML code demonstrates various operations with time and duration, similar to the original Go example. Here are some key differences and explanations:

  1. Standard ML uses the Time and Date modules for time-related operations.

  2. Times are typically represented as seconds since the epoch in Standard ML.

  3. Creating a specific date and time is done using the Date.date function and then converting it to a Time.time value.

  4. Extracting components of a time value requires converting it to a Date.date value first.

  5. Standard ML doesn’t have built-in nanosecond precision, so we omit those operations.

  6. Duration calculations are done using the Time.- function, which returns a Time.time value representing the duration.

  7. To get duration in different units, we convert the time difference to real numbers and perform the necessary calculations.

  8. The Time.+ and Time.- functions are used to add or subtract durations from times.

This code provides a similar functionality to the Go example, adapted to Standard ML’s syntax and standard library functions.