# Add a utc\_now() function?

**URL:** <https://forum.defold.com/t/add-a-utc-now-function/6404>\
**Category:** Feature requests (USE GITHUB)\
**Created:** [March 17, 2017, 3:19am UTC](https://forum.defold.com/t/add-a-utc-now-function/6404 "2017-03-17T03:19:54Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericsroy](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ericsroy/32/2734_2.png) [@ericsroy](https://forum.defold.com/u/ericsroy)\
**Post date:** [March 17, 2017, 3:19am UTC](https://forum.defold.com/t/add-a-utc-now-function/6404/1 "2017-03-17T03:19:54Z")

</div>

You know, it would seriously make my day if you guys added something like a `utc_now()` function that could be used as an alternative to the horrible builtin lua time apis.

All I want is a simple function for getting a utc timestamp, with no buggering about with timezones. Maybe getting better than 1-second precision would be nice too. Maybe you could slip such a function into the `sys` module?

Look at how difficult this is in pure lua:  
[http://lua.2524044.n2.nabble.com/os-time-and-timezones-td7461931.html](http://lua.2524044.n2.nabble.com/os-time-and-timezones-td7461931.html)

Honestly, the implementations that I’ve found are complex enough that I can’t even convince myself that they work in all cases…

And before anyone asks, I need absolute time, not just relative time values that can be used to calculate deltas (such as `socket.gettime()`).

Ultimately, I may decide that having a trusted timestamp is necessary, in which case I might have to do a request to an ntp server or something, though it would be nice to not rely on having an internet connection.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [March 17, 2017, 6:51am UTC](https://forum.defold.com/t/add-a-utc-now-function/6404/2 "2017-03-17T06:51:41Z")

</div>

I’m guessing that this could be done using a native extension as well, once we have support for all platforms. Do you happen to know the platform specific functions involved?

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [March 17, 2017, 8:00am UTC](https://forum.defold.com/t/add-a-utc-now-function/6404/3 "2017-03-17T08:00:26Z")

</div>

```lua
function utc_now()
	return os.time(os.date("!*t"));
end

print(utc_now())

```

Can someone who knows Lua better explain why this is bad?

> in which case I might have to do a request to an ntp server or something

If time is super important and system time can’t be trusted this seems like the right thing to do.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [March 17, 2017, 8:43am UTC](https://forum.defold.com/t/add-a-utc-now-function/6404/4 "2017-03-17T08:43:16Z")

</div>

I’ve never looked into the formatting options for os.date(), but it seems as though “!\*t” is what @ericsroy is after. ! = Coordinated Universal Time and \*t = table with data parameters, and os.time() converts this into a timestamp.

But, judging from the link provided by ericsroy it seems as though the whole thing might have to be more complex than what you suggested.

BTW, what kind of app/game are you creating @ericsroy?

---

<div class="post-metadata">

**Author:** ![ericsroy](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ericsroy/32/2734_2.png) [@ericsroy](https://forum.defold.com/u/ericsroy)\
**Post date:** [March 17, 2017, 2:44pm UTC](https://forum.defold.com/t/add-a-utc-now-function/6404/5 "2017-03-17T14:44:58Z")

</div>

@Pkeod Actually, from what i’ve read, that solution isn’t quite right. Apparently, `os.date("!*t")` will give you utc time, but `os.time` always assumes that the provided time table is in your current time zone! Totally nuts, I know… Anyway, you might want to edit your post with a warning to that effect so people don’t copy-paste that solution into their games.

@britzl Yes, native extensions could provide me with a solution eventually. Not sure about the appropriate apis on all platforms, but for anyone interested, here’s how I usually get a utc timestamp (in milliseconds) on windows:

```lua
// FILETIME corresponding to unix epoch
// This constant came from the postgres source code.
static const uint64_t unixEpochOffset = 116444736000000000;

FILETIME ft = {};
GetSystemTimeAsFileTime(&ft);

ULARGE_INTEGER largeInt;
largeInt.HighPart = ft.dwHighDateTime;
largeInt.LowPart = ft.dwLowDateTime;

int64_t utcNowMs = (int64_t)((largeInt.QuadPart - unixEpochOffset) / 10000ull);

```

I’m assuming it’s far simpler than that on more unixy platforms, or in languages with good time apis.

I’m working on a idle/passive game, so it’s important that I be able to accurately measure the time since the app last ran, so I can fast-forward my simulation and provide the user with the rewards that have accrued since they’ve been away. If I worked in the local timezone, I might get bitten by daylight savings time.

And of course, you can imagine how a user might manipulate their device time to try to get more rewards - I’ll need to decide how much I care about this.

---

<div class="post-metadata">

**Author:** ![Tomires](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/tomires/32/3885_2.png) [@Tomires](https://forum.defold.com/u/Tomires)\
**Post date:** [March 17, 2017, 3:20pm UTC](https://forum.defold.com/t/add-a-utc-now-function/6404/6 "2017-03-17T15:20:36Z")

</div>

If I were making a game that depends on time (either some casual clicker stuff or a mechanic that provides periodic rewards to a single-player game), I would probably utilise NTP servers with a fallback to device time in case the user doesn’t have an internet connection. I assume this would stop people who aren’t tech-savvy from cheating the system.

I have also devised a pretty neat trick that might be applicable to this scenario - how about letting the player reap rewards either way, but when they gain internet access run a routine that compares the last time they’ve played the game with current time provided by an NTP server? And afterwards, if the time on file is ahead of network time, punish them somehow 😃

---

<div class="post-metadata">

**Author:** ![ericsroy](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ericsroy/32/2734_2.png) [@ericsroy](https://forum.defold.com/u/ericsroy)\
**Post date:** [March 17, 2017, 3:27pm UTC](https://forum.defold.com/t/add-a-utc-now-function/6404/7 "2017-03-17T15:27:44Z")

</div>

Yes, I think you’re right, probably ntp with fallback to device is the way to go.

As for punishing them, I’m not sure… what if they legitimately changed their device time for some reason? (just got off a plane flight, etc). Since my players won’t be competing against one another, I think letting them cheat is probably fine. I still like the idea of a trusted NTP time though, just so the game doesn’t seem broken if the device time changes.

Maybe I’ll implement an NTP time checker using lua socket and share it with people here…

---

<div class="post-metadata">

**Author:** ![Tomires](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/tomires/32/3885_2.png) [@Tomires](https://forum.defold.com/u/Tomires)\
**Post date:** [March 17, 2017, 3:31pm UTC](https://forum.defold.com/t/add-a-utc-now-function/6404/8 "2017-03-17T15:31:26Z")

</div>

Good point. How about setting some bounds, e.g. if the time is ahead by more than a day or two?

Can you think of any other legitimate reasons on why to change device time other than simply moving between different timezones?

---

<div class="post-metadata">

**Author:** ![ericsroy](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ericsroy/32/2734_2.png) [@ericsroy](https://forum.defold.com/u/ericsroy)\
**Post date:** [March 17, 2017, 3:32pm UTC](https://forum.defold.com/t/add-a-utc-now-function/6404/9 "2017-03-17T15:32:59Z")

</div>

Yeah - you wouldn’t expect more than a handful of hours difference for normal use cases. Good suggestion

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [March 17, 2017, 4:51pm UTC](https://forum.defold.com/t/add-a-utc-now-function/6404/10 "2017-03-17T16:51:50Z")

</div>

> [@ericsroy](#):
>
> here’s how I usually get a utc timestamp (in milliseconds) on windows:

You should be able to use [LuaJIT’s FFI feature](http://luajit.org/ext_ffi.html) to call this code directly from Lua without going through a native extension.

---

<div class="post-metadata">

**Author:** ![ericsroy](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ericsroy/32/2734_2.png) [@ericsroy](https://forum.defold.com/u/ericsroy)\
**Post date:** [March 17, 2017, 4:56pm UTC](https://forum.defold.com/t/add-a-utc-now-function/6404/11 "2017-03-17T16:56:24Z")

</div>

What about other platforms? Is it kosher to call native functions via the FFI on iOS for example?

---

<div class="post-metadata">

**Author:** ![ericsroy](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ericsroy/32/2734_2.png) [@ericsroy](https://forum.defold.com/u/ericsroy)\
**Post date:** [March 17, 2017, 7:15pm UTC](https://forum.defold.com/t/add-a-utc-now-function/6404/12 "2017-03-17T19:15:13Z")

</div>

Here’s a module I wrote for doing NTP time checks with luasocket:

> <https://gist.github.com/ericroy/453ecae379d1e1acdc2311d3b6bf49f0>
