Add a general purpose hashtable pattern matcher
[jimtcl.git] / jim-clock.c
blobfe957b2841713dd54563f0c9f0801210a9ea46d5
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 "jim.h"
19 #include "jimautoconf.h"
20 #include "jim-subcmd.h"
22 #ifdef HAVE_SYS_TIME_H
23 #include <sys/time.h>
24 #endif
26 static int clock_cmd_format(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
28 /* How big is big enough? */
29 char buf[100];
30 time_t t;
31 long seconds;
33 const char *format = "%a %b %d %H:%M:%S %Z %Y";
35 if (argc == 2 || (argc == 3 && !Jim_CompareStringImmediate(interp, argv[1], "-format"))) {
36 return -1;
39 if (argc == 3) {
40 format = Jim_String(argv[2]);
43 if (Jim_GetLong(interp, argv[0], &seconds) != JIM_OK) {
44 return JIM_ERR;
46 t = seconds;
48 strftime(buf, sizeof(buf), format, localtime(&t));
50 Jim_SetResultString(interp, buf, -1);
52 return JIM_OK;
55 #ifdef HAVE_STRPTIME
56 static int clock_cmd_scan(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
58 char *pt;
59 struct tm tm;
60 time_t now = time(0);
62 if (!Jim_CompareStringImmediate(interp, argv[1], "-format")) {
63 return -1;
66 /* Initialise with the current date/time */
67 localtime_r(&now, &tm);
69 pt = strptime(Jim_String(argv[0]), Jim_String(argv[2]), &tm);
70 if (pt == 0 || *pt != 0) {
71 Jim_SetResultString(interp, "Failed to parse time according to format", -1);
72 return JIM_ERR;
75 /* Now convert into a time_t */
76 Jim_SetResultInt(interp, mktime(&tm));
78 return JIM_OK;
80 #endif
82 static int clock_cmd_seconds(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
84 Jim_SetResultInt(interp, time(NULL));
86 return JIM_OK;
89 static int clock_cmd_micros(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
91 struct timeval tv;
93 gettimeofday(&tv, NULL);
95 Jim_SetResultInt(interp, (jim_wide) tv.tv_sec * 1000000 + tv.tv_usec);
97 return JIM_OK;
100 static int clock_cmd_millis(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
102 struct timeval tv;
104 gettimeofday(&tv, NULL);
106 Jim_SetResultInt(interp, (jim_wide) tv.tv_sec * 1000 + tv.tv_usec / 1000);
108 return JIM_OK;
111 static const jim_subcmd_type clock_command_table[] = {
112 { "seconds",
113 NULL,
114 clock_cmd_seconds,
117 /* Description: Returns the current time as seconds since the epoch */
119 { "clicks",
120 NULL,
121 clock_cmd_micros,
124 /* Description: Returns the current time in 'clicks' */
126 { "microseconds",
127 NULL,
128 clock_cmd_micros,
131 /* Description: Returns the current time in microseconds */
133 { "milliseconds",
134 NULL,
135 clock_cmd_millis,
138 /* Description: Returns the current time in milliseconds */
140 { "format",
141 "seconds ?-format format?",
142 clock_cmd_format,
145 /* Description: Format the given time */
147 #ifdef HAVE_STRPTIME
148 { "scan",
149 "str -format format",
150 clock_cmd_scan,
153 /* Description: Determine the time according to the given format */
155 #endif
156 { NULL }
159 int Jim_clockInit(Jim_Interp *interp)
161 if (Jim_PackageProvide(interp, "clock", "1.0", JIM_ERRMSG))
162 return JIM_ERR;
164 Jim_CreateCommand(interp, "clock", Jim_SubCmdProc, (void *)clock_command_table, NULL);
165 return JIM_OK;