Convert string with timezone to unix timestamp - hive

I'm trying to convert a string with a specific timezone (not UTC) to an unix timestamp.
Conversion from strings in UTC works pretty well with unix_timestamp function:
hive> select unix_timestamp("2018-12-31 23:59:59 UTC", "yyyy-MM-dd HH:mm:ss z") as unixtime;
unixtime
1546300799
However, when I simply change the timezone (to another valid TZ name) it doesn't work:
hive> select unix_timestamp("2018-12-31 23:59:59 America/Sao_Paulo", "yyyy-MM-dd HH:mm:ss z") as unixtime;
unixtime
NULL
Any suggestions?

Your problem is the timezone itself, my understanding is that there is no direct way to do this using java cannonical id ("America/Sao_Paulo"). The correct format for the date that you are looking for (in full) should be
select unix_timestamp("2018-12-31 23:59:59 Brasilia Summer Time", "yyyy-MM-dd HH:mm:ss zzzz") as unixtime;

Related

How to adjust timestamp timezone offset for YYYY-MM-DD''T''HH:mm:ss.SSSZ format timestamp in presto?

I have a dataset with timestamp strings like '2022-05-25T13:31:22.566-0400' I would like to convert it to '%Y-%m-%dT%H:%i:%s' format but adjusting for the timezone difference.
So for the above, how convert '2022-05-25T13:31:22.566-0400' to '2022-05-25T17:31:22.566' in Presto?
Thanks a lot!
You can use from_iso8601_timestamp to parse date, then cast to timestamp to remove timezone info (will be treated as UTC, at time zone 'UTC' should have the same effect) and use date_format to get required output format:
select date_format(
cast(from_iso8601_timestamp('2022-05-25T13:31:22.566-0400') as timestamp),
'%Y-%m-%dT%H:%i:%s')
Or
select date_format(
from_iso8601_timestamp('2022-05-25T13:31:22.566-0400') at time zone 'UTC',
'%Y-%m-%dT%H:%i:%s')
Output:
_col0
2022-05-25T17:31:22

BigQuery: Convert DateTime in different time zone to UTC

I'm new to Big Query,
I want to convert the following date-time to UTC timezone.
DateTime format "2021-07-03T23:59:00+04:00[Asia/Dubai]" to UTC.
Thank you.
You just need to convert it to TIMESTAMP() without specifying any timezone (since it automatically sets the datetime at UTC) and then convert it back to DATETIME():
SELECT
your_datetime,
DATETIME(TIMESTAMP(your_datetime)) AS datetime_utc
FROM
<your_table>
OUTPUT:
your_datetime datetime_utc
------------------------- -------------------
2021-07-03T23:59:00+04:00 2021-07-03T19:59:00

PostgreSQL convert date with timezone to a timestamp

I have date stored in format DD/MM/YYYY HH24:MI TZ (datetime with timezone) in my psql database. I need to convert this date to a timestamp.
I tried to_timestamp() but it is not working with timezone.
ERROR: "TZ"/"tz"/"OF" format patterns are not supported in to_date
I tried ::timestamptz but it consider date to be in format of MM/DD/YYYY HH24:MI TZ
ERROR: date/time field value out of range
Is there any way to convert the format from DD/MM/YYYY HH24:MI TZ to MM/DD/YYYY HH24:MI TZ or convert DD/MM/YYYY HH24:MI TZ to timestamp?
e.g. "28/04/2017 13:00 +2:30"
try casting directly?..
so=# select to_timestamp('04/28/2017 13:00 +2:30','D/MM/YYYY HH24:MI TZ');
ERROR: "TZ"/"tz"/"OF" format patterns are not supported in to_date
Time: 20.850 ms
so=# select '04/28/2017 13:00 +2:30'::timestamptz;
timestamptz
------------------------
2017-04-28 10:30:00+00
(1 row)
Time: 0.554 ms
https://www.postgresql.org/docs/current/static/functions-formatting.html
to_timestamp and to_date exist to handle input formats that cannot be
converted by simple casting. For most standard date/time formats,
simply casting the source string to the required data type works, and
is much easier.
update
regarding your comment, you just need to adjust datestyle to change the parsing:
so=# set datestyle to DMY;
SET
Time: 9.997 ms
so=# select '04/28/2017 13:00 +2:30'::timestamptz;
ERROR: date/time field value out of range: "04/28/2017 13:00 +2:30"
LINE 1: select '04/28/2017 13:00 +2:30'::timestamptz;
^
HINT: Perhaps you need a different "datestyle" setting.
Time: 10.217 ms
so=# set datestyle to MDY;
SET
Time: 8.799 ms
so=# select '04/28/2017 13:00 +2:30'::timestamptz;
timestamptz
------------------------
2017-04-28 10:30:00+00
(1 row)

Postgres SQL Timezone conversion

I could appreciate a second pair of eyes on my Postgres syntax.
Database stores timestamp in UTC. I'm trying to convert from UTC to Eastern Daylight Time EDT, but the output is not accurate.
Here's my syntax:
SELECT
to_char(((timestamp AT TIME ZONE 'UTC') AT TIME ZONE 'EDT'), 'MM/DD/YYYY HH24:MI')
FROM table_name
Record TimeStamp:
09/10/2016 12:00
Query Output:
09/10/2016 16:00
Desired Output:
09/10/2016 08:00
Thanks for your assistance.
Saying AT TIMEZONE twice is redundant since it will just convert from whatever the current timezone (which you suggest is UTC) to UTC then to EDT.
The fact that you feel the need to convert it to UTC tells me you're not storing it as a TIMESTAMP WITH TIMEZONE. Check if this is the case. If it is, that's likely your problem. From the docs:
If no time zone is stated in the input string, then it is assumed to be in the time zone indicated by the system's timezone parameter, and is converted to UTC using the offset for the timezone zone.
Basically, if you don't specify, it assumes it's from your current timezone by default, not UTC. It's possible you entered a UTC timestamp and it assumed it was EDT.

Convert a date/time from UTC to session time zone

My program receives timestamps in UTC, e.g. Tue, 31 May 2016 11:43:47 UTC. How do I convert them to a timestamp in the session's current timezone?
First convert the string to a timestamp with timezone (use the correct function). To show the result in your local (session) time zone, use the "AT LOCAL" clause.
select to_timestamp_tz('Tue, 31 May 2016 19:43:47 UTC',
'Dy, dd Mon yyyy hh24:mi:ss tzr') at local from dual;
Result (using my front-end settings for displaying timestamp with timezone - yours may be different):
31-MAY-16 02.43.47.000000000 PM AMERICA/CHICAGO
Use the tz_offset function to get the timezone offset for the session's timezone, then use the from_tz function to convert the string to a timestamp with time zone, and use at time zone to change it to use the session's timezone.
select from_tz(to_timestamp('Tue, 31 May 2016 11:43:47 UTC'
,'Dy, dd Mon yyyy hh24:mi:ss "UTC"')
, 'UTC')
at time zone tz_offset(sessiontimezone)
,sessiontimezone
from dual;
31/05/2016 19:43:47.000000000 +08:00 Australia/Perth