5 * Implements the clock command
17 #include "jim-subcmd.h"
19 static int clock_cmd_format(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
21 /* How big is big enough? */
26 const char *format
= "%a %b %d %H:%M:%S %Z %Y";
28 if (argc
== 2 || (argc
== 3 && !Jim_CompareStringImmediate(interp
, argv
[1], "-format"))) {
33 format
= Jim_GetString(argv
[2], NULL
);
36 if (Jim_GetLong(interp
, argv
[0], &seconds
) != JIM_OK
) {
41 strftime(buf
, sizeof(buf
), format
, localtime(&t
));
43 Jim_SetResultString(interp
, buf
, -1);
49 static int clock_cmd_scan(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
55 if (!Jim_CompareStringImmediate(interp
, argv
[1], "-format")) {
59 /* Initialise with the current date/time */
60 tm
= *localtime(&now
);
62 pt
= strptime(Jim_GetString(argv
[0], NULL
), Jim_GetString(argv
[2], NULL
), &tm
);
63 if (pt
== 0 || *pt
!= 0) {
64 Jim_SetResultString(interp
, "Failed to parse time according to format", -1);
68 /* Now convert into a time_t */
69 Jim_SetResultInt(interp
, mktime(&tm
));
75 static int clock_cmd_seconds(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
77 Jim_SetResultInt(interp
, time(NULL
));
82 static const jim_subcmd_type clock_command_table
[] = {
84 .function
= clock_cmd_seconds
,
87 .description
= "Returns the current time as seconds since the epoch"
90 .args
= "seconds ?-format format?",
91 .function
= clock_cmd_format
,
94 .description
= "Format the given time"
98 .args
= "str -format format",
99 .function
= clock_cmd_scan
,
102 .description
= "Determine the time according to the given format"
108 int Jim_clockInit(Jim_Interp
*interp
)
110 Jim_CreateCommand(interp
, "clock", Jim_SubCmdProc
, (void *)clock_command_table
, NULL
);