package: simplification/code cleanup
[jimtcl.git] / jim-clock.c
blob97f73b4b24a60afe90469a954205cfbe3c7e7053
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>
18 #include "jimautoconf.h"
19 #include <jim-subcmd.h>
21 #ifdef HAVE_SYS_TIME_H
22 #include <sys/time.h>
23 #endif
25 static int clock_cmd_format(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
27 /* How big is big enough? */
28 char buf[100];
29 time_t t;
30 long seconds;
32 const char *format = "%a %b %d %H:%M:%S %Z %Y";
34 if (argc == 2 || (argc == 3 && !Jim_CompareStringImmediate(interp, argv[1], "-format"))) {
35 return -1;
38 if (argc == 3) {
39 format = Jim_String(argv[2]);
42 if (Jim_GetLong(interp, argv[0], &seconds) != JIM_OK) {
43 return JIM_ERR;
45 t = seconds;
47 if (strftime(buf, sizeof(buf), format, localtime(&t)) == 0) {
48 Jim_SetResultString(interp, "format string too long", -1);
49 return JIM_ERR;
52 Jim_SetResultString(interp, buf, -1);
54 return JIM_OK;
57 #ifdef HAVE_STRPTIME
58 static int clock_cmd_scan(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
60 char *pt;
61 struct tm tm;
62 time_t now = time(0);
64 if (!Jim_CompareStringImmediate(interp, argv[1], "-format")) {
65 return -1;
68 /* Initialise with the current date/time */
69 localtime_r(&now, &tm);
71 pt = strptime(Jim_String(argv[0]), Jim_String(argv[2]), &tm);
72 if (pt == 0 || *pt != 0) {
73 Jim_SetResultString(interp, "Failed to parse time according to format", -1);
74 return JIM_ERR;
77 /* Now convert into a time_t */
78 Jim_SetResultInt(interp, mktime(&tm));
80 return JIM_OK;
82 #endif
84 static int clock_cmd_seconds(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
86 Jim_SetResultInt(interp, time(NULL));
88 return JIM_OK;
91 static int clock_cmd_micros(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
93 struct timeval tv;
95 gettimeofday(&tv, NULL);
97 Jim_SetResultInt(interp, (jim_wide) tv.tv_sec * 1000000 + tv.tv_usec);
99 return JIM_OK;
102 static int clock_cmd_millis(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
104 struct timeval tv;
106 gettimeofday(&tv, NULL);
108 Jim_SetResultInt(interp, (jim_wide) tv.tv_sec * 1000 + tv.tv_usec / 1000);
110 return JIM_OK;
113 static const jim_subcmd_type clock_command_table[] = {
114 { "seconds",
115 NULL,
116 clock_cmd_seconds,
119 /* Description: Returns the current time as seconds since the epoch */
121 { "clicks",
122 NULL,
123 clock_cmd_micros,
126 /* Description: Returns the current time in 'clicks' */
128 { "microseconds",
129 NULL,
130 clock_cmd_micros,
133 /* Description: Returns the current time in microseconds */
135 { "milliseconds",
136 NULL,
137 clock_cmd_millis,
140 /* Description: Returns the current time in milliseconds */
142 { "format",
143 "seconds ?-format format?",
144 clock_cmd_format,
147 /* Description: Format the given time */
149 #ifdef HAVE_STRPTIME
150 { "scan",
151 "str -format format",
152 clock_cmd_scan,
155 /* Description: Determine the time according to the given format */
157 #endif
158 { NULL }
161 int Jim_clockInit(Jim_Interp *interp)
163 if (Jim_PackageProvide(interp, "clock", "1.0", JIM_ERRMSG))
164 return JIM_ERR;
166 Jim_CreateCommand(interp, "clock", Jim_SubCmdProc, (void *)clock_command_table, NULL);
167 return JIM_OK;