Use stopwatch time for measuring ping and launcher caret, change UTC time measurement to seconds instead of milliseconds

This commit is contained in:
UnknownShadow200 2024-03-28 19:03:30 +11:00
parent 5fb4887191
commit ff42b7d352
26 changed files with 73 additions and 87 deletions

View File

@ -232,9 +232,9 @@ static void EntityShadows_MakeTexture(void) {
BitmapCol* row = Bitmap_GetRow(&bmp, y);
for (x = 0; x < sh_size; x++) {
double dist =
(sh_half - (x + 0.5)) * (sh_half - (x + 0.5)) +
(sh_half - (y + 0.5)) * (sh_half - (y + 0.5));
float dist =
(sh_half - (x + 0.5f)) * (sh_half - (x + 0.5f)) +
(sh_half - (y + 0.5f)) * (sh_half - (y + 0.5f));
row[x] = dist < sh_half * sh_half ? color : 0;
}
}

View File

@ -139,7 +139,7 @@ static void Http_Init(void) {
Http_InitCommon();
/* If this webpage is https://, browsers deny any http:// downloading */
httpsOnly = interop_IsHttpsOnly();
startTime = DateTime_CurrentUTC_MS();
startTime = DateTime_CurrentUTC();
RequestList_Init(&queuedReqs);
RequestList_Init(&workingReqs);

View File

@ -534,7 +534,7 @@ void LBackend_CheckboxDraw(struct LCheckbox* w) {
/*########################################################################################################################*
*------------------------------------------------------InputWidget--------------------------------------------------------*
*#########################################################################################################################*/
static TimeMS caretStart;
static cc_uint64 caretStart;
static Rect2D caretRect, lastCaretRect;
#define Rect2D_Equals(a, b) a.x == b.x && a.y == b.y && a.Width == b.Width && a.Height == b.Height
@ -620,7 +620,7 @@ void LBackend_InputTick(struct LInput* w) {
Rect2D r;
if (!caretStart) return;
elapsed = (int)(DateTime_CurrentUTC_MS() - caretStart);
elapsed = Stopwatch_ElapsedMS(caretStart, Stopwatch_Measure());
caretShow = (elapsed % 1000) < 500;
if (caretShow == w->caretShow) return;
@ -641,7 +641,7 @@ void LBackend_InputTick(struct LInput* w) {
void LBackend_InputSelect(struct LInput* w, int idx, cc_bool wasSelected) {
struct OpenKeyboardArgs args;
caretStart = DateTime_CurrentUTC_MS();
caretStart = Stopwatch_Measure();
w->caretShow = true;
LInput_MoveCaretToCursor(w, idx);
LBackend_MarkDirty(w);

View File

@ -1600,9 +1600,9 @@ static void UpdatesScreen_Format(struct LLabel* lbl, const char* prefix, cc_uint
if (!timestamp) {
String_AppendConst(&str, "&cCheck failed");
} else {
now = DateTime_CurrentUTC_MS() - UNIX_EPOCH;
now = DateTime_CurrentUTC() - UNIX_EPOCH_SECONDS;
/* must divide as cc_uint64, int delta overflows after 26 days */
delta = (int)((now / 1000) - timestamp);
delta = (int)(now - timestamp);
UpdatesScreen_FormatTime(&str, delta);
}
LLabel_SetText(lbl, &str);

View File

@ -24,8 +24,8 @@ typedef int cc_file;
/* Origin points for when seeking in a file. */
/* NOTE: These have same values as SEEK_SET/SEEK_CUR/SEEK_END, do not change them */
enum File_SeekFrom { FILE_SEEKFROM_BEGIN, FILE_SEEKFROM_CURRENT, FILE_SEEKFROM_END };
/* Number of milliseconds since 01/01/0001 to start of unix time. */
#define UNIX_EPOCH 62135596800000ULL
/* Number of seconds since 01/01/0001 to start of unix time. */
#define UNIX_EPOCH_SECONDS 62135596800ULL
extern const cc_result ReturnCode_FileShareViolation;
extern const cc_result ReturnCode_FileNotFound;
@ -163,8 +163,8 @@ void Platform_Log2(const char* format, const void* a1, const void* a2);
void Platform_Log3(const char* format, const void* a1, const void* a2, const void* a3);
void Platform_Log4(const char* format, const void* a1, const void* a2, const void* a3, const void* a4);
/* Returns the current UTC time, as number of milliseconds since 1/1/0001 */
CC_API TimeMS DateTime_CurrentUTC_MS(void);
/* Returns the current UTC time, as number of seconds since 1/1/0001 */
CC_API TimeMS DateTime_CurrentUTC(void);
/* Returns the current local Time. */
CC_API void DateTime_CurrentLocal(struct DateTime* t);
/* Takes a platform-specific stopwatch measurement. */

View File

@ -62,11 +62,10 @@ void Platform_Log(const char* msg, int len) {
svcOutputDebugString("\n", 1);
}
#define UnixTime_TotalMS(time) ((cc_uint64)time.tv_sec * 1000 + UNIX_EPOCH + (time.tv_usec / 1000))
TimeMS DateTime_CurrentUTC_MS(void) {
TimeMS DateTime_CurrentUTC(void) {
struct timeval cur;
gettimeofday(&cur, NULL);
return UnixTime_TotalMS(cur);
return (cc_uint64)cur.tv_sec + UNIX_EPOCH_SECONDS;
}
void DateTime_CurrentLocal(struct DateTime* t) {

View File

@ -75,7 +75,7 @@ void Platform_Log(const char* msg, int len) {
LogOnscreen(msg, len);
}
TimeMS DateTime_CurrentUTC_MS(void) {
TimeMS DateTime_CurrentUTC(void) {
uint32 secs, ms;
timer_ms_gettime(&secs, &ms);
@ -86,7 +86,7 @@ TimeMS DateTime_CurrentUTC_MS(void) {
if (boot_time < boot_time_2000) boot_time = boot_time_2024;
cc_uint64 curSecs = boot_time + secs;
return (curSecs * 1000 + ms) + UNIX_EPOCH;
return curSecs + UNIX_EPOCH_SECONDS;
}
void DateTime_CurrentLocal(struct DateTime* t) {

View File

@ -61,11 +61,10 @@ void Platform_Log(const char* msg, int len) {
// TODO: Just use printf("%s", somehow ???
}
#define UnixTime_TotalMS(time) ((cc_uint64)time.tv_sec * 1000 + UNIX_EPOCH + (time.tv_usec / 1000))
TimeMS DateTime_CurrentUTC_MS(void) {
TimeMS DateTime_CurrentUTC(void) {
struct timeval cur;
gettimeofday(&cur, NULL);
return UnixTime_TotalMS(cur);
return (cc_uint64)cur.tv_sec + UNIX_EPOCH_SECONDS;
}
void DateTime_CurrentLocal(struct DateTime* t) {
@ -594,4 +593,4 @@ static cc_result GetMachineID(cc_uint32* key) {
}
#endif
#endif
#endif

View File

@ -48,11 +48,10 @@ void Platform_Log(const char* msg, int len) {
write(STDERR_FILENO, "\n", 1);
}
#define UnixTime_TotalMS(time) ((cc_uint64)time.tv_sec * 1000 + UNIX_EPOCH + (time.tv_usec / 1000))
TimeMS DateTime_CurrentUTC_MS(void) {
TimeMS DateTime_CurrentUTC(void) {
struct timeval cur;
gettimeofday(&cur, NULL);
return UnixTime_TotalMS(cur);
return (cc_uint64)cur.tv_sec + UNIX_EPOCH_SECONDS;
}
void DateTime_CurrentLocal(struct DateTime* t) {

View File

@ -84,11 +84,10 @@ void Platform_Log(const char* msg, int len) {
LogNocash(msg, len);
}
#define UnixTime_TotalMS(time) ((cc_uint64)time.tv_sec * 1000 + UNIX_EPOCH)
TimeMS DateTime_CurrentUTC_MS(void) {
TimeMS DateTime_CurrentUTC(void) {
struct timeval cur;
gettimeofday(&cur, NULL);
return UnixTime_TotalMS(cur); // no usec on the DS
return (cc_uint64)cur.tv_sec + UNIX_EPOCH_SECONDS;
}
void DateTime_CurrentLocal(struct DateTime* t) {

View File

@ -39,8 +39,7 @@ void Platform_Log(const char* msg, int len) {
printf("%s\n", tmp);
}
#define UnixTime_TotalMS(time) ((cc_uint64)time.tv_sec * 1000 + UNIX_EPOCH + (time.tv_usec / 1000))
TimeMS DateTime_CurrentUTC_MS(void) {
TimeMS DateTime_CurrentUTC(void) {
return 0;
}

View File

@ -56,11 +56,10 @@ void Platform_Log(const char* msg, int len) {
_print("%s", tmp);
}
#define UnixTime_TotalMS(time) ((cc_uint64)time.tv_sec * 1000 + UNIX_EPOCH + (time.tv_usec / 1000))
TimeMS DateTime_CurrentUTC_MS(void) {
TimeMS DateTime_CurrentUTC(void) {
struct timeval cur;
gettimeofday(&cur, NULL);
return UnixTime_TotalMS(cur);
return (cc_uint64)cur.tv_sec + UNIX_EPOCH_SECONDS;
}
void DateTime_CurrentLocal(struct DateTime* t) {

View File

@ -52,11 +52,10 @@ void Platform_Log(const char* msg, int len) {
sysTtyWrite(STDOUT_FILENO, "\n", 1, &done);
}
#define UnixTime_TotalMS(time) ((cc_uint64)time.tv_sec * 1000 + UNIX_EPOCH + (time.tv_usec / 1000))
TimeMS DateTime_CurrentUTC_MS(void) {
TimeMS DateTime_CurrentUTC(void) {
struct timeval cur;
gettimeofday(&cur, NULL);
return UnixTime_TotalMS(cur);
return (cc_uint64)cur.tv_sec + UNIX_EPOCH_SECONDS;
}
void DateTime_CurrentLocal(struct DateTime* t) {
@ -483,4 +482,4 @@ cc_bool Platform_DescribeError(cc_result res, cc_string* dst) {
static cc_result GetMachineID(cc_uint32* key) {
return ERR_NOT_SUPPORTED;
}
#endif
#endif

View File

@ -54,11 +54,10 @@ void Platform_Log(const char* msg, int len) {
//File_Close(file);
}
#define UnixTime_TotalMS(time) ((cc_uint64)time.tv_sec * 1000 + UNIX_EPOCH + (time.tv_usec / 1000))
TimeMS DateTime_CurrentUTC_MS(void) {
TimeMS DateTime_CurrentUTC(void) {
struct SceKernelTimeval cur;
sceKernelLibcGettimeofday(&cur, NULL);
return UnixTime_TotalMS(cur);
return (cc_uint64)cur.tv_sec + UNIX_EPOCH_SECONDS;
}
void DateTime_CurrentLocal(struct DateTime* t) {

View File

@ -37,11 +37,10 @@ void Platform_Log(const char* msg, int len) {
sceIoWrite(stdout_fd, msg, len);
}
#define UnixTime_TotalMS(time) ((cc_uint64)time.sec * 1000 + UNIX_EPOCH + (time.usec / 1000))
TimeMS DateTime_CurrentUTC_MS(void) {
TimeMS DateTime_CurrentUTC(void) {
struct SceKernelTimeval cur;
sceKernelLibcGettimeofday(&cur, NULL);
return UnixTime_TotalMS(cur);
return (cc_uint64)cur.sec + UNIX_EPOCH_SECONDS;
}
void DateTime_CurrentLocal(struct DateTime* t) {

View File

@ -103,11 +103,10 @@ void Platform_Log(const char* msg, int len) {
}
#endif
#define UnixTime_TotalMS(time) ((cc_uint64)time.tv_sec * 1000 + UNIX_EPOCH + (time.tv_usec / 1000))
TimeMS DateTime_CurrentUTC_MS(void) {
TimeMS DateTime_CurrentUTC(void) {
struct timeval cur;
gettimeofday(&cur, NULL);
return UnixTime_TotalMS(cur);
return (cc_uint64)cur.tv_sec + UNIX_EPOCH_SECONDS;
}
void DateTime_CurrentLocal(struct DateTime* t) {

View File

@ -87,11 +87,10 @@ void Platform_Log(const char* msg, int len) {
svcOutputDebugString(msg, len);
}
#define UnixTime_TotalMS(time) ((cc_uint64)time.tv_sec * 1000 + UNIX_EPOCH + (time.tv_usec / 1000))
TimeMS DateTime_CurrentUTC_MS(void) {
TimeMS DateTime_CurrentUTC(void) {
struct timeval cur;
gettimeofday(&cur, NULL);
return UnixTime_TotalMS(cur);
return (cc_uint64)cur.tv_sec + UNIX_EPOCH_SECONDS;
}
void DateTime_CurrentLocal(struct DateTime* t) {

View File

@ -84,11 +84,10 @@ void Platform_Log(const char* msg, int len) {
interop_Log(msg, len);
}
#define UnixTime_TotalMS(time) ((cc_uint64)time.tv_sec * 1000 + UNIX_EPOCH + (time.tv_usec / 1000))
TimeMS DateTime_CurrentUTC_MS(void) {
TimeMS DateTime_CurrentUTC(void) {
struct timeval cur;
gettimeofday(&cur, NULL);
return UnixTime_TotalMS(cur);
return (cc_uint64)cur.tv_sec + UNIX_EPOCH_SECONDS;
}
extern void interop_GetLocalTime(struct DateTime* t);

View File

@ -58,15 +58,12 @@ void Platform_Log(const char* msg, int len) {
OSReport("%s\n", tmp);
}
#define WIIU_EPOCH_ADJUST 946684800000ULL // Wii U time epoch is year 2000, not 1970
#define WIIU_EPOCH_ADJUST 946684800ULL // Wii U time epoch is year 2000, not 1970
TimeMS DateTime_CurrentUTC_MS(void) {
OSTime time = OSGetTime();
// avoid overflow in time calculation
TimeMS DateTime_CurrentUTC(void) {
OSTime time = OSGetTime();
cc_int64 secs = (time_t)OSTicksToSeconds(time);
time -= OSSecondsToTicks(secs);
cc_uint64 msecs = OSTicksToMilliseconds(time);
return (secs * 1000 + msecs) + UNIX_EPOCH + WIIU_EPOCH_ADJUST;
return secs + UNIX_EPOCH_SECONDS + WIIU_EPOCH_ADJUST;
}
void DateTime_CurrentLocal(struct DateTime* t) {
@ -455,4 +452,4 @@ void Platform_Init(void) {
*-------------------------------------------------------Encryption--------------------------------------------------------*
*#########################################################################################################################*/
static cc_result GetMachineID(cc_uint32* key) { return ERR_NOT_SUPPORTED; }
#endif
#endif

View File

@ -96,18 +96,18 @@ void Platform_Log(const char* msg, int len) {
OutputDebugStringA("\n");
}
#define FILETIME_EPOCH 50491123200000ULL
#define FILETIME_UNIX_EPOCH 11644473600LL
#define FileTime_TotalMS(time) ((time / 10000) + FILETIME_EPOCH)
#define FileTime_UnixTime(time) ((time / 10000000) - FILETIME_UNIX_EPOCH)
TimeMS DateTime_CurrentUTC_MS(void) {
#define FILETIME_EPOCH 50491123200ULL
#define FILETIME_UNIX_EPOCH 11644473600ULL
#define FileTime_TotalSecs(time) ((time / 10000000) + FILETIME_EPOCH)
#define FileTime_UnixTime(time) ((time / 10000000) - FILETIME_UNIX_EPOCH)
TimeMS DateTime_CurrentUTC(void) {
FILETIME ft;
cc_uint64 raw;
GetSystemTimeAsFileTime(&ft);
/* in 100 nanosecond units, since Jan 1 1601 */
raw = ft.dwLowDateTime | ((cc_uint64)ft.dwHighDateTime << 32);
return FileTime_TotalMS(raw);
return FileTime_TotalSecs(raw);
}
void DateTime_CurrentLocal(struct DateTime* t) {

View File

@ -39,16 +39,16 @@ void Platform_Log(const char* msg, int len) {
OutputDebugStringA(tmp);
}
#define FILETIME_EPOCH 50491123200000ULL
#define FILETIME_UNIX_EPOCH 11644473600LL
#define FileTime_TotalMS(time) ((time / 10000) + FILETIME_EPOCH)
#define FileTime_UnixTime(time) ((time / 10000000) - FILETIME_UNIX_EPOCH)
TimeMS DateTime_CurrentUTC_MS(void) {
#define FILETIME_EPOCH 50491123200ULL
#define FILETIME_UNIX_EPOCH 11644473600ULL
#define FileTime_TotalSecs(time) ((time / 10000000) + FILETIME_EPOCH)
#define FileTime_UnixTime(time) ((time / 10000000) - FILETIME_UNIX_EPOCH)
TimeMS DateTime_CurrentUTC(void) {
LARGE_INTEGER ft;
KeQuerySystemTime(&ft);
/* in 100 nanosecond units, since Jan 1 1601 */
return FileTime_TotalMS(ft.QuadPart);
return FileTime_TotalSecs(ft.QuadPart);
}
void DateTime_CurrentLocal(struct DateTime* t) {

View File

@ -41,11 +41,10 @@ void Platform_Log(const char* msg, int len) {
puts(tmp);
}
#define UnixTime_TotalMS(time) ((cc_uint64)time.tv_sec * 1000 + UNIX_EPOCH + (time.tv_usec / 1000))
TimeMS DateTime_CurrentUTC_MS(void) {
TimeMS DateTime_CurrentUTC(void) {
struct timeval cur;
gettimeofday(&cur, NULL);
return UnixTime_TotalMS(cur);
return (cc_uint64)cur.tv_sec + UNIX_EPOCH_SECONDS;
}
void DateTime_CurrentLocal(struct DateTime* t) {

View File

@ -476,7 +476,7 @@ static void InjectEntropy(SSLContext* ctx) {
#endif
static void SetCurrentTime(SSLContext* ctx) {
cc_uint64 cur = DateTime_CurrentUTC_MS() / 1000;
cc_uint64 cur = DateTime_CurrentUTC();
uint32_t days = (uint32_t)(cur / 86400) + 366;
uint32_t secs = (uint32_t)(cur % 86400);

View File

@ -67,7 +67,7 @@ int Ping_NextPingId(void) {
head = (head + 1) % Array_Elems(ping_entries);
ping_entries[head].id = next;
ping_entries[head].sent = DateTime_CurrentUTC_MS();
ping_entries[head].sent = Stopwatch_Measure();
ping_entries[head].recv = 0;
ping_head = head;
@ -79,25 +79,27 @@ void Ping_Update(int id) {
for (i = 0; i < Array_Elems(ping_entries); i++) {
if (ping_entries[i].id != id) continue;
ping_entries[i].recv = DateTime_CurrentUTC_MS();
ping_entries[i].recv = Stopwatch_Measure();
return;
}
}
int Ping_AveragePingMS(void) {
int i, measures = 0, totalMs = 0;
int i, measures = 0, totalMs;
cc_int64 total = 0;
for (i = 0; i < Array_Elems(ping_entries); i++) {
struct PingEntry entry = ping_entries[i];
if (!entry.sent || !entry.recv) continue;
totalMs += (int)(entry.recv - entry.sent);
total += entry.recv - entry.sent;
measures++;
}
if (!measures) return 0;
/* (recv - send) is time for packet to be sent to server and then sent back. */
/* However for ping, only want time to send data to server, so half the total. */
totalMs = Stopwatch_ElapsedMS(0, total);
/* (recv - send) is average time for packet to be sent to server and then sent back. */
/* However for ping, only want average time to send data to server, so half the total. */
totalMs /= 2;
return totalMs / measures;
}

View File

@ -9,7 +9,7 @@ Copyright 2014-2023 ClassiCube | Licensed under BSD-3
struct Bitmap;
struct StringsBuffer;
/* Represents a particular instance in time in some timezone. Not necessarily UTC time. */
/* NOTE: TimeMS and DateTime_CurrentUTC_MS() should almost always be used instead. */
/* NOTE: TimeMS and DateTime_CurrentUTC() should almost always be used instead. */
/* This struct should only be used when actually needed. (e.g. log message time) */
struct DateTime {
int year; /* Year, ranges from 0 to 65535 */

View File

@ -192,7 +192,7 @@ static void Http_FinishRequest(struct HttpRequest* req) {
Mutex_Lock(processedMutex);
{
req->timeDownloaded = DateTime_CurrentUTC_MS();
req->timeDownloaded = DateTime_CurrentUTC();
RequestList_Append(&processedReqs, req, false);
}
Mutex_Unlock(processedMutex);
@ -205,10 +205,10 @@ static void Http_CleanCacheTask(struct ScheduledTask* task) {
Mutex_Lock(processedMutex);
{
TimeMS now = DateTime_CurrentUTC_MS();
TimeMS now = DateTime_CurrentUTC();
for (i = processedReqs.count - 1; i >= 0; i--) {
item = &processedReqs.entries[i];
if (item->timeDownloaded + (10 * 1000) >= now) continue;
if (now > item->timeDownloaded + 10) continue;
HttpRequest_Free(item);
RequestList_RemoveAt(&processedReqs, i);