open Ctypes
open PosixTypes
open Foreign
type tm
let tm = structure "tm"
let (-:) ty label = field tm label ty
let tm_sec = int -: "tm_sec"
let tm_min = int -: "tm_min"
let tm_hour = int -: "tm_hour"
let tm_mday = int -: "tm_mday"
let tm_mon = int -: "tm_mon"
let tm_year = int -: "tm_year"
let tm_wday = int -: "tm_wday"
let tm_yday = int -: "tm_yday"
let tm_isdst = int -: "tm_isdst"
let () = seal (tm : tm structure typ)
let time = foreign "time" ~check_errno:true (ptr time_t @-> returning time_t)
let asctime = foreign "asctime" (ptr tm @-> returning string)
let localtime = foreign "localtime" (ptr time_t @-> returning (ptr tm))
let () = begin
let timep = allocate_n ~count:1 time_t in
let time = time timep in
assert (time = !@timep);
let tm = localtime timep in
Printf.printf "tm.tm_mon = %d\n" (getf !@tm tm_mon);
Printf.printf "tm.tm_year = %d\n" (getf !@tm tm_year);
print_endline (asctime tm)
end