Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert time into seconds string issue

Tags:

ocaml

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

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 the next one:

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

As I understand it is about calling

Time.to_sec_string time

like image 792
senior_pimiento Avatar asked Dec 11 '15 08:12

senior_pimiento


1 Answers

In older version of 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 of time they changed its interface and now it is

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

That means, that now this is a function with two parameters. The first one is still a value of type Time.t, but the second is a labeled argument of type Zone.t that specifies a time zone. A labeled parameter, unlike a regular positional parameter, can be passed to a function at arbitrary position, so that you don't need to remember the order of arguments. A usual invokation will look like this:

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

where Time.Zone.local is a time zone object, that represents your local time zone. Since it is a labeled parameter, you can also call the function like this:

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

Moreover, since both parameters here have different types, OCaml can even guess who is who without using the label, so you can fallback to positional arguments:

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

Finally, most of types in Core has in there interface to_string function. So an easier fix would be to use it, instead of to_sec_string. It has a little bit different and more verbose format, though:

Time.to_string time ^ " -- " ^ message
like image 62
ivg Avatar answered Nov 20 '22 09:11

ivg