1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
/*
* tcl_clock.c
*
* Implements the clock command
*/
/* For strptime() */
#define _XOPEN_SOURCE
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include "jim.h"
#include "jim-subcmd.h"
static int clock_cmd_format(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
/* How big is big enough? */
char buf[100];
time_t t;
long seconds;
const char *format = "%a %b %d %H:%M:%S %Z %Y";
if (argc == 2 || (argc == 3 && !Jim_CompareStringImmediate(interp, argv[1], "-format"))) {
return -1;
}
if (argc == 3) {
format = Jim_GetString(argv[2], NULL);
}
if (Jim_GetLong(interp, argv[0], &seconds) != JIM_OK) {
return JIM_ERR;
}
t = seconds;
strftime(buf, sizeof(buf), format, localtime(&t));
Jim_SetResultString(interp, buf, -1);
return JIM_OK;
}
static int clock_cmd_scan(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
char *pt;
struct tm tm;
time_t now = time(0);
if (!Jim_CompareStringImmediate(interp, argv[1], "-format")) {
return -1;
}
/* Initialise with the current date/time */
tm = *localtime(&now);
pt = strptime(Jim_GetString(argv[0], NULL), Jim_GetString(argv[2], NULL), &tm);
if (pt == 0 || *pt != 0) {
Jim_SetResultString(interp, "Failed to parse time according to format", -1);
return JIM_ERR;
}
/* Now convert into a time_t */
Jim_SetResult(interp, Jim_NewIntObj(interp, (jim_wide)mktime(&tm)));
return JIM_OK;
}
static int clock_cmd_seconds(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
Jim_SetResult(interp, Jim_NewIntObj(interp, (jim_wide)time(NULL)));
return JIM_OK;
}
static const jim_subcmd_type command_table[] = {
{ .cmd = "seconds",
.function = clock_cmd_seconds,
.minargs = 0,
.maxargs = 0,
.description = "Returns the current time as seconds since the epoch"
},
{ .cmd = "format",
.args = "seconds ?-format format?",
.function = clock_cmd_format,
.minargs = 1,
.maxargs = 3,
.description = "Format the given time"
},
{ .cmd = "scan",
.args = "str -format format",
.function = clock_cmd_scan,
.minargs = 3,
.maxargs = 3,
.description = "Determine the time according to the given format"
},
{ 0 }
};
int Jim_clockInit(Jim_Interp *interp)
{
if (Jim_PackageProvide(interp, "clock", "1.0", JIM_ERRMSG) != JIM_OK) {
return JIM_ERR;
}
Jim_CreateCommand(interp, "clock", Jim_SubCmdProc, (void *)command_table, NULL);
return JIM_OK;
}
|