Skip to content

Commit

Permalink
[util] Add current_date_str() platform method
Browse files Browse the repository at this point in the history
Will be used for metadata on new presets.
  • Loading branch information
grandchild committed Nov 1, 2024
1 parent a0ed955 commit 7d15447
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
7 changes: 7 additions & 0 deletions avs/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ uint64_t timer_us();
*/
double timer_us_precision();

/**
* Return a string pointer with the current date in the format "YYYY-MM-DD" (11 bytes).
* The data is stored in a static buffer, so the return pointer is always the same, but
* its contents are not guaranteed to stay the same between calls to this function.
*/
const char* current_date_str();

/**
* Create a new lock object.
*/
Expand Down
15 changes: 14 additions & 1 deletion avs/platform_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <sys/mman.h> // mprotect flags
#include <sys/stat.h> // mkdir()

/* timers */
/* timers & time */

uint64_t timer_ms() {
struct timespec ts;
Expand All @@ -38,6 +38,19 @@ double timer_us_precision() {
return ts.tv_sec * 1000.0 * 1000.0 + (double)ts.tv_nsec / 1000.0;
}

const char* current_date_str() {
static char date_str[11];
time_t t = time(NULL);
struct tm date = *localtime(&t);
snprintf(date_str,
11,
"%04d-%02d-%02d",
date.tm_year + 1900,
date.tm_mon + 1,
date.tm_mday);
return date_str;
}

/* locking */

#define PTHREAD_LOCK(lock) ((pthread_mutex_t*)(lock))
Expand Down
10 changes: 9 additions & 1 deletion avs/platform_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <stdio.h>
#include <windowsx.h>

/* timers */
/* timers & time */

static int64_t timer_highres_ticks_per_us = 0;

Expand Down Expand Up @@ -36,6 +36,14 @@ double timer_us_precision() {
return 1.0f / (double)timer_highres_ticks_per_us;
}

const char* current_date_str() {
static char date_str[11];
SYSTEMTIME date;
GetLocalTime(&date);
snprintf(date_str, 11, "%04d-%02d-%02d", date.wYear, date.wMonth, date.wDay);
return date_str;
}

/* locking */

#define WIN_LOCK(lock) ((CRITICAL_SECTION*)(lock))
Expand Down

0 comments on commit 7d15447

Please sign in to comment.