Skip to content

Commit b10c9c2

Browse files
committed
Add VM syscall for getting local time offset
This is needed to get the local time since an NaCl VM can't read the time zone database. Returns an offset instead of just the local date, in the interests of not doing a trap call every frame.
1 parent 5d2d092 commit b10c9c2

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

src/common/IPC/CommonSyscalls.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ namespace VM {
154154
enum EngineMiscMessages {
155155
CREATE_SHARED_MEMORY,
156156
CRASH_DUMP,
157+
GET_LOCAL_TIME_OFFSET,
157158
};
158159

159160
// CreateSharedMemoryMsg
@@ -165,6 +166,11 @@ namespace VM {
165166
using CrashDumpMsg = IPC::SyncMessage<
166167
IPC::Message<IPC::Id<MISC, CRASH_DUMP>, std::vector<uint8_t>>
167168
>;
169+
// GetLocalTimeOffsetMsg
170+
using GetLocalTimeOffsetMsg = IPC::SyncMessage<
171+
IPC::Message<IPC::Id<MISC, EngineMiscMessages::GET_LOCAL_TIME_OFFSET>>,
172+
IPC::Reply<int>
173+
>;
168174

169175
enum VMMiscMessages {
170176
GET_NETCODE_TABLES,

src/engine/framework/CommonVMServices.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,21 @@ namespace VM {
5959
Sys::NaclCrashDump(dump, vmName);
6060
});
6161
break;
62+
63+
case GET_LOCAL_TIME_OFFSET:
64+
IPC::HandleMsg<GetLocalTimeOffsetMsg>(channel, std::move(reader), [this](int& offset) {
65+
time_t epochTime = time(nullptr);
66+
struct tm localDate;
67+
#ifdef _WIN32
68+
localtime_s(&localDate, &epochTime);
69+
time_t localEpochTime = _mkgmtime(&localDate);
70+
#else
71+
localtime_r(&epochTime, &localDate);
72+
time_t localEpochTime = timegm(&localDate);
73+
#endif
74+
offset = static_cast<int>(difftime(localEpochTime, epochTime));
75+
});
76+
break;
6277
}
6378
}
6479

src/shared/CommonProxies.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,14 @@ namespace VM {
435435
SendMsg<CrashDumpMsg>(std::vector<uint8_t>{data, data + size});
436436
}
437437

438+
// Add this number of seconds to the UTC date/time to get the local date/time.
439+
// The current daylight savings time adjustment is included in this.
440+
int GetLocalTimeOffset() {
441+
int offset;
442+
SendMsg<GetLocalTimeOffsetMsg>(offset);
443+
return offset;
444+
}
445+
438446
void InitializeProxies(int milliseconds) {
439447
baseTime = Sys::SteadyClock::now() - std::chrono::milliseconds(milliseconds);
440448
Cmd::InitializeProxy();

src/shared/CommonProxies.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ namespace Cmd {
4343
namespace VM {
4444

4545
void CrashDump(const uint8_t* data, size_t size);
46+
int GetLocalTimeOffset();
4647
void InitializeProxies(int milliseconds);
4748
void HandleCommonSyscall(int major, int minor, Util::Reader reader, IPC::Channel& channel);
4849

0 commit comments

Comments
 (0)