aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/core-commands.c42
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},