C equivalent to playdate.getTime()

I was looking into using the real-time clock in my C-only project, but I noticed there is no way to get the current device time in the C API. Unlike many of the Lua exclusive SDK functions, there is no way to implement playdate.getTime() myself. I know you can call Lua functions from C, but it seems very complicated for something that should be easy to access. It would be very useful to have this function available to C projects!

Edit: I realized that playdate->system->getSecondsSinceEpoch() is based on the device clock, which would allow me to convert it to the current date and time. I am still figuring out how to do that, but it looks like it is actually possible to do it purely in C.

1 Like

getSecondsSinceEpoch() has no knowledge of time zone, so it does look like we're missing a way to query the system clock. +1 for this request.

Yes, it seems you are right about the time zone. Even when using localtime_r instead of gmtime_r to convert the Epoch time it is still converted to GMT. Here is the code I used to get the current GMT time on device if anyone is trying to figure it out:

#include <time.h>
//...
// Store the current time in timeResult struct
time_t secondsSinceEpoch = (time_t)(pd->system->getSecondsSinceEpoch(NULL) + 946684800);
struct tm timeResult;
gmtime_r(&secondsSinceEpoch, &timeResult);

// Print the current time
char buffer[26];
asctime_r(&timeResult, buffer);
pd->system->logToConsole("%s", buffer);
1 Like

Yeah, this looks like something we forgot to add. Thanks for letting us know!

2 Likes