aboutsummaryrefslogtreecommitdiff
path: root/jim-clock.c
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2009-07-27 11:04:16 +1000
committerSteve Bennett <steveb@workware.net.au>2010-10-15 10:11:01 +1000
commit1bb6166bdb5e2a92b50e0ea178fa55a5f0d7261f (patch)
tree239853a0680ef9126249c450b4b7cf7ead7c84e3 /jim-clock.c
parent050dc9d1556f69ddaaa156682715ee06ea1ff6fa (diff)
downloadjimtcl-1bb6166bdb5e2a92b50e0ea178fa55a5f0d7261f.zip
jimtcl-1bb6166bdb5e2a92b50e0ea178fa55a5f0d7261f.tar.gz
jimtcl-1bb6166bdb5e2a92b50e0ea178fa55a5f0d7261f.tar.bz2
Add clock command
Diffstat (limited to 'jim-clock.c')
-rw-r--r--jim-clock.c110
1 files changed, 110 insertions, 0 deletions
diff --git a/jim-clock.c b/jim-clock.c
new file mode 100644
index 0000000..fb3c433
--- /dev/null
+++ b/jim-clock.c
@@ -0,0 +1,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;
+}