mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-04-10 10:53:36 +02:00
Add time and realtime clock functions
Adds functions to query the system's realtime clock, convert time intervals to/from a calendar date and time in either UTC or the local time, and perform time related calculations. An SDL_Time type (a time interval represented in nanoseconds), and SDL_DateTime struct (broken down calendar date and time) were added to facilitate this functionality. Querying the system time results in a value expressed in nanoseconds since the Unix epoch (Jan 1, 1970) in UTC +0000. Conversions to and from the various platform epochs and units are performed when required. Any direct handling of timezones and DST were intentionally avoided. The offset from UTC is provided when converting from UTC to a local time by calculating the difference between the original UTC and the resulting local time, but no other timezone or DST information is used. The preferred date formatting and 12/24 hour time for the system locale can be retrieved via global preferences. Helper functions for obtaining the day of week or day or year for calendar date, and getting the number of days in a month in a given year are provided for convenience. These are simple, but useful for performing various time related calculations. An automated test for time conversion is included, as is a simple standalone test to display the current system date and time onscreen along with a calendar, the rendering of which demonstrates the use of the utility functions (press up/down to increment or decrement the current month, and keys 1-5 to change the date and time formats).
This commit is contained in:
committed by
Sam Lantinga
parent
b6c9a72740
commit
a6fbf0488c
@@ -52,6 +52,7 @@
|
||||
#define SDL_INIT_EVERYTHING ~0U
|
||||
|
||||
/* Initialization/Cleanup routines */
|
||||
#include "time/SDL_time_c.h"
|
||||
#include "timer/SDL_timer_c.h"
|
||||
#ifdef SDL_VIDEO_DRIVER_WINDOWS
|
||||
extern int SDL_HelperWindowCreate(void);
|
||||
@@ -204,6 +205,7 @@ int SDL_InitSubSystem(Uint32 flags)
|
||||
}
|
||||
#endif
|
||||
|
||||
SDL_InitTime();
|
||||
SDL_InitTicks();
|
||||
|
||||
/* Initialize the event subsystem */
|
||||
@@ -536,6 +538,7 @@ void SDL_Quit(void)
|
||||
SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
|
||||
|
||||
SDL_QuitTicks();
|
||||
SDL_QuitTime();
|
||||
|
||||
#ifdef SDL_USE_LIBDBUS
|
||||
SDL_DBus_Quit();
|
||||
|
||||
@@ -92,6 +92,7 @@ SDL3_0.0.0 {
|
||||
SDL_CreateWindowWithProperties;
|
||||
SDL_CursorVisible;
|
||||
SDL_DXGIGetOutputInfo;
|
||||
SDL_DateTimeToTime;
|
||||
SDL_DelEventWatch;
|
||||
SDL_DelHintCallback;
|
||||
SDL_Delay;
|
||||
@@ -128,8 +129,6 @@ SDL3_0.0.0 {
|
||||
SDL_EnumerateStorageDirectory;
|
||||
SDL_Error;
|
||||
SDL_EventEnabled;
|
||||
SDL_FileTimeFromWindows;
|
||||
SDL_FileTimeToWindows;
|
||||
SDL_FillSurfaceRect;
|
||||
SDL_FillSurfaceRects;
|
||||
SDL_FilterEvents;
|
||||
@@ -202,8 +201,12 @@ SDL3_0.0.0 {
|
||||
SDL_GetCurrentDisplayOrientation;
|
||||
SDL_GetCurrentRenderOutputSize;
|
||||
SDL_GetCurrentThreadID;
|
||||
SDL_GetCurrentTime;
|
||||
SDL_GetCurrentVideoDriver;
|
||||
SDL_GetCursor;
|
||||
SDL_GetDayOfWeek;
|
||||
SDL_GetDayOfYear;
|
||||
SDL_GetDaysInMonth;
|
||||
SDL_GetDefaultAssertionHandler;
|
||||
SDL_GetDefaultCursor;
|
||||
SDL_GetDesktopDisplayMode;
|
||||
@@ -781,6 +784,9 @@ SDL3_0.0.0 {
|
||||
SDL_TellIO;
|
||||
SDL_TextInputActive;
|
||||
SDL_TextInputShown;
|
||||
SDL_TimeFromWindows;
|
||||
SDL_TimeToDateTime;
|
||||
SDL_TimeToWindows;
|
||||
SDL_TryLockMutex;
|
||||
SDL_TryLockRWLockForReading;
|
||||
SDL_TryLockRWLockForWriting;
|
||||
|
||||
@@ -117,6 +117,7 @@
|
||||
#define SDL_CreateWindowWithProperties SDL_CreateWindowWithProperties_REAL
|
||||
#define SDL_CursorVisible SDL_CursorVisible_REAL
|
||||
#define SDL_DXGIGetOutputInfo SDL_DXGIGetOutputInfo_REAL
|
||||
#define SDL_DateTimeToTime SDL_DateTimeToTime_REAL
|
||||
#define SDL_DelEventWatch SDL_DelEventWatch_REAL
|
||||
#define SDL_DelHintCallback SDL_DelHintCallback_REAL
|
||||
#define SDL_Delay SDL_Delay_REAL
|
||||
@@ -153,8 +154,6 @@
|
||||
#define SDL_EnumerateStorageDirectory SDL_EnumerateStorageDirectory_REAL
|
||||
#define SDL_Error SDL_Error_REAL
|
||||
#define SDL_EventEnabled SDL_EventEnabled_REAL
|
||||
#define SDL_FileTimeFromWindows SDL_FileTimeFromWindows_REAL
|
||||
#define SDL_FileTimeToWindows SDL_FileTimeToWindows_REAL
|
||||
#define SDL_FillSurfaceRect SDL_FillSurfaceRect_REAL
|
||||
#define SDL_FillSurfaceRects SDL_FillSurfaceRects_REAL
|
||||
#define SDL_FilterEvents SDL_FilterEvents_REAL
|
||||
@@ -227,8 +226,12 @@
|
||||
#define SDL_GetCurrentDisplayOrientation SDL_GetCurrentDisplayOrientation_REAL
|
||||
#define SDL_GetCurrentRenderOutputSize SDL_GetCurrentRenderOutputSize_REAL
|
||||
#define SDL_GetCurrentThreadID SDL_GetCurrentThreadID_REAL
|
||||
#define SDL_GetCurrentTime SDL_GetCurrentTime_REAL
|
||||
#define SDL_GetCurrentVideoDriver SDL_GetCurrentVideoDriver_REAL
|
||||
#define SDL_GetCursor SDL_GetCursor_REAL
|
||||
#define SDL_GetDayOfWeek SDL_GetDayOfWeek_REAL
|
||||
#define SDL_GetDayOfYear SDL_GetDayOfYear_REAL
|
||||
#define SDL_GetDaysInMonth SDL_GetDaysInMonth_REAL
|
||||
#define SDL_GetDefaultAssertionHandler SDL_GetDefaultAssertionHandler_REAL
|
||||
#define SDL_GetDefaultCursor SDL_GetDefaultCursor_REAL
|
||||
#define SDL_GetDesktopDisplayMode SDL_GetDesktopDisplayMode_REAL
|
||||
@@ -805,6 +808,9 @@
|
||||
#define SDL_TellIO SDL_TellIO_REAL
|
||||
#define SDL_TextInputActive SDL_TextInputActive_REAL
|
||||
#define SDL_TextInputShown SDL_TextInputShown_REAL
|
||||
#define SDL_TimeFromWindows SDL_TimeFromWindows_REAL
|
||||
#define SDL_TimeToDateTime SDL_TimeToDateTime_REAL
|
||||
#define SDL_TimeToWindows SDL_TimeToWindows_REAL
|
||||
#define SDL_TryLockMutex SDL_TryLockMutex_REAL
|
||||
#define SDL_TryLockRWLockForReading SDL_TryLockRWLockForReading_REAL
|
||||
#define SDL_TryLockRWLockForWriting SDL_TryLockRWLockForWriting_REAL
|
||||
|
||||
@@ -155,6 +155,7 @@ SDL_DYNAPI_PROC(int,SDL_CreateWindowAndRenderer,(int a, int b, Uint32 c, SDL_Win
|
||||
SDL_DYNAPI_PROC(SDL_Window*,SDL_CreateWindowWithProperties,(SDL_PropertiesID a),(a),return)
|
||||
SDL_DYNAPI_PROC(SDL_bool,SDL_CursorVisible,(void),(),return)
|
||||
SDL_DYNAPI_PROC(SDL_bool,SDL_DXGIGetOutputInfo,(SDL_DisplayID a, int *b, int *c),(a,b,c),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_DateTimeToTime,(const SDL_DateTime *a, SDL_Time *b),(a,b),return)
|
||||
SDL_DYNAPI_PROC(void,SDL_DelEventWatch,(SDL_EventFilter a, void *b),(a,b),)
|
||||
SDL_DYNAPI_PROC(void,SDL_DelHintCallback,(const char *a, SDL_HintCallback b, void *c),(a,b,c),)
|
||||
SDL_DYNAPI_PROC(void,SDL_Delay,(Uint32 a),(a),)
|
||||
@@ -191,8 +192,6 @@ SDL_DYNAPI_PROC(int,SDL_EnumerateProperties,(SDL_PropertiesID a, SDL_EnumeratePr
|
||||
SDL_DYNAPI_PROC(int,SDL_EnumerateStorageDirectory,(SDL_Storage *a, const char *b, SDL_EnumerateDirectoryCallback c, void *d),(a,b,c,d),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_Error,(SDL_errorcode a),(a),return)
|
||||
SDL_DYNAPI_PROC(SDL_bool,SDL_EventEnabled,(Uint32 a),(a),return)
|
||||
SDL_DYNAPI_PROC(SDL_FileTime,SDL_FileTimeFromWindows,(Uint32 a, Uint32 b),(a,b),return)
|
||||
SDL_DYNAPI_PROC(void,SDL_FileTimeToWindows,(Sint64 a, Uint32 *b, Uint32 *c),(a,b,c),)
|
||||
SDL_DYNAPI_PROC(int,SDL_FillSurfaceRect,(SDL_Surface *a, const SDL_Rect *b, Uint32 c),(a,b,c),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_FillSurfaceRects,(SDL_Surface *a, const SDL_Rect *b, int c, Uint32 d),(a,b,c,d),return)
|
||||
SDL_DYNAPI_PROC(void,SDL_FilterEvents,(SDL_EventFilter a, void *b),(a,b),)
|
||||
@@ -265,8 +264,12 @@ SDL_DYNAPI_PROC(const SDL_DisplayMode*,SDL_GetCurrentDisplayMode,(SDL_DisplayID
|
||||
SDL_DYNAPI_PROC(SDL_DisplayOrientation,SDL_GetCurrentDisplayOrientation,(SDL_DisplayID a),(a),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_GetCurrentRenderOutputSize,(SDL_Renderer *a, int *b, int *c),(a,b,c),return)
|
||||
SDL_DYNAPI_PROC(SDL_ThreadID,SDL_GetCurrentThreadID,(void),(),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_GetCurrentTime,(SDL_Time *a),(a),return)
|
||||
SDL_DYNAPI_PROC(const char*,SDL_GetCurrentVideoDriver,(void),(),return)
|
||||
SDL_DYNAPI_PROC(SDL_Cursor*,SDL_GetCursor,(void),(),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_GetDayOfWeek,(int a, int b, int c),(a,b,c),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_GetDayOfYear,(int a, int b, int c),(a,b,c),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_GetDaysInMonth,(int a, int b),(a,b),return)
|
||||
SDL_DYNAPI_PROC(SDL_AssertionHandler,SDL_GetDefaultAssertionHandler,(void),(),return)
|
||||
SDL_DYNAPI_PROC(SDL_Cursor*,SDL_GetDefaultCursor,(void),(),return)
|
||||
SDL_DYNAPI_PROC(const SDL_DisplayMode*,SDL_GetDesktopDisplayMode,(SDL_DisplayID a),(a),return)
|
||||
@@ -825,6 +828,9 @@ SDL_DYNAPI_PROC(int,SDL_SyncWindow,(SDL_Window *a),(a),return)
|
||||
SDL_DYNAPI_PROC(Sint64,SDL_TellIO,(SDL_IOStream *a),(a),return)
|
||||
SDL_DYNAPI_PROC(SDL_bool,SDL_TextInputActive,(void),(),return)
|
||||
SDL_DYNAPI_PROC(SDL_bool,SDL_TextInputShown,(void),(),return)
|
||||
SDL_DYNAPI_PROC(SDL_Time,SDL_TimeFromWindows,(Uint32 a, Uint32 b),(a,b),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_TimeToDateTime,(SDL_Time a, SDL_DateTime *b, SDL_bool c),(a,b,c),return)
|
||||
SDL_DYNAPI_PROC(void,SDL_TimeToWindows,(SDL_Time a, Uint32 *b, Uint32 *c),(a,b,c),)
|
||||
SDL_DYNAPI_PROC(int,SDL_TryLockMutex,(SDL_Mutex *a),(a),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_TryLockRWLockForReading,(SDL_RWLock *a),(a),return)
|
||||
SDL_DYNAPI_PROC(int,SDL_TryLockRWLockForWriting,(SDL_RWLock *a),(a),return)
|
||||
|
||||
@@ -22,38 +22,6 @@
|
||||
#include "SDL_internal.h"
|
||||
#include "SDL_sysfilesystem.h"
|
||||
|
||||
static const Sint64 delta_1601_epoch_100ns = 11644473600ll * 10000000ll; // [100 ns] (100 ns units between 1/1/1601 and 1/1/1970, 11644473600 seconds)
|
||||
|
||||
void SDL_FileTimeToWindows(SDL_FileTime ftime, Uint32 *dwLowDateTime, Uint32 *dwHighDateTime)
|
||||
{
|
||||
Uint64 wtime;
|
||||
|
||||
// Convert ftime to 100ns units
|
||||
Sint64 ftime_100ns = (ftime / 100);
|
||||
|
||||
if (ftime_100ns < 0 && -ftime_100ns > delta_1601_epoch_100ns) {
|
||||
// If we're trying to show a timestamp from before before the Windows epoch, (Jan 1, 1601), clamp it to zero
|
||||
wtime = 0;
|
||||
} else {
|
||||
wtime = (Uint64)(delta_1601_epoch_100ns + ftime_100ns);
|
||||
}
|
||||
|
||||
if (dwLowDateTime) {
|
||||
*dwLowDateTime = (Uint32)wtime;
|
||||
}
|
||||
|
||||
if (dwHighDateTime) {
|
||||
*dwHighDateTime = (Uint32)(wtime >> 32);
|
||||
}
|
||||
}
|
||||
|
||||
SDL_FileTime SDL_FileTimeFromWindows(Uint32 dwLowDateTime, Uint32 dwHighDateTime)
|
||||
{
|
||||
Uint64 wtime = (((Uint64)dwHighDateTime << 32) | dwLowDateTime);
|
||||
|
||||
return (Sint64)(wtime - delta_1601_epoch_100ns) * 100;
|
||||
}
|
||||
|
||||
int SDL_RemovePath(const char *path)
|
||||
{
|
||||
if (!path) {
|
||||
|
||||
@@ -124,10 +124,16 @@ int SDL_SYS_GetPathInfo(const char *path, SDL_PathInfo *info)
|
||||
info->size = (Uint64) statbuf.st_size;
|
||||
}
|
||||
|
||||
info->create_time = (SDL_FileTime)SDL_SECONDS_TO_NS(statbuf.st_ctime);
|
||||
info->modify_time = (SDL_FileTime)SDL_SECONDS_TO_NS(statbuf.st_mtime);
|
||||
info->access_time = (SDL_FileTime)SDL_SECONDS_TO_NS(statbuf.st_atime);
|
||||
|
||||
#if (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200809L) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 700)
|
||||
/* Use high-res file times, if available. */
|
||||
info->create_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_ctim.tv_sec) + statbuf.st_ctim.tv_nsec;
|
||||
info->modify_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_mtim.tv_sec) + statbuf.st_mtim.tv_nsec;
|
||||
info->access_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_atim.tv_sec) + statbuf.st_atim.tv_nsec;
|
||||
#else
|
||||
info->create_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_ctime);
|
||||
info->modify_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_mtime);
|
||||
info->access_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_atime);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -165,9 +165,9 @@ int SDL_SYS_GetPathInfo(const char *path, SDL_PathInfo *info)
|
||||
info->size = ((((Uint64) winstat.nFileSizeHigh) << 32) | winstat.nFileSizeLow);
|
||||
}
|
||||
|
||||
info->create_time = SDL_FileTimeFromWindows(winstat.ftCreationTime.dwLowDateTime, winstat.ftCreationTime.dwHighDateTime);
|
||||
info->modify_time = SDL_FileTimeFromWindows(winstat.ftLastWriteTime.dwLowDateTime, winstat.ftLastWriteTime.dwHighDateTime);
|
||||
info->access_time = SDL_FileTimeFromWindows(winstat.ftLastAccessTime.dwLowDateTime, winstat.ftLastAccessTime.dwHighDateTime);
|
||||
info->create_time = SDL_TimeFromWindows(winstat.ftCreationTime.dwLowDateTime, winstat.ftCreationTime.dwHighDateTime);
|
||||
info->modify_time = SDL_TimeFromWindows(winstat.ftLastWriteTime.dwLowDateTime, winstat.ftLastWriteTime.dwHighDateTime);
|
||||
info->access_time = SDL_TimeFromWindows(winstat.ftLastAccessTime.dwLowDateTime, winstat.ftLastAccessTime.dwHighDateTime);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
232
src/time/SDL_time.c
Normal file
232
src/time/SDL_time.c
Normal file
@@ -0,0 +1,232 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
#include "SDL_internal.h"
|
||||
|
||||
#include "SDL_time_c.h"
|
||||
|
||||
static SDL_bool time_initialized;
|
||||
|
||||
/* The following algorithms are based on those of Howard Hinnant and are in the public domain.
|
||||
*
|
||||
* http://howardhinnant.github.io/date_algorithms.html
|
||||
*/
|
||||
|
||||
/* Given a calendar date, returns days since Jan 1 1970, and optionally
|
||||
* the day of the week [0-6, 0 is Sunday] and day of the year [0-365].
|
||||
*/
|
||||
Sint64 SDL_CivilToDays(int year, int month, int day, int *day_of_week, int *day_of_year)
|
||||
{
|
||||
|
||||
year -= month <= 2;
|
||||
const int era = (year >= 0 ? year : year - 399) / 400;
|
||||
const unsigned yoe = (unsigned)(year - era * 400); // [0, 399]
|
||||
const unsigned doy = (153 * (month > 2 ? month - 3 : month + 9) + 2) / 5 + day - 1; // [0, 365]
|
||||
const unsigned doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; // [0, 146096]
|
||||
const Sint64 z = (Sint64)(era) * 146097 + (Sint64)(doe)-719468;
|
||||
|
||||
if (day_of_week) {
|
||||
*day_of_week = (int)(z >= -4 ? (z + 4) % 7 : (z + 5) % 7 + 6);
|
||||
}
|
||||
if (day_of_year) {
|
||||
/* This algorithm considers March 1 to be the first day of the year, so offset by Jan + Feb. */
|
||||
if (doy > 305) {
|
||||
/* Day 0 is the first day of the year. */
|
||||
*day_of_year = doy - 306;
|
||||
} else {
|
||||
const int doy_offset = 59 + (!(year % 4) && ((year % 100) || !(year % 400)));
|
||||
*day_of_year = doy + doy_offset;
|
||||
}
|
||||
}
|
||||
|
||||
return z;
|
||||
}
|
||||
|
||||
void SDL_InitTime()
|
||||
{
|
||||
if (time_initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Default to ISO 8061 date format, as it is unambiguous, and 24 hour time. */
|
||||
SDL_DATE_FORMAT dateFormat = SDL_DATE_FORMAT_YYYYMMDD;
|
||||
SDL_TIME_FORMAT timeFormat = SDL_TIME_FORMAT_24HR;
|
||||
SDL_PropertiesID props = SDL_GetGlobalProperties();
|
||||
|
||||
SDL_GetSystemTimeLocalePreferences(&dateFormat, &timeFormat);
|
||||
|
||||
if (!SDL_HasProperty(props, SDL_PROP_GLOBAL_SYSTEM_DATE_FORMAT_NUMBER)) {
|
||||
SDL_SetNumberProperty(props, SDL_PROP_GLOBAL_SYSTEM_DATE_FORMAT_NUMBER, dateFormat);
|
||||
}
|
||||
if (!SDL_HasProperty(props, SDL_PROP_GLOBAL_SYSTEM_TIME_FORMAT_NUMBER)) {
|
||||
SDL_SetNumberProperty(props, SDL_PROP_GLOBAL_SYSTEM_TIME_FORMAT_NUMBER, timeFormat);
|
||||
}
|
||||
|
||||
time_initialized = SDL_TRUE;
|
||||
}
|
||||
|
||||
void SDL_QuitTime()
|
||||
{
|
||||
time_initialized = SDL_FALSE;
|
||||
}
|
||||
|
||||
int SDL_GetDaysInMonth(int year, int month)
|
||||
{
|
||||
static const int DAYS_IN_MONTH[] = {
|
||||
30, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
|
||||
};
|
||||
|
||||
if (month < 1 || month > 12) {
|
||||
return SDL_SetError("Month out of range [1-12], requested: %i", month);
|
||||
}
|
||||
|
||||
int days = DAYS_IN_MONTH[month - 1];
|
||||
|
||||
/* A leap year occurs every 4 years...
|
||||
* but not every 100 years...
|
||||
* except for every 400 years.
|
||||
*/
|
||||
if (month == 2 && (!(year % 4) && ((year % 100) || !(year % 400)))) {
|
||||
++days;
|
||||
}
|
||||
|
||||
return days;
|
||||
}
|
||||
|
||||
int SDL_GetDayOfYear(int year, int month, int day)
|
||||
{
|
||||
int dayOfYear;
|
||||
|
||||
if (month < 1 || month > 12) {
|
||||
return SDL_SetError("Month out of range [1-12], requested: %i", month);
|
||||
}
|
||||
if (day < 1 || day > SDL_GetDaysInMonth(year, month)) {
|
||||
return SDL_SetError("Day out of range [1-%i], requested: %i", SDL_GetDaysInMonth(year, month), month);
|
||||
}
|
||||
|
||||
SDL_CivilToDays(year, month, day, NULL, &dayOfYear);
|
||||
return dayOfYear;
|
||||
}
|
||||
|
||||
int SDL_GetDayOfWeek(int year, int month, int day)
|
||||
{
|
||||
int dayOfWeek;
|
||||
|
||||
if (month < 1 || month > 12) {
|
||||
return SDL_SetError("Month out of range [1-12], requested: %i", month);
|
||||
}
|
||||
if (day < 1 || day > SDL_GetDaysInMonth(year, month)) {
|
||||
return SDL_SetError("Day out of range [1-%i], requested: %i", SDL_GetDaysInMonth(year, month), month);
|
||||
}
|
||||
|
||||
SDL_CivilToDays(year, month, day, &dayOfWeek, NULL);
|
||||
return dayOfWeek;
|
||||
}
|
||||
|
||||
static SDL_bool SDL_DateTimeIsValid(const SDL_DateTime *dt)
|
||||
{
|
||||
if (dt->month < 1 || dt->month > 12) {
|
||||
SDL_SetError("Malformed SDL_DateTime: month out of range [1-12], current: %i", dt->month);
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
const int daysInMonth = SDL_GetDaysInMonth(dt->year, dt->month);
|
||||
if (dt->day < 1 || dt->day > daysInMonth) {
|
||||
SDL_SetError("Malformed SDL_DateTime: day of month out of range [1-%i], current: %i", daysInMonth, dt->month);
|
||||
return SDL_FALSE;
|
||||
}
|
||||
if (dt->hour < 0 || dt->hour > 23) {
|
||||
SDL_SetError("Malformed SDL_DateTime: hour out of range [0-23], current: %i", dt->hour);
|
||||
return SDL_FALSE;
|
||||
}
|
||||
if (dt->minute < 0 || dt->minute > 59) {
|
||||
SDL_SetError("Malformed SDL_DateTime: minute out of range [0-59], current: %i", dt->minute);
|
||||
return SDL_FALSE;
|
||||
}
|
||||
if (dt->second < 0 || dt->second > 60) {
|
||||
SDL_SetError("Malformed SDL_DateTime: second out of range [0-60], current: %i", dt->second);
|
||||
return SDL_FALSE; /* 60 accounts for a possible leap second. */
|
||||
}
|
||||
if (dt->nanosecond < 0 || dt->nanosecond >= SDL_NS_PER_SECOND) {
|
||||
SDL_SetError("Malformed SDL_DateTime: nanosecond out of range [0-999999999], current: %i", dt->nanosecond);
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
return SDL_TRUE;
|
||||
}
|
||||
|
||||
int SDL_DateTimeToTime(const SDL_DateTime *dt, SDL_Time *ticks)
|
||||
{
|
||||
static const Sint64 max_seconds = SDL_NS_TO_SECONDS(SDL_MAX_TIME) - 1;
|
||||
static const Sint64 min_seconds = SDL_NS_TO_SECONDS(SDL_MIN_TIME) + 1;
|
||||
int ret = 0;
|
||||
|
||||
if (!dt) {
|
||||
return SDL_InvalidParamError("dt");
|
||||
}
|
||||
if (!ticks) {
|
||||
return SDL_InvalidParamError("ticks");
|
||||
}
|
||||
if (!SDL_DateTimeIsValid(dt)) {
|
||||
/* The validation function sets the error string. */
|
||||
return -1;
|
||||
}
|
||||
|
||||
*ticks = SDL_CivilToDays(dt->year, dt->month, dt->day, NULL, NULL) * SDL_SECONDS_PER_DAY;
|
||||
*ticks += (((dt->hour * 60) + dt->minute) * 60) + dt->second - dt->utc_offset;
|
||||
if (*ticks > max_seconds || *ticks < min_seconds) {
|
||||
*ticks = SDL_clamp(*ticks, min_seconds, max_seconds);
|
||||
ret = SDL_SetError("Date out of range for SDL_Time representation; SDL_Time value clamped");
|
||||
}
|
||||
*ticks = SDL_SECONDS_TO_NS(*ticks) + dt->nanosecond;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define DELTA_EPOCH_1601_100NS (11644473600ll * 10000000ll) // [100 ns] (100 ns units between 1601-01-01 and 1970-01-01, 11644473600 seconds)
|
||||
|
||||
void SDL_TimeToWindows(SDL_Time ticks, Uint32 *dwLowDateTime, Uint32 *dwHighDateTime)
|
||||
{
|
||||
/* Convert nanoseconds to Win32 ticks.
|
||||
* SDL_Time has a range of roughly 292 years, so even SDL_MIN_TIME can't underflow thw Win32 epoch.
|
||||
*/
|
||||
const Uint64 wtime = (Uint64)((ticks / 100) + DELTA_EPOCH_1601_100NS);
|
||||
|
||||
if (dwLowDateTime) {
|
||||
*dwLowDateTime = (Uint32)wtime;
|
||||
}
|
||||
|
||||
if (dwHighDateTime) {
|
||||
*dwHighDateTime = (Uint32)(wtime >> 32);
|
||||
}
|
||||
}
|
||||
|
||||
SDL_Time SDL_TimeFromWindows(Uint32 dwLowDateTime, Uint32 dwHighDateTime)
|
||||
{
|
||||
static const Uint64 wintime_min = (Uint64)((SDL_MIN_TIME / 100) + DELTA_EPOCH_1601_100NS);
|
||||
static const Uint64 wintime_max = (Uint64)((SDL_MAX_TIME / 100) + DELTA_EPOCH_1601_100NS);
|
||||
|
||||
Uint64 wtime = (((Uint64)dwHighDateTime << 32) | dwLowDateTime);
|
||||
|
||||
/* Clamp the windows time range to the SDL_Time min/max */
|
||||
wtime = SDL_clamp(wtime, wintime_min, wintime_max);
|
||||
|
||||
return (SDL_Time)(wtime - DELTA_EPOCH_1601_100NS) * 100;
|
||||
}
|
||||
39
src/time/SDL_time_c.h
Normal file
39
src/time/SDL_time_c.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef SDL_time_c_h_
|
||||
#define SDL_time_c_h_
|
||||
|
||||
#include "SDL_internal.h"
|
||||
|
||||
#define SDL_SECONDS_PER_DAY 86400
|
||||
|
||||
extern void SDL_InitTime(void);
|
||||
extern void SDL_QuitTime(void);
|
||||
|
||||
/* Given a calendar date, returns days since Jan 1 1970, and optionally
|
||||
* the day of the week (0-6, 0 is Sunday) and day of the year (0-365).
|
||||
*/
|
||||
extern Sint64 SDL_CivilToDays(int year, int month, int day, int *day_of_week, int *day_of_year);
|
||||
|
||||
extern void SDL_GetSystemTimeLocalePreferences(SDL_DATE_FORMAT *df, SDL_TIME_FORMAT *tf);
|
||||
|
||||
#endif /* SDL_time_c_h_ */
|
||||
102
src/time/n3ds/SDL_systime.c
Normal file
102
src/time/n3ds/SDL_systime.c
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
#include "SDL_internal.h"
|
||||
|
||||
#ifdef SDL_TIME_N3DS
|
||||
|
||||
#include "../SDL_time_c.h"
|
||||
#include <3ds.h>
|
||||
|
||||
/*
|
||||
* The 3DS clock is essentially a simple digital watch and provides
|
||||
* no timezone or DST functionality.
|
||||
*/
|
||||
|
||||
/* 3DS epoch is Jan 1 1900 */
|
||||
#define DELTA_EPOCH_1900_OFFSET_MS 2208988800000LL
|
||||
|
||||
/* Returns year/month/day triple in civil calendar
|
||||
* Preconditions: z is number of days since 1970-01-01 and is in the range:
|
||||
* [INT_MIN, INT_MAX-719468].
|
||||
*
|
||||
* http://howardhinnant.github.io/date_algorithms.html#civil_from_days
|
||||
*/
|
||||
static void civil_from_days(int days, int *year, int *month, int *day)
|
||||
{
|
||||
days += 719468;
|
||||
const int era = (days >= 0 ? days : days - 146096) / 146097;
|
||||
const unsigned doe = (unsigned)(days - era * 146097); // [0, 146096]
|
||||
const unsigned yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; // [0, 399]
|
||||
const int y = (int)(yoe) + era * 400;
|
||||
const unsigned doy = doe - (365 * yoe + yoe / 4 - yoe / 100); // [0, 365]
|
||||
const unsigned mp = (5 * doy + 2) / 153; // [0, 11]
|
||||
const unsigned d = doy - (153 * mp + 2) / 5 + 1; // [1, 31]
|
||||
const unsigned m = mp < 10 ? mp + 3 : mp - 9; // [1, 12]
|
||||
|
||||
*year = y + (m <= 2);
|
||||
*month = (int)m;
|
||||
*day = (int)d;
|
||||
}
|
||||
|
||||
void SDL_GetSystemTimeLocalePreferences(SDL_DATE_FORMAT *df, SDL_TIME_FORMAT *tf)
|
||||
{
|
||||
/* NOP */
|
||||
}
|
||||
|
||||
int SDL_GetCurrentTime(SDL_Time *ticks)
|
||||
{
|
||||
if (!ticks) {
|
||||
return SDL_InvalidParamError("ticks");
|
||||
}
|
||||
|
||||
/* Returns milliseconds since the epoch. */
|
||||
const Uint64 ndsTicksMax = (SDL_MAX_TIME / SDL_NS_PER_MS) + DELTA_EPOCH_1900_OFFSET_MS;
|
||||
const Uint64 ndsTicks = SDL_min(osGetTime(), ndsTicksMax);
|
||||
|
||||
*ticks = SDL_MS_TO_NS(ndsTicks - DELTA_EPOCH_1900_OFFSET_MS);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SDL_TimeToDateTime(SDL_Time ticks, SDL_DateTime *dt, SDL_bool localTime)
|
||||
{
|
||||
if (!dt) {
|
||||
return SDL_InvalidParamError("dt");
|
||||
}
|
||||
|
||||
const int days = (int)(SDL_NS_TO_SECONDS(ticks) / SDL_SECONDS_PER_DAY);
|
||||
civil_from_days(days, &dt->year, &dt->month, &dt->day);
|
||||
|
||||
int rem = (int)(SDL_NS_TO_SECONDS(ticks) - (days * SDL_SECONDS_PER_DAY));
|
||||
dt->hour = rem / (60 * 60);
|
||||
rem -= dt->hour * 60 * 60;
|
||||
dt->minute = rem / 60;
|
||||
rem -= dt->minute * 60;
|
||||
dt->second = rem;
|
||||
dt->nanosecond = ticks % SDL_NS_PER_SECOND;
|
||||
dt->utc_offset = 0; /* Unknown */
|
||||
|
||||
SDL_CivilToDays(dt->year, dt->month, dt->day, &dt->day_of_week, NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* SDL_TIME_N3DS */
|
||||
65
src/time/ps2/SDL_systime.c
Normal file
65
src/time/ps2/SDL_systime.c
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
#include "SDL_internal.h"
|
||||
|
||||
#ifdef SDL_TIME_PS2
|
||||
|
||||
#include "../SDL_time_c.h"
|
||||
|
||||
/* PS2 epoch is Jan 1 2000 JST (UTC +9) */
|
||||
#define UNIX_EPOCH_OFFSET_SEC 946717200
|
||||
|
||||
/* TODO: Implement this... */
|
||||
void SDL_GetSystemTimeLocalePreferences(SDL_DATE_FORMAT *df, SDL_TIME_FORMAT *tf)
|
||||
{
|
||||
}
|
||||
|
||||
int SDL_GetCurrentTime(SDL_Time *ticks)
|
||||
{
|
||||
if (!ticks) {
|
||||
return SDL_InvalidParamError("ticks");
|
||||
}
|
||||
|
||||
*ticks = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SDL_TimeToDateTime(SDL_Time ticks, SDL_DateTime *dt, SDL_bool localTime)
|
||||
{
|
||||
if (!dt) {
|
||||
return SDL_InvalidParamError("dt");
|
||||
}
|
||||
|
||||
dt->year = 1970;
|
||||
dt->month = 1;
|
||||
dt->day = 1;
|
||||
dt->hour = 0;
|
||||
dt->minute = 0;
|
||||
dt->second = 0;
|
||||
dt->nanosecond = 0;
|
||||
dt->day_of_week = 4;
|
||||
dt->utc_offset = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* SDL_TIME_PS2 */
|
||||
136
src/time/psp/SDL_systime.c
Normal file
136
src/time/psp/SDL_systime.c
Normal file
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
#include "SDL_internal.h"
|
||||
|
||||
#ifdef SDL_TIME_PSP
|
||||
|
||||
#include <psprtc.h>
|
||||
#include <psputility_sysparam.h>
|
||||
|
||||
#include "../SDL_time_c.h"
|
||||
|
||||
/* Sony seems to use 0001-01-01T00:00:00 as an epoch. */
|
||||
#define DELTA_EPOCH_0001_OFFSET 62135596800ULL
|
||||
|
||||
void SDL_GetSystemTimeLocalePreferences(SDL_DATE_FORMAT *df, SDL_TIME_FORMAT *tf)
|
||||
{
|
||||
int val;
|
||||
|
||||
if (sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_DATE_FORMAT, &val) == 0) {
|
||||
switch (val) {
|
||||
case PSP_SYSTEMPARAM_DATE_FORMAT_YYYYMMDD:
|
||||
*df = SDL_DATE_FORMAT_YYYYMMDD;
|
||||
break;
|
||||
case PSP_SYSTEMPARAM_DATE_FORMAT_MMDDYYYY:
|
||||
*df = SDL_DATE_FORMAT_MMDDYYYY;
|
||||
break;
|
||||
case PSP_SYSTEMPARAM_DATE_FORMAT_DDMMYYYY:
|
||||
*df = SDL_DATE_FORMAT_DDMMYYYY;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_TIME_FORMAT, &val) == 0) {
|
||||
switch (val) {
|
||||
case PSP_SYSTEMPARAM_TIME_FORMAT_24HR:
|
||||
*tf = SDL_TIME_FORMAT_24HR;
|
||||
break;
|
||||
case PSP_SYSTEMPARAM_TIME_FORMAT_12HR:
|
||||
*tf = SDL_TIME_FORMAT_12HR;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int SDL_GetCurrentTime(SDL_Time *ticks)
|
||||
{
|
||||
u64 sceTicks;
|
||||
|
||||
if (!ticks) {
|
||||
return SDL_InvalidParamError("ticks");
|
||||
}
|
||||
|
||||
const int ret = sceRtcGetCurrentTick(&sceTicks);
|
||||
if (!ret) {
|
||||
const u32 res = sceRtcGetTickResolution();
|
||||
const u32 div = SDL_NS_PER_SECOND / res;
|
||||
const Uint64 epoch_offset = DELTA_EPOCH_0001_OFFSET * res;
|
||||
|
||||
const Uint64 scetime_min = (Uint64)((SDL_MIN_TIME / div) + epoch_offset);
|
||||
const Uint64 scetime_max = (Uint64)((SDL_MAX_TIME / div) + epoch_offset);
|
||||
|
||||
/* Clamp to the valid SDL_Time range. */
|
||||
sceTicks = SDL_clamp(sceTicks, scetime_min, scetime_max);
|
||||
|
||||
*ticks = (SDL_Time)(sceTicks - epoch_offset) * div;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return SDL_SetError("Failed to retrieve system time (%i)", ret);
|
||||
}
|
||||
|
||||
int SDL_TimeToDateTime(SDL_Time ticks, SDL_DateTime *dt, SDL_bool localTime)
|
||||
{
|
||||
ScePspDateTime t;
|
||||
u64 local;
|
||||
int ret = 0;
|
||||
|
||||
if (!dt) {
|
||||
return SDL_InvalidParamError("dt");
|
||||
}
|
||||
|
||||
const u32 res = sceRtcGetTickResolution();
|
||||
const u32 div = (SDL_NS_PER_SECOND / res);
|
||||
const u64 sceTicks = (u64)((ticks / div) + (DELTA_EPOCH_0001_OFFSET * div));
|
||||
|
||||
if (localTime) {
|
||||
ret = sceRtcConvertUtcToLocalTime(&sceTicks, &local);
|
||||
} else {
|
||||
local = sceTicks;
|
||||
}
|
||||
|
||||
if (!ret) {
|
||||
ret = sceRtcSetTick(&t, &local);
|
||||
if (!ret) {
|
||||
dt->year = t.year;
|
||||
dt->month = t.month;
|
||||
dt->day = t.day;
|
||||
dt->hour = t.hour;
|
||||
dt->minute = t.minute;
|
||||
dt->second = t.second;
|
||||
dt->nanosecond = ticks % SDL_NS_PER_SECOND;
|
||||
dt->utc_offset = (int)(((Sint64)local - (Sint64)sceTicks) / (Sint64)res);
|
||||
|
||||
SDL_CivilToDays(dt->year, dt->month, dt->day, &dt->day_of_week, NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return SDL_SetError("Local time conversion failed (%i)", ret);
|
||||
}
|
||||
|
||||
#endif /* SDL_TIME_PSP */
|
||||
193
src/time/unix/SDL_systime.c
Normal file
193
src/time/unix/SDL_systime.c
Normal file
@@ -0,0 +1,193 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
#include "SDL_internal.h"
|
||||
|
||||
#ifdef SDL_TIME_UNIX
|
||||
|
||||
#include "../SDL_time_c.h"
|
||||
#include <errno.h>
|
||||
#include <langinfo.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
|
||||
#if !defined(HAVE_CLOCK_GETTIME) && defined(SDL_PLATFORM_APPLE)
|
||||
#include <mach/clock.h>
|
||||
#include <mach/mach.h>
|
||||
#include <mach/mach_time.h>
|
||||
#endif
|
||||
|
||||
void SDL_GetSystemTimeLocalePreferences(SDL_DATE_FORMAT *df, SDL_TIME_FORMAT *tf)
|
||||
{
|
||||
/* This *should* be well-supported aside from very old legacy systems, but apparently
|
||||
* Android didn't add this until SDK version 26, so a check is needed...
|
||||
*/
|
||||
#ifdef HAVE_NL_LANGINFO
|
||||
const char *s = nl_langinfo(D_FMT);
|
||||
|
||||
/* Figure out the preferred system date format from the first format character. */
|
||||
if (s) {
|
||||
while (*s) {
|
||||
switch (*s++) {
|
||||
case 'Y':
|
||||
case 'y':
|
||||
case 'F':
|
||||
case 'C':
|
||||
*df = SDL_DATE_FORMAT_YYYYMMDD;
|
||||
goto found_date;
|
||||
case 'd':
|
||||
case 'e':
|
||||
*df = SDL_DATE_FORMAT_DDMMYYYY;
|
||||
goto found_date;
|
||||
case 'b':
|
||||
case 'D':
|
||||
case 'h':
|
||||
case 'm':
|
||||
*df = SDL_DATE_FORMAT_MMDDYYYY;
|
||||
goto found_date;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
found_date:
|
||||
|
||||
s = nl_langinfo(T_FMT);
|
||||
|
||||
/* Figure out the preferred system date format. */
|
||||
if (s) {
|
||||
while (*s) {
|
||||
switch (*s++) {
|
||||
case 'H':
|
||||
case 'k':
|
||||
case 'T':
|
||||
*tf = SDL_TIME_FORMAT_24HR;
|
||||
return;
|
||||
case 'I':
|
||||
case 'l':
|
||||
case 'r':
|
||||
*tf = SDL_TIME_FORMAT_12HR;
|
||||
return;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
int SDL_GetCurrentTime(SDL_Time *ticks)
|
||||
{
|
||||
if (!ticks) {
|
||||
return SDL_InvalidParamError("ticks");
|
||||
}
|
||||
#ifdef HAVE_CLOCK_GETTIME
|
||||
struct timespec tp;
|
||||
|
||||
if (clock_gettime(CLOCK_REALTIME, &tp) == 0) {
|
||||
tp.tv_sec = SDL_min(tp.tv_sec, SDL_NS_TO_SECONDS(SDL_MAX_TIME) - 1);
|
||||
*ticks = SDL_SECONDS_TO_NS(tp.tv_sec) + tp.tv_nsec;
|
||||
return 0;
|
||||
}
|
||||
|
||||
SDL_SetError("Failed to retrieve system time (%i)", errno);
|
||||
|
||||
#elif defined(SDL_PLATFORM_APPLE)
|
||||
clock_serv_t cclock;
|
||||
int ret = host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
|
||||
if (ret == 0) {
|
||||
mach_timespec_t mts;
|
||||
|
||||
SDL_zero(mts);
|
||||
ret = clock_get_time(cclock, &mts);
|
||||
if (ret == 0) {
|
||||
/* mach_timespec_t tv_sec is 32-bit, so no overflow possible */
|
||||
*ticks = SDL_SECONDS_TO_NS(mts.tv_sec) + mts.tv_nsec;
|
||||
}
|
||||
mach_port_deallocate(mach_task_self(), cclock);
|
||||
|
||||
if (!ret) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_SetError("Failed to retrieve system time (%i)", ret);
|
||||
|
||||
#else
|
||||
struct timeval tv;
|
||||
SDL_zero(tv);
|
||||
if (gettimeofday(&tv, NULL) == 0) {
|
||||
tv.tv_sec = SDL_min(tv.tv_sec, SDL_NS_TO_SECONDS(SDL_MAX_TIME) - 1);
|
||||
*ticks = SDL_SECONDS_TO_NS(tv.tv_sec) + SDL_US_TO_NS(tv.tv_usec);
|
||||
return 0;
|
||||
}
|
||||
|
||||
SDL_SetError("Failed to retrieve system time (%i)", errno);
|
||||
#endif
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int SDL_TimeToDateTime(SDL_Time ticks, SDL_DateTime *dt, SDL_bool localTime)
|
||||
{
|
||||
#if defined (HAVE_GMTIME_R) || defined(HAVE_LOCALTIME_R)
|
||||
struct tm tm_storage;
|
||||
#endif
|
||||
struct tm *tm = NULL;
|
||||
|
||||
if (!dt) {
|
||||
return SDL_InvalidParamError("dt");
|
||||
}
|
||||
|
||||
const time_t tval = (time_t)SDL_NS_TO_SECONDS(ticks);
|
||||
|
||||
if (localTime) {
|
||||
#ifdef HAVE_LOCALTIME_R
|
||||
tm = localtime_r(&tval, &tm_storage);
|
||||
#else
|
||||
tm = localtime(&tval);
|
||||
#endif
|
||||
} else {
|
||||
#ifdef HAVE_GMTIME_R
|
||||
tm = gmtime_r(&tval, &tm_storage);
|
||||
#else
|
||||
tm = gmtime(&tval);
|
||||
#endif
|
||||
}
|
||||
|
||||
if (tm) {
|
||||
dt->year = tm->tm_year + 1900;
|
||||
dt->month = tm->tm_mon + 1;
|
||||
dt->day = tm->tm_mday;
|
||||
dt->hour = tm->tm_hour;
|
||||
dt->minute = tm->tm_min;
|
||||
dt->second = tm->tm_sec;
|
||||
dt->nanosecond = ticks % SDL_NS_PER_SECOND;
|
||||
dt->day_of_week = tm->tm_wday;
|
||||
dt->utc_offset = tm->tm_gmtoff;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return SDL_SetError("SDL_DateTime conversion failed (%i)", errno);
|
||||
}
|
||||
|
||||
#endif /* SDL_TIME_UNIX */
|
||||
135
src/time/vita/SDL_systime.c
Normal file
135
src/time/vita/SDL_systime.c
Normal file
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
#include "SDL_internal.h"
|
||||
|
||||
#ifdef SDL_TIME_VITA
|
||||
|
||||
#include "../SDL_time_c.h"
|
||||
#include <psp2/apputil.h>
|
||||
#include <psp2/rtc.h>
|
||||
#include <psp2/system_param.h>
|
||||
|
||||
/* Sony seems to use 0001-01-01T00:00:00 as an epoch. */
|
||||
#define DELTA_EPOCH_0001_OFFSET 62135596800ULL
|
||||
|
||||
void SDL_GetSystemTimeLocalePreferences(SDL_DATE_FORMAT *df, SDL_TIME_FORMAT *tf)
|
||||
{
|
||||
int val;
|
||||
|
||||
if (sceAppUtilSystemParamGetInt(SCE_SYSTEM_PARAM_ID_DATE_FORMAT, &val) == 0) {
|
||||
switch (val) {
|
||||
case SCE_SYSTEM_PARAM_DATE_FORMAT_YYYYMMDD:
|
||||
*df = SDL_DATE_FORMAT_YYYYMMDD;
|
||||
break;
|
||||
case SCE_SYSTEM_PARAM_DATE_FORMAT_MMDDYYYY:
|
||||
*df = SDL_DATE_FORMAT_MMDDYYYY;
|
||||
break;
|
||||
case SCE_SYSTEM_PARAM_DATE_FORMAT_DDMMYYYY:
|
||||
*df = SDL_DATE_FORMAT_DDMMYYYY;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (sceAppUtilSystemParamGetInt(SCE_SYSTEM_PARAM_ID_DATE_FORMAT, &val) == 0) {
|
||||
switch (val) {
|
||||
case SCE_SYSTEM_PARAM_TIME_FORMAT_24HR:
|
||||
*tf = SDL_TIME_FORMAT_24HR;
|
||||
break;
|
||||
case SCE_SYSTEM_PARAM_TIME_FORMAT_12HR:
|
||||
*tf = SDL_TIME_FORMAT_12HR;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int SDL_GetCurrentTime(SDL_Time *ticks)
|
||||
{
|
||||
SceRtcTick sceTicks;
|
||||
|
||||
if (!ticks) {
|
||||
return SDL_InvalidParamError("ticks");
|
||||
}
|
||||
|
||||
const int ret = sceRtcGetCurrentTick(&sceTicks);
|
||||
if (!ret) {
|
||||
const unsigned int res = sceRtcGetTickResolution();
|
||||
const unsigned int div = SDL_NS_PER_SECOND / res;
|
||||
const Uint64 epoch_offset = DELTA_EPOCH_0001_OFFSET * res;
|
||||
|
||||
const Uint64 scetime_min = (Uint64)((SDL_MIN_TIME / div) + epoch_offset);
|
||||
const Uint64 scetime_max = (Uint64)((SDL_MAX_TIME / div) + epoch_offset);
|
||||
|
||||
/* Clamp to the valid SDL_Time range. */
|
||||
sceTicks.tick = SDL_clamp(sceTicks.tick, scetime_min, scetime_max);
|
||||
*ticks = (SDL_Time)(sceTicks.tick - epoch_offset) * div;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return SDL_SetError("Failed to retrieve system time (%i)", ret);
|
||||
}
|
||||
|
||||
int SDL_TimeToDateTime(SDL_Time ticks, SDL_DateTime *dt, SDL_bool localTime)
|
||||
{
|
||||
SceDateTime t;
|
||||
SceRtcTick sceTicks, sceLocalTicks;
|
||||
int ret = 0;
|
||||
|
||||
if (!dt) {
|
||||
return SDL_InvalidParamError("dt");
|
||||
}
|
||||
|
||||
const unsigned int res = sceRtcGetTickResolution();
|
||||
const unsigned int div = (SDL_NS_PER_SECOND / res);
|
||||
sceTicks.tick = (Uint64)((ticks / div) + (DELTA_EPOCH_0001_OFFSET * div));
|
||||
|
||||
if (localTime) {
|
||||
ret = sceRtcConvertUtcToLocalTime(&sceTicks, &sceLocalTicks);
|
||||
} else {
|
||||
sceLocalTicks.tick = sceTicks.tick;
|
||||
}
|
||||
|
||||
if (!ret) {
|
||||
ret = sceRtcSetTick(&t, &sceLocalTicks);
|
||||
if (!ret) {
|
||||
dt->year = t.year;
|
||||
dt->month = t.month;
|
||||
dt->day = t.day;
|
||||
dt->hour = t.hour;
|
||||
dt->minute = t.minute;
|
||||
dt->second = t.second;
|
||||
dt->nanosecond = ticks % SDL_NS_PER_SECOND;
|
||||
dt->utc_offset = (int)(((Sint64)sceLocalTicks.tick - (Sint64)sceTicks.tick) / (Sint64)res);
|
||||
|
||||
SDL_CivilToDays(dt->year, dt->month, dt->day, &dt->day_of_week, NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return SDL_SetError("Local time conversion failed (%i)", ret);
|
||||
}
|
||||
|
||||
#endif /* SDL_TIME_VITA */
|
||||
139
src/time/windows/SDL_systime.c
Normal file
139
src/time/windows/SDL_systime.c
Normal file
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
#include "SDL_internal.h"
|
||||
|
||||
#ifdef SDL_TIME_WINDOWS
|
||||
|
||||
#include "../../core/windows/SDL_windows.h"
|
||||
#include <minwinbase.h>
|
||||
#include <timezoneapi.h>
|
||||
|
||||
#include "../SDL_time_c.h"
|
||||
|
||||
#define NS_PER_WINDOWS_TICK 100ULL
|
||||
#define WINDOWS_TICK 10000000ULL
|
||||
#define UNIX_EPOCH_OFFSET_SEC 11644473600ULL
|
||||
|
||||
void SDL_GetSystemTimeLocalePreferences(SDL_DATE_FORMAT *df, SDL_TIME_FORMAT *tf)
|
||||
{
|
||||
WCHAR str[80]; /* Per the docs, the time and short date format strings can be a max of 80 characters. */
|
||||
|
||||
if (GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, str, sizeof(str) / sizeof(WCHAR))) {
|
||||
LPWSTR s = str;
|
||||
while (*s) {
|
||||
switch (*s++) {
|
||||
case L'y':
|
||||
*df = SDL_DATE_FORMAT_YYYYMMDD;
|
||||
goto found_date;
|
||||
case L'd':
|
||||
*df = SDL_DATE_FORMAT_DDMMYYYY;
|
||||
goto found_date;
|
||||
case L'M':
|
||||
*df = SDL_DATE_FORMAT_MMDDYYYY;
|
||||
goto found_date;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
found_date:
|
||||
|
||||
/* Figure out the preferred system date format. */
|
||||
if (GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, str, sizeof(str) / sizeof(WCHAR))) {
|
||||
LPWSTR s = str;
|
||||
while (*s) {
|
||||
switch (*s++) {
|
||||
case L'H':
|
||||
*tf = SDL_TIME_FORMAT_24HR;
|
||||
return;
|
||||
case L'h':
|
||||
*tf = SDL_TIME_FORMAT_12HR;
|
||||
return;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int SDL_GetCurrentTime(SDL_Time *ticks)
|
||||
{
|
||||
FILETIME ft;
|
||||
|
||||
if (!ticks) {
|
||||
return SDL_InvalidParamError("ticks");
|
||||
}
|
||||
|
||||
SDL_zero(ft);
|
||||
GetSystemTimePreciseAsFileTime(&ft);
|
||||
*ticks = SDL_TimeFromWindows(ft.dwLowDateTime, ft.dwHighDateTime);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SDL_TimeToDateTime(SDL_Time ticks, SDL_DateTime *dt, SDL_bool localTime)
|
||||
{
|
||||
FILETIME ft, local_ft;
|
||||
SYSTEMTIME utc_st, local_st;
|
||||
SYSTEMTIME *st = NULL;
|
||||
Uint32 low, high;
|
||||
|
||||
if (!dt) {
|
||||
return SDL_InvalidParamError("dt");
|
||||
}
|
||||
|
||||
SDL_TimeToWindows(ticks, &low, &high);
|
||||
ft.dwLowDateTime = (DWORD)low;
|
||||
ft.dwHighDateTime = (DWORD)high;
|
||||
|
||||
if (FileTimeToSystemTime(&ft, &utc_st)) {
|
||||
if (localTime) {
|
||||
if (SystemTimeToTzSpecificLocalTime(NULL, &utc_st, &local_st)) {
|
||||
/* Calculate the difference for the UTC offset. */
|
||||
SystemTimeToFileTime(&local_st, &local_ft);
|
||||
const SDL_Time local_ticks = SDL_TimeFromWindows(local_ft.dwLowDateTime, local_ft.dwHighDateTime);
|
||||
dt->utc_offset = SDL_NS_TO_SECONDS(local_ticks - ticks);
|
||||
st = &local_st;
|
||||
}
|
||||
} else {
|
||||
dt->utc_offset = 0;
|
||||
st = &utc_st;
|
||||
}
|
||||
|
||||
if (st) {
|
||||
dt->year = st->wYear;
|
||||
dt->month = st->wMonth;
|
||||
dt->day = st->wDay;
|
||||
dt->hour = st->wHour;
|
||||
dt->minute = st->wMinute;
|
||||
dt->second = st->wSecond;
|
||||
dt->nanosecond = ticks % SDL_NS_PER_SECOND;
|
||||
dt->day_of_week = st->wDayOfWeek;
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return SDL_SetError("SDL_DateTime conversion failed (%lu)", GetLastError());
|
||||
}
|
||||
|
||||
#endif /* SDL_TIME_WINDOWS */
|
||||
Reference in New Issue
Block a user