Added some date/time basics. (0.19.0)
This commit is contained in:
parent
cefa1bf7e0
commit
63edf97aba
11 changed files with 284 additions and 1 deletions
39
src/vm/os/clock_mono_ms.c
Normal file
39
src/vm/os/clock_mono_ms.c
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-04
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file clock_mono_ms.c
|
||||
* @brief Implements OP_CLOCK_MONO_MS to push monotonic clock in ms.
|
||||
*
|
||||
* Stack before: []
|
||||
* Stack after: [int ms]
|
||||
*/
|
||||
|
||||
#include <time.h>
|
||||
#include <stdint.h>
|
||||
|
||||
case OP_CLOCK_MONO_MS: {
|
||||
int64_t ms;
|
||||
#if defined(CLOCK_MONOTONIC) && !defined(_WIN32)
|
||||
struct timespec ts;
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
|
||||
ms = (int64_t)ts.tv_sec * 1000 + (int64_t)(ts.tv_nsec / 1000000);
|
||||
} else {
|
||||
time_t s = time(NULL);
|
||||
ms = (int64_t)s * 1000;
|
||||
}
|
||||
#else
|
||||
time_t s = time(NULL);
|
||||
ms = (int64_t)s * 1000;
|
||||
#endif
|
||||
push_value(vm, make_int(ms));
|
||||
break;
|
||||
}
|
||||
72
src/vm/os/date_format.c
Normal file
72
src/vm/os/date_format.c
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-04
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file date_format.c
|
||||
* @brief Implements OP_DATE_FORMAT to format an epoch milliseconds timestamp using strftime.
|
||||
*
|
||||
* Behavior:
|
||||
* - Pops fmt string, then milliseconds since Unix epoch (int).
|
||||
* - Formats the local time according to fmt and pushes a string.
|
||||
*
|
||||
* Errors:
|
||||
* - If types are wrong, prints error and pushes empty string to keep stack safe.
|
||||
*/
|
||||
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
case OP_DATE_FORMAT: {
|
||||
Value fmt = pop_value(vm);
|
||||
Value ms = pop_value(vm);
|
||||
if (ms.type != VAL_INT || fmt.type != VAL_STRING) {
|
||||
fprintf(stderr, "DATE_FORMAT expects (fmt:string, ms:int)\n");
|
||||
if (ms.type != VAL_NIL) free_value(ms);
|
||||
if (fmt.type != VAL_NIL) free_value(fmt);
|
||||
push_value(vm, make_string(""));
|
||||
break;
|
||||
}
|
||||
time_t secs = (time_t)(ms.i / 1000);
|
||||
struct tm tmv;
|
||||
#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 1
|
||||
localtime_r(&secs, &tmv);
|
||||
#else
|
||||
struct tm *ptm = localtime(&secs);
|
||||
if (!ptm) {
|
||||
free_value(ms); free_value(fmt);
|
||||
push_value(vm, make_string(""));
|
||||
break;
|
||||
}
|
||||
tmv = *ptm;
|
||||
#endif
|
||||
/* Determine buffer size by attempting once with a reasonable size and retrying if needed */
|
||||
size_t cap = 128;
|
||||
char *buf = (char*)malloc(cap);
|
||||
size_t n = strftime(buf, cap, fmt.s, &tmv);
|
||||
if (n == 0) {
|
||||
/* try larger */
|
||||
cap = 512;
|
||||
buf = (char*)realloc(buf, cap);
|
||||
n = strftime(buf, cap, fmt.s, &tmv);
|
||||
}
|
||||
Value out;
|
||||
if (n == 0) {
|
||||
out = make_string("");
|
||||
} else {
|
||||
out = make_string(buf);
|
||||
}
|
||||
free(buf);
|
||||
free_value(ms);
|
||||
free_value(fmt);
|
||||
push_value(vm, out);
|
||||
break;
|
||||
}
|
||||
39
src/vm/os/time_now_ms.c
Normal file
39
src/vm/os/time_now_ms.c
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-04
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file time_now_ms.c
|
||||
* @brief Implements OP_TIME_NOW_MS to push current wall-clock time in ms since Unix epoch.
|
||||
*
|
||||
* Stack before: []
|
||||
* Stack after: [int ms]
|
||||
*/
|
||||
|
||||
#include <time.h>
|
||||
#include <stdint.h>
|
||||
|
||||
case OP_TIME_NOW_MS: {
|
||||
int64_t ms;
|
||||
#if defined(CLOCK_REALTIME) && !defined(_WIN32)
|
||||
struct timespec ts;
|
||||
if (clock_gettime(CLOCK_REALTIME, &ts) == 0) {
|
||||
ms = (int64_t)ts.tv_sec * 1000 + (int64_t)(ts.tv_nsec / 1000000);
|
||||
} else {
|
||||
time_t s = time(NULL);
|
||||
ms = (int64_t)s * 1000;
|
||||
}
|
||||
#else
|
||||
time_t s = time(NULL);
|
||||
ms = (int64_t)s * 1000;
|
||||
#endif
|
||||
push_value(vm, make_int(ms));
|
||||
break;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue