Getting UTC UNIX timestamp in Lua - datetime

An API returns a timestamp as UNIX timestamp at UTC and I would like to know if this timestamp was more than x seconds ago. As expected, this works fine with os.time() - x > timestamp in UTC, but blows up in other timezones.
Unfortunately I can't find a good way solve this in lua.
os.date helpfully has the ! prefix (e.g. os.date("!%H:%M:%S")) to return time at UTC, but it seems that despite the documentation stating it supports all strftime options, this does not support the %s option. I have heard people mention that this is caused by Lua compile time options for a similar issue, but changing these is not possible as the interpreter is provided by the user.

You can use
os.time(os.date("!*t"))
to get the current UNIX epoch.

Ok, so you want the UTC time. Keep in mind that os.time actually knows nothing about timezones, so for example:
os.time(os.date("!*t"))
Will get UTC time and populate table struct.
Will convert table struct according to current timezone to unix timestamp.
So you actually would get your UNIX_TIME - TIMEZONE_OFFSET. If you are in GMT+5 you will get timestamp at UTC-5.
The correct way to do time conversion in lua is:
os.time() -- get current epoch value
os.time{ ... } -- get epoch value for local date/time values
os.date("*t"),os.date("%format") -- get your local date/time
os.date("!*t") or os.date("!%format") -- get UTC date/time
os.date("*t", timestamp),os.date("%format", timestamp) -- get your local date/time for given timestamp
os.date("!*t", timestamp) or os.date("!%format", timestamp) -- get UTC date/time for given timestamp
Kudos to Mons at https://gist.github.com/ichramm/5674287.
If you really need to convert any UTC date to timestamp, there's a good description on how to do this in this question: Convert a string date to a timestamp

os.time() gives you the unix timestamp. The timestamp is seconds since 00:00:00 UTC on 1 January 1970, so it's the same across timezones.
For example, run this code:
print('timestamp', os.time())
print('local hour', os.date("*t").hour)
print('utc hour', os.date("!*t").hour)
Presumably, your local and utc hour are different. Also run it in an online repl. The server's local and utc hour are the same, but both your and the server's timestamp are about the same.

Related

Converting a timestamp with a local timezone displayed in default BigQuery UTC format to actual UTC

I have an external data source automatically inserting data into my BigQuery table, this data source includes a timestamp field which does not have a timezone connected to it, however, I know this timestamp is in the Europe/Amsterdam timezone.
The problem here is that when this timestamp is inserted into BigQuery, BigQuery automatically defaults the timestamp to UTC, which it is not. And in my specific case, I want to convert this timestamp to UTC. However because BigQuery already defaulted the timestamp to UTC (while it is actually Europe/Amsterdam), I cannot easily convert it to the actual UTC timezone.
Is there any way to convert this timestamp, which BigQuery thinks is already UTC, to the actual UTC timezone within a query? I can't just give it a -02:00 offset due to Daylight Savings coming into play which changes this offset from 2 hours to only 1 hour depending on the time of year.
Any help would be appreciated, I have been kind of stuck on this :)
An example of the timestamp in BigQuery would be 2022-09-30 01:23:45 UTC
There is probably a better way but this should work
with
input as (select timestamp("2022-09-30 01:23:45 UTC") as ts)
select
ts,
timestamp(replace(cast(ts as string), '+00', " Europe/Amsterdam")) updated_ts
from input
ts
updated_ts
2022-09-30 01:23:45 UTC
2022-09-29 23:23:45 UTC

Moment isAfter day

So I need to check whether the current time is after a EndDate, which is a date stored as 19:00 UTC. I only need to know if it's the next day.
This is what I'm currently doing
const now = moment();
const tooLate = now.isAfter(moment(EndDate), 'day');
I worry about timezones. I supposedly need this for central time. Is this sufficient? Or do I need to worry about the timezone? As I understand it, moment runs in UTC to start with.
Depends on the format of EndDate. By default moment() will parse in local time, so unless EndDate is a JS Date object, a unix timestamp (e.g. 1318781876406), or in string form with UTC offset (e.g. 2013-02-08 09:30:26.123+07:00) then you will have a problem if it is not in the same timezone as the client.
You can use moment.UTC() to parse in UTC time instead of local time. This should be enough to solve the any formatting problems you might have with EndDate

How does this teradata timestamp with timezone example make sense?

In the teradata documentation it says:
"Suppose an installation is in the PST time zone and it is New Years Eve, 1998-12-31 20:30 local time.
The system TIMESTAMP WITH TIME ZONE for the indicated time is ' 1999-01-01 04:30-08:00 ' internally."
This does not mesh with my understanding. I figure it ought to be '1999-01-01 04:30+00:00' internally because it should be stored in UTC.
Or, it can be stored as a the local time with a -8 offset, but this example seems to mix the two. Perhaps I am misunderstanding the text?
Not sure if this is an answer, but it's too long for a comment.
That "internal" storage part is very misleading. We don't care how Teradata stores anything internally.
I find this easier to look at using BTEQ, since SQL Assistant doesn't show timezones, at least by default. So, assuming you've logged into BTEQ...
--set session timezone to pst (GMT - 8)
SET TIME ZONE INTERVAL -'08:00' HOUR TO MINUTE ;
create volatile table vt_foo (
ts_w_zone timestamp(0) with time zone,
ts_wo_zone timestamp) on commit preserve rows;
insert into vt_foo
select
cast('1998-12-31 20:30:00' as timestamp(0)),
cast('1998-12-31 20:30:00' as timestamp);
select * from vt_foo;
Currently the two values (with and without tz) will match.
ts_w_zone ts_wo_zone
------------------------- --------------------------
1998-12-31 20:30:00-08:00 1998-12-31 20:30:00.000000
Now let's change the timezone for your session to something else, and look at what we get.
SET TIME ZONE INTERVAL -'03:00' HOUR TO MINUTE ;
select * from vt_foo;
ts_w_zone ts_wo_zone
------------------------- --------------------------
1998-12-31 20:30:00-08:00 1999-01-01 01:30:00.000000
The timestamp with zone is still the same. Displaying it without timezone is automatically converting it to your session timezone, which in this example is GMT -3.
EDIT:
Technically, Teradata is actually storing the time with timezone as GMT (1999-01-01 04:30:00) with the timezone offset (-8). That's where the documentation gets the  1999-01-01 04:30-08:00 value from). But that is not how it displays it.

Output DateTime as String with predefined time zone shift

I have a DateTime in my .NET program that I need to print with particular value of time zone offset (for instance, +01:00 always). Output should contain full date time with timezone. It has to be unrelated to system timezone setting. How I could achieve this?
Example: I have a timestamp such as 12-03-2016T12:30:34+03:00 and I need to output it calculated for predefined TZ +1: 12-03-2016T10:30:34+01:00
Found some approach to it.
First of all, DateTime does not have time zone stored in it. Instead it has flag whether it is UTC or Local (without the idea what Local TZ shift is). So: first thing is to get your initial parsing of time from any string time stamp in UTC.
Once it is stored in DateTime object (with Kind=UTC), you have to convert it to the timezone you desire output for. I find examples here useful: datetime to string with time zone.
Note: if you need to convert London daylight-saving time, you have to know right names of timezones in NET so you get it right. See Difference between UTC and GMT Standard Time in .NET

DateTime Json Return From WebAPI with Default Serializer

I know this question has been hashed over multiple times and I read lots of posts on that hashing but still am confused.
Using MVC4/WebAPI, I have a datetime that is simply created as new DateTime.Now.
My WebAPI is return data like this:
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, new
{
data = sessionRecordSmalls,
count = sessionRecordSmalls.Count,
success = true
});
where sessionRecordsSmall has a public property of DateTime in it.
When I look at the date in the VS debugger, it shows it as now with no timezone of course because DateTime does not include a timezone.
{10/6/2012 9:45:00 AM}
When I look at what gets downloaded from the server, I see in the JSON
2012-10-06T09:45:00
I think the T0 means Timezone 0, not 100% sure of that. My JavaScript library interprets it as timezone 0, then shows the actual date downloaded as GMT (-9 hours ago for me).
My question is, what is the JSON downloaded? Is that include a timezone? Am I missing some important step here?
if serializing with json.net keep in mind that you can specify DateTimeZoneHandling.
Example in WebApiConf.cs
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.DateTimeZoneHandling =Newtonsoft.Json.DateTimeZoneHandling.Local;
The date time 2012-10-06T09:45:00, which we recive in JSON with Web API and default serializer is the ISO 8601 format.
In fact this is so called Combined date and time representations. Extract:
..A single point in time can be represented by concatenating a
complete date expression, the letter T as a delimiter, and a valid
time expression. For example "2007-04-05T14:30"...
There is no time zone information in this format. As mentioned in the Time zone designators Extract:
Time zones in ISO 8601 are represented as local time (with the
location unspecified), as UTC, or as an offset from UTC. If no UTC
relation information is given with a time representation, the time is
assumed to be in local time.
In other words, if there is no offset from UTC specified, it is treated as a local time.
The UTC format would be extended with Z at the end
If the time is in UTC, add a Z directly after the time without a
space. Z is the zone designator for the zero UTC offset. "09:30 UTC"
is therefore represented as "09:30Z" or "0930Z". "14:45:15 UTC" would
be "14:45:15Z" or "144515Z".
UTC time is also known as 'Zulu' time, since 'Zulu' is the NATO phonetic alphabet word for 'Z'.
So, the date-time we recieve is the ISO 8601 format, treated as local time zone (no Z at the end like this 2012-10-06T09:45:00Z)

Resources