diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2025-07-04 01:28:36 +0200 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2025-07-04 20:24:26 +0200 |
commit | ce1a2b1b428e95d300af478fb0b36ca3773f7f84 (patch) | |
tree | 4779ff7dfbd9005a17124bef9f8d00476c11fe69 /src/core-commands.c | |
parent | 7bc7662f3cd1ceaf55e00f3d5f84e9772574afc8 (diff) | |
download | sciteco-ce1a2b1b428e95d300af478fb0b36ca3773f7f84.tar.gz |
implemented <^B> for returning the current date
* It is encoded with the same formula as on VAX/VMS on TECO-11, on TECOC and TECO-64.
* As an extension, when colon-modified it returns the number of seconds since the epoch.
It might be even more useful to return the microseconds since the epoch, but that would require
64-bit integers, which can theoretically be disabled at build time.
Diffstat (limited to 'src/core-commands.c')
-rw-r--r-- | src/core-commands.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/core-commands.c b/src/core-commands.c index b8918ef..819b1aa 100644 --- a/src/core-commands.c +++ b/src/core-commands.c @@ -18,6 +18,7 @@ #include "config.h" #endif +#include <time.h> #include <string.h> #include <glib.h> @@ -1541,6 +1542,45 @@ teco_ranges_cleanup(void) g_free(teco_ranges); } +/*$ ^B date time timestamp + * ^B -> (((year-1900)*16 + month)*32 + day) -- Retrieve date, time or timestamp + * :^B -> seconds + * ::^B -> timestamp + * + * By default returns the current date via the given equation. + * + * If colon-modified it returns the number of <seconds> since the Epoch, + * 1970-01-01 00:00:00 +0000 (UTC). + * + * If modified by two colons it returns the system's monotonic time in microseconds, + * which can be used as a <timestamp>. + */ +static void +teco_state_control_date(teco_machine_main_t *ctx, GError **error) +{ + GDate date; + + switch (teco_machine_main_eval_colon(ctx)) { + case 0: + g_date_clear(&date, 1); + g_date_set_time_t(&date, time(NULL)); + teco_expressions_push(((g_date_get_year(&date)-1900)*16 + g_date_get_month(&date))*32 + + g_date_get_day(&date)); + break; + case 1: + teco_expressions_push(time(NULL)); + break; + case 2: + /* + * NOTE: Might not be reliable if TECO_INTEGER==32. + */ + teco_expressions_push(g_get_monotonic_time()); + break; + default: + g_assert_not_reached(); + } +} + static teco_state_t * teco_state_control_input(teco_machine_main_t *ctx, gunichar chr, GError **error) { @@ -1567,6 +1607,8 @@ teco_state_control_input(teco_machine_main_t *ctx, gunichar chr, GError **error) /* * Commands */ + ['B'] = {&teco_state_start, teco_state_control_date, + .modifier_colon = 2}, ['O'] = {&teco_state_start, teco_state_control_octal}, ['D'] = {&teco_state_start, teco_state_control_decimal}, ['R'] = {&teco_state_start, teco_state_control_radix}, |