Convert time to string string

I am reading a Real World OCaml book and got an error with the code from this book. Since I don't see any activity on book issues on GitHub, I would like to ask you here. Here is the cobe problem:

let log_entry maybe_time message = let time = match maybe_time with | Some x -> x | None -> Time.now () in Time.to_sec_string time ^ " -- " ^ message ;; 

And the error is as follows:

 Error: This expression has type zone:Core.Zone.t -> string but an expression was expected of type string 

As I understand it, this is a challenge

Time time.to_sec_string

+6
source share
2 answers

In the old version of the Core library, the to_sec_string function had the following interface:

 (** [to_sec_string t] Same as to_string, but without milliseconds *) val to_sec_string : t -> string 

At some point in time, they changed their interface, and now it

 (** Same as [to_string_abs], but without milliseconds *) val to_sec_string : t -> zone:Zone.t -> string 

This means that now it is a function with two parameters. The first is still a value of type Time.t , and the second is a tagged argument of type Zone.t , which indicates the time zone. The labeled parameter, unlike the usual positional parameter, can be passed to the function in an arbitrary position, so you do not need to remember the order of the arguments. A typical call would look like this:

 let s = Time.to_sec_string time ~zone:Time.Zone.local 

where Time.Zone.local is the timezone object that represents your local timezone. Since this is a designated parameter, you can also call the function as follows:

 let s = Time.to_sec_string ~zone:Time.Zone.local time 

In addition, since both parameters are of different types here, OCaml can even guess who is who without using a label, so you can refuse positional arguments:

 let s = Time.to_sec_string time Time.Zone.local 

Finally, most types in Core have a to_string interface. Therefore, it would be easier to use it instead of to_sec_string . However, it has a slightly different and more detailed format:

 Time.to_string time ^ " -- " ^ message 
+9
source

I found a discussion of this issue on GitHub

The solution is to add:

 Time.to_sec_string ~zone:Core.Zone.local (time) ^ " -- " ^ message 

Although I do not understand how this works.

+5
source

All Articles