load: use AssocData to free load handles
[jimtcl.git] / jim-clock.c
blobf79110603c15d3a95a4d3c3d875b0a4fc7281da8
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 strftime(buf, sizeof(buf), format, localtime(&t));
49 Jim_SetResultString(interp, buf, -1);
51 return JIM_OK;
54 #ifdef HAVE_STRPTIME
55 static int clock_cmd_scan(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
57 char *pt;
58 struct tm tm;
59 time_t now = time(0);
61 if (!Jim_CompareStringImmediate(interp, argv[1], "-format")) {
62 return -1;
65 /* Initialise with the current date/time */
66 localtime_r(&now, &tm);
68 pt = strptime(Jim_String(argv[0]), Jim_String(argv[2]), &tm);
69 if (pt == 0 || *pt != 0) {
70 Jim_SetResultString(interp, "Failed to parse time according to format", -1);
71 return JIM_ERR;
74 /* Now convert into a time_t */
75 Jim_SetResultInt(interp, mktime(&tm));
77 return JIM_OK;
79 #endif
81 static int clock_cmd_seconds(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
83 Jim_SetResultInt(interp, time(NULL));
85 return JIM_OK;
88 static int clock_cmd_micros(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
90 struct timeval tv;
92 gettimeofday(&tv, NULL);
94 Jim_SetResultInt(interp, (jim_wide) tv.tv_sec * 1000000 + tv.tv_usec);
96 return JIM_OK;
99 static int clock_cmd_millis(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
101 struct timeval tv;
103 gettimeofday(&tv, NULL);
105 Jim_SetResultInt(interp, (jim_wide) tv.tv_sec * 1000 + tv.tv_usec / 1000);
107 return JIM_OK;
110 static const jim_subcmd_type clock_command_table[] = {
111 { "seconds",
112 NULL,
113 clock_cmd_seconds,
116 /* Description: Returns the current time as seconds since the epoch */
118 { "clicks",
119 NULL,
120 clock_cmd_micros,
123 /* Description: Returns the current time in 'clicks' */
125 { "microseconds",
126 NULL,
127 clock_cmd_micros,
130 /* Description: Returns the current time in microseconds */
132 { "milliseconds",
133 NULL,
134 clock_cmd_millis,
137 /* Description: Returns the current time in milliseconds */
139 { "format",
140 "seconds ?-format format?",
141 clock_cmd_format,
144 /* Description: Format the given time */
146 #ifdef HAVE_STRPTIME
147 { "scan",
148 "str -format format",
149 clock_cmd_scan,
152 /* Description: Determine the time according to the given format */
154 #endif
155 { NULL }
158 int Jim_clockInit(Jim_Interp *interp)
160 if (Jim_PackageProvide(interp, "clock", "1.0", JIM_ERRMSG))
161 return JIM_ERR;
163 Jim_CreateCommand(interp, "clock", Jim_SubCmdProc, (void *)clock_command_table, NULL);
164 return JIM_OK;