usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / usbmodeswitch / jim / jim-clock.c
blob51ffb511bdb5b7f3fea3d7ba8b282fe3c146e0ab
2 /*
3 * tcl_clock.c
5 * Implements the clock command
6 */
8 /* For strptime() */
9 #ifndef _XOPEN_SOURCE
10 #define _XOPEN_SOURCE 500
11 #endif
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdio.h>
16 #include <time.h>
17 #include <sys/time.h>
19 #include "jim.h"
20 #include "jimautoconf.h"
21 #include "jim-subcmd.h"
23 static int clock_cmd_format(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
25 /* How big is big enough? */
26 char buf[100];
27 time_t t;
28 long seconds;
30 const char *format = "%a %b %d %H:%M:%S %Z %Y";
32 if (argc == 2 || (argc == 3 && !Jim_CompareStringImmediate(interp, argv[1], "-format"))) {
33 return -1;
36 if (argc == 3) {
37 format = Jim_String(argv[2]);
40 if (Jim_GetLong(interp, argv[0], &seconds) != JIM_OK) {
41 return JIM_ERR;
43 t = seconds;
45 strftime(buf, sizeof(buf), format, localtime(&t));
47 Jim_SetResultString(interp, buf, -1);
49 return JIM_OK;
52 #ifdef HAVE_STRPTIME
53 static int clock_cmd_scan(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
55 char *pt;
56 struct tm tm;
57 time_t now = time(0);
59 if (!Jim_CompareStringImmediate(interp, argv[1], "-format")) {
60 return -1;
63 /* Initialise with the current date/time */
64 localtime_r(&now, &tm);
66 pt = strptime(Jim_String(argv[0]), Jim_String(argv[2]), &tm);
67 if (pt == 0 || *pt != 0) {
68 Jim_SetResultString(interp, "Failed to parse time according to format", -1);
69 return JIM_ERR;
72 /* Now convert into a time_t */
73 Jim_SetResultInt(interp, mktime(&tm));
75 return JIM_OK;
77 #endif
79 static int clock_cmd_seconds(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
81 Jim_SetResultInt(interp, time(NULL));
83 return JIM_OK;
86 static int clock_cmd_micros(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
88 struct timeval tv;
90 gettimeofday(&tv, NULL);
92 Jim_SetResultInt(interp, (jim_wide) tv.tv_sec * 1000000 + tv.tv_usec);
94 return JIM_OK;
97 static int clock_cmd_millis(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
99 struct timeval tv;
101 gettimeofday(&tv, NULL);
103 Jim_SetResultInt(interp, (jim_wide) tv.tv_sec * 1000 + tv.tv_usec / 1000);
105 return JIM_OK;
108 static const jim_subcmd_type clock_command_table[] = {
109 { .cmd = "seconds",
110 .function = clock_cmd_seconds,
111 .minargs = 0,
112 .maxargs = 0,
113 .description = "Returns the current time as seconds since the epoch"
115 { .cmd = "clicks",
116 .function = clock_cmd_micros,
117 .minargs = 0,
118 .maxargs = 0,
119 .description = "Returns the current time in 'clicks'"
121 { .cmd = "microseconds",
122 .function = clock_cmd_micros,
123 .minargs = 0,
124 .maxargs = 0,
125 .description = "Returns the current time in microseconds"
127 { .cmd = "milliseconds",
128 .function = clock_cmd_millis,
129 .minargs = 0,
130 .maxargs = 0,
131 .description = "Returns the current time in milliseconds"
133 { .cmd = "format",
134 .args = "seconds ?-format format?",
135 .function = clock_cmd_format,
136 .minargs = 1,
137 .maxargs = 3,
138 .description = "Format the given time"
140 #ifdef HAVE_STRPTIME
141 { .cmd = "scan",
142 .args = "str -format format",
143 .function = clock_cmd_scan,
144 .minargs = 3,
145 .maxargs = 3,
146 .description = "Determine the time according to the given format"
148 #endif
149 { 0 }
152 int Jim_clockInit(Jim_Interp *interp)
154 if (Jim_PackageProvide(interp, "clock", "1.0", JIM_ERRMSG))
155 return JIM_ERR;
157 Jim_CreateCommand(interp, "clock", Jim_SubCmdProc, (void *)clock_command_table, NULL);
158 return JIM_OK;