The libc routine gettimeofday returns the current calendar time as the elapsed time since the epoch. It takes pointers to two structures as arguments and fill those structures with data.
First libc needs to be loaded:
w> libc: load/library %/usr/lib/libc.dylib
Then the structures and routine are defined:
timeval: struct [
slong sec
sint32 usec
] none
timezone: struct [
sint minuteswest
sint dsttime
] none
gettimeofday: routine [
[typecheck]
libc "gettimeofday" [
tp [struct!] pointer
tzp [struct!] pointer
]
sint
]
gettimeofday can now be called:
w> gettimeofday timeval timezone
== 0
timeval and timezone then holds the data. The routine localtime_r takes time and a struct tm as arguments and fill the structure with data:
tm: struct [
sint sec
sint min
sint hour
sint mday
sint mon
sint year
sint wday
sint yday
sint isdst
slong gmtoff
pointer zone
] none
localtime-r: routine [
[typecheck]
libc "localtime_r" [
time [struct!] pointer
resultp [struct!] pointer
]
pointer handle!
]
The time argument is a pointer to a slong holding the seconds. But that's the first variable in the timeval structure, so we can just pass that:
w> localtime-r timeval tm
Now tm is filled with data, and we can e.g. get the day of the year:
w> tm/yday
== 353
There is some freedom in defining structures. C datatype and argument name can be exchanged. It's also possible to give initial values, so these two are the same:
struct [float f] [1.0]
struct [f float] [1.0]
Now find some more interesting libraries to integrate with!