Add epoch
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
# Copyright (c) 2026 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Turn things off we don't need
|
||||
set(JERRY_CMDLINE OFF CACHE BOOL "" FORCE)
|
||||
set(JERRY_EXT ON CACHE BOOL "" FORCE)
|
||||
set(JERRY_DEBUGGER OFF CACHE BOOL "" FORCE)
|
||||
set(JERRY_BUILTIN_DATE OFF CACHE BOOL "" FORCE)
|
||||
|
||||
# Fetch Jerry
|
||||
include(FetchContent)
|
||||
FetchContent_Declare(
|
||||
jerryscript
|
||||
GIT_REPOSITORY https://github.com/jerryscript-project/jerryscript.git
|
||||
GIT_TAG v3.0.0
|
||||
)
|
||||
FetchContent_MakeAvailable(jerryscript)
|
||||
|
||||
# Mark found
|
||||
set(jerryscript_FOUND ON)
|
||||
|
||||
# Define targets
|
||||
if(TARGET jerryscript-core)
|
||||
set(JERRY_CORE_TARGET jerryscript-core)
|
||||
elseif(TARGET jerry-core)
|
||||
set(JERRY_CORE_TARGET jerry-core)
|
||||
endif()
|
||||
|
||||
if(TARGET jerryscript-ext)
|
||||
set(JERRY_EXT_TARGET jerryscript-ext)
|
||||
elseif(TARGET jerry-ext)
|
||||
set(JERRY_EXT_TARGET jerry-ext)
|
||||
endif()
|
||||
|
||||
if(TARGET jerryscript-port)
|
||||
set(JERRY_PORT_TARGET jerryscript-port)
|
||||
elseif(TARGET jerry-port)
|
||||
set(JERRY_PORT_TARGET jerry-port)
|
||||
endif()
|
||||
|
||||
# Export include dirs through the targets
|
||||
target_include_directories(${JERRY_CORE_TARGET} INTERFACE
|
||||
${jerryscript_SOURCE_DIR}/jerry-core/include
|
||||
)
|
||||
|
||||
target_include_directories(${JERRY_EXT_TARGET} INTERFACE
|
||||
${jerryscript_SOURCE_DIR}/jerry-ext/include
|
||||
)
|
||||
|
||||
target_include_directories(${JERRY_PORT_TARGET} INTERFACE
|
||||
${jerryscript_SOURCE_DIR}/jerry-port/default/include
|
||||
)
|
||||
|
||||
# Suppress JerryScript-only warning
|
||||
if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
|
||||
target_compile_options(${JERRY_CORE_TARGET} PRIVATE
|
||||
-Wno-error=unterminated-string-initialization
|
||||
)
|
||||
endif()
|
||||
|
||||
add_library(jerryscript::core ALIAS ${JERRY_CORE_TARGET})
|
||||
add_library(jerryscript::ext ALIAS ${JERRY_EXT_TARGET})
|
||||
add_library(jerryscript::port ALIAS ${JERRY_PORT_TARGET})
|
||||
@@ -46,6 +46,15 @@ if(NOT Lua_FOUND)
|
||||
target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PUBLIC Lua::Lua)
|
||||
endif()
|
||||
|
||||
if(NOT jerryscript_FOUND)
|
||||
find_package(jerryscript REQUIRED)
|
||||
target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
||||
jerryscript::core
|
||||
jerryscript::ext
|
||||
jerryscript::port
|
||||
)
|
||||
endif()
|
||||
|
||||
# Includes
|
||||
target_include_directories(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
|
||||
@@ -459,15 +459,15 @@ void consoleDispose(void) {
|
||||
}
|
||||
|
||||
// Check for errors or input
|
||||
if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) break;
|
||||
if (!(pfd.revents & POLLIN)) {
|
||||
if(pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) break;
|
||||
if(!(pfd.revents & POLLIN)) {
|
||||
pfd.revents = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Read a line from stdin
|
||||
if(!fgets(line, CONSOLE_LINE_MAX, stdin)) {
|
||||
if (feof(stdin)) break;
|
||||
if(feof(stdin)) break;
|
||||
clearerr(stdin);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -24,6 +24,18 @@
|
||||
#include "system/system.h"
|
||||
#include "console/console.h"
|
||||
|
||||
#include "jerryscript.h"
|
||||
|
||||
double jerry_port_current_time(void) {
|
||||
dusktimeepoch_t epoch = timeGetEpoch();
|
||||
return epoch.time * 1000.0;
|
||||
}
|
||||
|
||||
int32_t jerry_port_local_tza(double unix_ms) {
|
||||
(void) unix_ms;
|
||||
return 0;
|
||||
}
|
||||
|
||||
engine_t ENGINE;
|
||||
|
||||
errorret_t engineInit(const int32_t argc, const char_t **argv) {
|
||||
@@ -48,6 +60,23 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) {
|
||||
errorChain(networkInit());
|
||||
errorChain(gameInit());
|
||||
|
||||
//
|
||||
jerry_init(JERRY_INIT_EMPTY);
|
||||
const jerry_char_t script[] = "1 + 1";
|
||||
jerry_value_t result = jerry_eval(
|
||||
script,
|
||||
sizeof(script) - 1,
|
||||
JERRY_PARSE_NO_OPTS
|
||||
);
|
||||
if(jerry_value_is_exception(result)) {
|
||||
consolePrint("Script error\n");
|
||||
} else {
|
||||
double num = jerry_value_as_number(result);
|
||||
consolePrint("Result: %f\n", num);
|
||||
}
|
||||
jerry_value_free(result);
|
||||
jerry_cleanup();
|
||||
|
||||
/* Run the init script. */
|
||||
consolePrint("Engine initialized");
|
||||
errorChain(scriptContextExecFile(&SCRIPT_MANAGER.mainContext, "init.lua"));
|
||||
|
||||
@@ -7,4 +7,5 @@
|
||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
time.c
|
||||
timeepoch.c
|
||||
)
|
||||
@@ -9,6 +9,8 @@
|
||||
#include "util/memory.h"
|
||||
#include "assert/assert.h"
|
||||
|
||||
#include "console/console.h"
|
||||
|
||||
dusktime_t TIME;
|
||||
|
||||
void timeInit(void) {
|
||||
@@ -46,4 +48,14 @@ void timeUpdate(void) {
|
||||
TIME.delta = DUSK_TIME_STEP;
|
||||
TIME.time += DUSK_TIME_STEP;
|
||||
#endif
|
||||
|
||||
// Print time in UTC style string
|
||||
dusktimeepoch_t epoch = timeGetEpoch();
|
||||
char_t buffer[256];
|
||||
timeEpochFormat(epoch, "%Y-%m-%d %H:%M:%S", buffer, sizeof(buffer));
|
||||
consolePrint("Real Time: %s", buffer);
|
||||
}
|
||||
|
||||
dusktimeepoch_t timeGetEpoch(void) {
|
||||
return timeEpochInit(timeGetRealPlatform(), timeGetRealTimeZonePlatform());
|
||||
}
|
||||
+10
-2
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dusk.h"
|
||||
#include "timeepoch.h"
|
||||
|
||||
#ifndef DUSK_TIME_STEP
|
||||
#define DUSK_TIME_STEP (16.0f / 1000.0f)
|
||||
@@ -46,4 +46,12 @@ void timeInit(void);
|
||||
/**
|
||||
* Updates the time system
|
||||
*/
|
||||
void timeUpdate(void);
|
||||
void timeUpdate(void);
|
||||
|
||||
/**
|
||||
* Returns the current epoch time, in seconds since January 1, 1970, with the
|
||||
* current time zone offset applied.
|
||||
*
|
||||
* @return The current epoch time.
|
||||
*/
|
||||
dusktimeepoch_t timeGetEpoch(void);
|
||||
@@ -0,0 +1,198 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "time.h"
|
||||
#include "assert/assert.h"
|
||||
|
||||
dusktimeepoch_t timeEpochInit(const double_t time, const double_t timeZone) {
|
||||
dusktimeepoch_t epoch;
|
||||
epoch.time = time;
|
||||
epoch.timeZone = timeZone;
|
||||
epoch.offsetTime = time + timeZone;
|
||||
return epoch;
|
||||
}
|
||||
|
||||
dusktimeepoch_t timeEpochSwitchTimeZone(
|
||||
const dusktimeepoch_t epoch,
|
||||
const double_t offset
|
||||
) {
|
||||
return timeEpochInit(
|
||||
epoch.time + offset - epoch.timeZone,
|
||||
offset
|
||||
);
|
||||
}
|
||||
|
||||
int32_t timeEpochCompare(const dusktimeepoch_t a, const dusktimeepoch_t b) {
|
||||
if(a.time < b.time) return -1;
|
||||
if(a.time > b.time) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t timeEpochGetHours(const dusktimeepoch_t epoch) {
|
||||
return ((int32_t)(epoch.offsetTime / 3600.0)) % 24;
|
||||
}
|
||||
|
||||
int32_t timeEpochGetMinutes(const dusktimeepoch_t epoch) {
|
||||
return ((int32_t)(epoch.offsetTime / 60.0)) % 60;
|
||||
}
|
||||
|
||||
int32_t timeEpochGetSeconds(const dusktimeepoch_t epoch) {
|
||||
return ((int32_t)(epoch.offsetTime)) % 60;
|
||||
}
|
||||
|
||||
int32_t timeEpochGetDayOfMonth(const dusktimeepoch_t epoch) {
|
||||
int64_t days = (int64_t)(epoch.offsetTime / 86400.0);
|
||||
|
||||
int32_t year = TIME_EPOCH_START_YEAR;
|
||||
|
||||
while (1) {
|
||||
int32_t daysInYear = TIME_EPOCH_DAYS_IN_YEAR(year);
|
||||
if(days < daysInYear) break;
|
||||
|
||||
days -= daysInYear;
|
||||
year++;
|
||||
}
|
||||
|
||||
int32_t month = timeEpochGetMonth(epoch);
|
||||
|
||||
for(int32_t i = 0; i < month - 1; i++) {
|
||||
int32_t daysInMonth = TIME_EPOCH_DAYS_IN_MONTH[i];
|
||||
|
||||
if(i == 1 && timeEpochIsLeapYear(year)) {
|
||||
daysInMonth = 29;
|
||||
}
|
||||
|
||||
days -= daysInMonth;
|
||||
}
|
||||
|
||||
return (int32_t)days + 1;
|
||||
}
|
||||
|
||||
int32_t timeEpochGetMonth(const dusktimeepoch_t epoch) {
|
||||
int64_t days = (int64_t)(epoch.offsetTime / 86400.0);
|
||||
|
||||
int32_t year = TIME_EPOCH_START_YEAR;
|
||||
|
||||
while (1) {
|
||||
int32_t daysInYear = TIME_EPOCH_DAYS_IN_YEAR(year);
|
||||
if(days < daysInYear) break;
|
||||
|
||||
days -= daysInYear;
|
||||
year++;
|
||||
}
|
||||
|
||||
for(int32_t month = 0; month < 12; month++) {
|
||||
int32_t daysInMonth = TIME_EPOCH_DAYS_IN_MONTH[month];
|
||||
|
||||
if(month == 1 && timeEpochIsLeapYear(year)) daysInMonth = 29;
|
||||
if(days < daysInMonth) return month + 1;
|
||||
days -= daysInMonth;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int32_t timeEpochGetYear(const dusktimeepoch_t epoch) {
|
||||
int64_t days = (int64_t)(epoch.offsetTime / 86400.0);
|
||||
int32_t year = TIME_EPOCH_START_YEAR;
|
||||
|
||||
while (1) {
|
||||
int32_t daysInYear = TIME_EPOCH_DAYS_IN_YEAR(year);
|
||||
if(days < daysInYear) return year;
|
||||
|
||||
days -= daysInYear;
|
||||
year++;
|
||||
}
|
||||
}
|
||||
|
||||
bool_t timeEpochIsLeapYear(const int32_t year) {
|
||||
return (
|
||||
((year % 4 == 0) && (year % 100 != 0)) ||
|
||||
(year % 400 == 0)
|
||||
);
|
||||
}
|
||||
|
||||
size_t timeEpochFormat(
|
||||
const dusktimeepoch_t epoch,
|
||||
const char_t* format,
|
||||
char_t* buffer,
|
||||
const size_t bufferSize
|
||||
) {
|
||||
assertNotNull(format, "Format string cannot be null");
|
||||
assertNotNull(buffer, "Buffer cannot be null");
|
||||
assertTrue(bufferSize > 0, "Buffer size must be greater than 0");
|
||||
|
||||
size_t bufferIndex = 0;
|
||||
|
||||
for(size_t i = 0; format[i] != '\0'; i++) {
|
||||
if(format[i] == '%') {
|
||||
i++;
|
||||
char_t formatSpecifier = format[i];
|
||||
|
||||
char_t tempBuffer[16];
|
||||
size_t tempBufferIndex = 0;
|
||||
|
||||
switch (formatSpecifier) {
|
||||
case 'Y':
|
||||
tempBufferIndex = snprintf(
|
||||
tempBuffer, sizeof(tempBuffer), "%04d",
|
||||
timeEpochGetYear(epoch)
|
||||
);
|
||||
break;
|
||||
case 'm':
|
||||
tempBufferIndex = snprintf(
|
||||
tempBuffer, sizeof(tempBuffer), "%02d",
|
||||
timeEpochGetMonth(epoch)
|
||||
);
|
||||
break;
|
||||
case 'd':
|
||||
tempBufferIndex = snprintf(
|
||||
tempBuffer, sizeof(tempBuffer), "%02d",
|
||||
timeEpochGetDayOfMonth(epoch)
|
||||
);
|
||||
break;
|
||||
case 'H':
|
||||
tempBufferIndex = snprintf(
|
||||
tempBuffer, sizeof(tempBuffer), "%02d",
|
||||
timeEpochGetHours(epoch)
|
||||
);
|
||||
break;
|
||||
case 'M':
|
||||
tempBufferIndex = snprintf(
|
||||
tempBuffer, sizeof(tempBuffer), "%02d",
|
||||
timeEpochGetMinutes(epoch)
|
||||
);
|
||||
break;
|
||||
case 'S':
|
||||
tempBufferIndex = snprintf(
|
||||
tempBuffer, sizeof(tempBuffer), "%02d",
|
||||
timeEpochGetSeconds(epoch)
|
||||
);
|
||||
break;
|
||||
default:
|
||||
continue;
|
||||
}
|
||||
|
||||
for(size_t j = 0; j < tempBufferIndex; j++) {
|
||||
if(bufferIndex < bufferSize - 1) {
|
||||
buffer[bufferIndex++] = tempBuffer[j];
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if(bufferIndex < bufferSize - 1) {
|
||||
buffer[bufferIndex++] = format[i];
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
buffer[bufferIndex] = '\0';
|
||||
return bufferIndex;
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dusk.h"
|
||||
|
||||
typedef struct {
|
||||
double_t time;
|
||||
double_t timeZone;
|
||||
double_t offsetTime;
|
||||
} dusktimeepoch_t;
|
||||
|
||||
static const int32_t TIME_EPOCH_DAYS_IN_MONTH[] = {
|
||||
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
|
||||
};
|
||||
|
||||
#define TIME_EPOCH_START_YEAR 1970
|
||||
#define TIME_EPOCH_DAYS_IN_YEAR_NON_LEAP 365
|
||||
#define TIME_EPOCH_DAYS_IN_YEAR_LEAP 366
|
||||
#define TIME_EPOCH_DAYS_IN_YEAR(year) (\
|
||||
timeEpochIsLeapYear(year) ? \
|
||||
TIME_EPOCH_DAYS_IN_YEAR_LEAP : \
|
||||
TIME_EPOCH_DAYS_IN_YEAR_NON_LEAP\
|
||||
)
|
||||
|
||||
/**
|
||||
* Initializes an epoch time with the given time and time zone.
|
||||
*
|
||||
* @param time The time in seconds since January 1, 1970.
|
||||
* @param timeZone The time zone offset in seconds from UTC.
|
||||
* @return The initialized epoch time.
|
||||
*/
|
||||
dusktimeepoch_t timeEpochInit(const double_t time, const double_t timeZone);
|
||||
|
||||
/**
|
||||
* Changes the time from one timezone to another.
|
||||
*
|
||||
* @param epoch The epoch time to change the timezone of.
|
||||
* @param offset The offset in seconds to change the timezone by.
|
||||
* @return The epoch time with the timezone changed.
|
||||
*/
|
||||
dusktimeepoch_t timeEpochSwitchTimeZone(
|
||||
const dusktimeepoch_t epoch,
|
||||
const double_t offset
|
||||
);
|
||||
|
||||
/**
|
||||
* Compares two epoch times.
|
||||
*
|
||||
* @param a The first epoch time.
|
||||
* @param b The second epoch time.
|
||||
* @return A negative value if a < b, zero if a == b, a positive value if a > b.
|
||||
*/
|
||||
int32_t timeEpochCompare(const dusktimeepoch_t a, const dusktimeepoch_t b);
|
||||
|
||||
/**
|
||||
* Returns the hours component of the epoch time, in the range [0, 23]. This is
|
||||
* in the timezone of the epoch.
|
||||
*
|
||||
* @param epoch The epoch time to get the hours component of.
|
||||
* @return The hours component of the epoch time.
|
||||
*/
|
||||
int32_t timeEpochGetHours(const dusktimeepoch_t epoch);
|
||||
|
||||
/**
|
||||
* Returns the minutes component of the epoch time, in the range [0, 59]. This
|
||||
* is in the timezone of the epoch.
|
||||
*
|
||||
* @param epoch The epoch time to get the minutes component of.
|
||||
* @return The minutes component of the epoch time.
|
||||
*/
|
||||
int32_t timeEpochGetMinutes(const dusktimeepoch_t epoch);
|
||||
|
||||
/**
|
||||
* Returns the seconds component of the epoch time, in the range [0, 59]. This
|
||||
* is in the timezone of the epoch.
|
||||
*
|
||||
* @param epoch The epoch time to get the seconds component of.
|
||||
* @return The seconds component of the epoch time.
|
||||
*/
|
||||
int32_t timeEpochGetSeconds(const dusktimeepoch_t epoch);
|
||||
|
||||
/**
|
||||
* Returns the day of the month component of the epoch time, in the range of [
|
||||
* 0, 30]. This is in the timezone of the epoch. e.g. Jan 1st is 0, Jan 31st is
|
||||
* 30.
|
||||
*
|
||||
* @param epoch The epoch time to get the day of the month component of.
|
||||
* @return The day of the month component of the epoch time.
|
||||
*/
|
||||
int32_t timeEpochGetDayOfMonth(const dusktimeepoch_t epoch);
|
||||
|
||||
/**
|
||||
* Returns the month component of the epoch time, in the range of [0, 11]. This
|
||||
* is in the timezone of the epoch. e.g. Jan is 0, Dec is 11.
|
||||
*
|
||||
* @param epoch The epoch time to get the month component of.
|
||||
* @return The month component of the epoch time.
|
||||
*/
|
||||
int32_t timeEpochGetMonth(const dusktimeepoch_t epoch);
|
||||
|
||||
/**
|
||||
* Returns the year component of the epoch time. This is in the timezone of the
|
||||
* epoch.
|
||||
*
|
||||
* @param epoch The epoch time to get the year component of.
|
||||
* @return The year component of the epoch time.
|
||||
*/
|
||||
int32_t timeEpochGetYear(const dusktimeepoch_t epoch);
|
||||
|
||||
/**
|
||||
* Returns whether the given year is a leap year.
|
||||
*
|
||||
* @param year The year to check if it is a leap year.
|
||||
* @return Whether the given year is a leap year.
|
||||
*/
|
||||
bool_t timeEpochIsLeapYear(const int32_t year);
|
||||
|
||||
/**
|
||||
* Buffers the epoch time into a string according to the given format.
|
||||
*
|
||||
* Format parameters:
|
||||
* %Y - Year with century as a decimal number.
|
||||
* %m - Month as a decimal number (01-12).
|
||||
* %d - Day of the month as a decimal number (01-31).
|
||||
* %H - Hour (24-hour clock) as a decimal number (00-23).
|
||||
* %M - Minute as a decimal number (00-59).
|
||||
* %S - Second as a decimal number (00-59).
|
||||
*
|
||||
* @param epoch The epoch time to format.
|
||||
* @param format The format string to format the epoch time with.
|
||||
* @param buffer The buffer to write the formatted string to.
|
||||
* @param bufferSize The size of the buffer to write the formatted string to.
|
||||
* @return The number of characters written to the buffer, excluding the null
|
||||
*/
|
||||
size_t timeEpochFormat(
|
||||
const dusktimeepoch_t epoch,
|
||||
const char_t* format,
|
||||
char_t* buffer,
|
||||
const size_t bufferSize
|
||||
);
|
||||
@@ -14,4 +14,5 @@ add_subdirectory(asset)
|
||||
add_subdirectory(log)
|
||||
add_subdirectory(input)
|
||||
add_subdirectory(network)
|
||||
add_subdirectory(system)
|
||||
add_subdirectory(system)
|
||||
add_subdirectory(time)
|
||||
@@ -28,7 +28,7 @@ bool_t networkLinuxIsConnected() {
|
||||
// Check if any non loopback interfaces have running flag set.
|
||||
bool_t connected = false;
|
||||
for(ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
|
||||
if (!ifa->ifa_name || !ifa->ifa_flags) continue;
|
||||
if(!ifa->ifa_name || !ifa->ifa_flags) continue;
|
||||
|
||||
// Skip loopback (localhost)
|
||||
if(ifa->ifa_flags & IFF_LOOPBACK) continue;
|
||||
@@ -64,7 +64,7 @@ networkinfo_t networkLinuxGetInfo() {
|
||||
|
||||
// Find the first non-loopback interface with an IP address
|
||||
for(ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
|
||||
if (!ifa->ifa_name || !ifa->ifa_flags) continue;
|
||||
if(!ifa->ifa_name || !ifa->ifa_flags) continue;
|
||||
|
||||
// Skip loopback (localhost)
|
||||
if(ifa->ifa_flags & IFF_LOOPBACK) continue;
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
# Copyright (c) 2026 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
timelinux.c
|
||||
)
|
||||
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "time/timelinux.h"
|
||||
#include <time.h>
|
||||
|
||||
double_t timeGetRealLinux(void) {
|
||||
struct timespec ts;
|
||||
|
||||
if(clock_gettime(CLOCK_REALTIME, &ts) != 0) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
return (
|
||||
((double)ts.tv_sec) +
|
||||
((double)ts.tv_nsec / 1000000000.0)
|
||||
);
|
||||
}
|
||||
|
||||
double_t timeGetRealTimeZoneLinux(void) {
|
||||
time_t now = time(NULL);
|
||||
struct tm local_tm;
|
||||
localtime_r(&now, &local_tm);
|
||||
return (double_t)(local_tm.tm_gmtoff);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dusk.h"
|
||||
|
||||
/**
|
||||
* Returns the real current time, in seconds since January 1, 1970.
|
||||
*
|
||||
* @return The real current time, in seconds since January 1, 1970.
|
||||
*/
|
||||
double_t timeGetRealLinux(void);
|
||||
|
||||
/**
|
||||
* Returns the real time zone offset in seconds from UTC.
|
||||
*
|
||||
* @return The real time zone offset in seconds from UTC.
|
||||
*/
|
||||
double_t timeGetRealTimeZoneLinux(void);
|
||||
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "time/timesdl2.h"
|
||||
#include "time/timelinux.h"
|
||||
|
||||
#define timeTickPlatform timeTickSDL2
|
||||
#define timeGetDeltaPlatform timeGetDeltaSDL2
|
||||
#define timeGetRealPlatform timeGetRealLinux
|
||||
#define timeGetRealTimeZonePlatform timeGetRealTimeZoneLinux
|
||||
@@ -9,4 +9,5 @@
|
||||
#include "timesdl2.h"
|
||||
|
||||
#define timeTickPlatform timeTickSDL2
|
||||
#define timeGetDeltaPlatform timeGetDeltaSDL2
|
||||
#define timeGetDeltaPlatform timeGetDeltaSDL2
|
||||
#define timeGetRealPlatform timeGetRealSDL2
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "timesdl2.h"
|
||||
#include "assert/assert.h"
|
||||
#include <time.h>
|
||||
|
||||
void timeTickSDL2(void) {
|
||||
TIME_TICKS_LAST_SDL2 = TIME_TICKS_SDL2;
|
||||
|
||||
Reference in New Issue
Block a user