I am struggling to understand how Hive deals with dates and timezones:
select item_id, cal_dt from ds where item_id = 7208582515 and cal_dt='2019-02-11';
7208582515 2019-02-10
I know it has to be a timezone issue. It's 11 pm PDT right now (same as the Hadoop cluster time, I think) but Hive (1.2) is running on a node without any timezone (UTC).
Previously, I had a bigger issue where a query with this condition:
next_dt = date_add(cal_dt,1)
It didn't work on the clock change dates. I think that was because my TZ was set to America/Los_Angeles so I unset it.
I don't know what's going on. I didn't expect dates to be affected by the timezone. What's the right thing to do?
Related
I have a query that returns a date as one of the column however I need that date to be in a different time zone then it currently is.
SELECT ISNULL(Value, 'NULL') AS VALUE, ISNULL(UPPER(FORMAT(start_date), 'MMM dd, yyyy hh:mm ')), 'NULL') as Start_Date FROM MY TABLE
I tried just to off set by 1 hour to make the time as in central time (currently its EST)
DATEADD(hh, -1, start_date)
the the problem comes when the date is inside daylight saving time and all returned dates within that time fate are being offest by 1 hour
Is there a way to convert that start_date column to accommodate for it?
First, I'll point out that SQL Server 2014 reached end of mainstream support on July 9th 2019. You really shouldn't be using it in a production environment any more.
If you must continue using it, then you can consider using my SQL Server Time Zone Support project. I wrote it a long time ago, and no longer maintain it, so like your server - use at your own risk.
This SQL:
select to_timestamp(extract(epoch from '0001-01-01 00:00:00'::timestamp))
produces this output:
0001-01-01 08:06:00+08:06
I realize that to_timestamp() always adds a time zone, hence the additional 8 hours and +8 in the time zone segment. But what is the :06? And where did the extra 6 minutes come from?
EDIT
If I initially execute set local timezone to 'UTC'; then I get expected results.
Before UTC was invented, each city had its own local time, mostly with a difference of just some minutes among each other.
Just after standardization of timezones (and the respective adoption by everybody), the local times were set to the values we know today.
That's why you get these strange results for ancient dates, specially before the year 1900.
Actually, Taipei changed from UTC+08:06 to UTC+08:00 only in Jan 1st of 1896, so dates before it will have the +08:06 offset.
If you set your timezone to UTC this doesn't happen, basically because UTC's offset is zero and never changes.
We have just changed timezone, and it gives me a problem with database
(I am now in the timezone GMT+2).
I have to check when something starts and I use this query :
SELECT *,extract(epoch from start at time zone 'cet') as start_, extract(epoch from stop at time zone 'cet') as stop_ from czas;
and it's wrong, because my start_/stop_ is 1 hour older : i.e that should be 16 instead of 17 and 'cet' should be replaced by 'cest'.
I found QTimeZone class, which should display current short zone name (cet/cest), but when I use it like this :
QDateTime now = QDateTime::currentDateTime();
QTimeZone zone_;
qDebug()<<"ZONE: "<<zone_.displayName(now,QTimeZone::ShortName);
I get an empty string.
Does anyone have an idea why?
I can't answer why QT isn't doing what you expect.
Perhapse I can help with solving the issue you started with which is the issue of using the correct timezone? For this PostgreSQL offers a number of timezone names which are set to the country and not the number of hours offset. For example 'Europe/London' specifies 'GMT' for winter timestamps and 'BST' for summer timestamps.
The list of these has been removed in the 9.x manual but in the 8.1 manual the list can be found here:
http://www.postgresql.org/docs/8.1/static/datetime-keywords.html
For your system use the following to get a complete list of timezone names:
select * from pg_timezone_names;
I don't know which of these is most suited to your application but for example your code could be changed to:
SELECT *,extract(epoch from start time zone 'Europe/Paris') as start_, extract(epoch from stop at time zone 'Europe/Paris') as stop_ from czas;
I have two data base in two differents machines with the same schemas, tables and data. I launch this query:
select mydate from mytable where date = '2013-10-03 14:25:00-07'::timestamp::date
the first machine return the correct rows and the second one doesn´t, both machines has the same prostgres version (9.2)
the only different between the machines is that first one works on windows and the second one on Linux (Centos)
Any suggestion?
'2013-10-03' can be interpreted as Oct, 3rd or March, 10th, depending on your datestyle setting. #Chris has more on that.
In addition to that, your query is generally incorrect. This expression is misleading:
'2013-10-03 14:25:00-07'::timestamp
timestamp in Postgres defaults to timestamp without time zone, which doesn't recognize time zone offsets. Therefore, the time zone offset -07 is discarded.
Use instead:
'2013-10-03 14:25:00-07'::timestamptz
Match the point in time:
SELECT * FROM mytable
WHERE mydate = '2013-10-03 14:25:00-07'::timestamptz
Does not depend on your local time zone setting, since the data type of the column is timestamp with time zone as you clarified in a later comment.
Match the day:
...
WHERE mydate::date = '2013-10-03 14:25:00-07'::timestamptz::date
Depends on your local time zone setting, which defines lower and upper borders of the "day".
Detailed explanation in this related answer:
Ignoring timezones altogether in Rails and PostgreSQL
The cleanest solution would be to, at the beginning of the session, just issue the following command:
SET datestyle = "ISO, YMD";
This will ensure properly handling the timestamp according to your input format.
I have a requirement to disregard records from the DB that are more than 10 minutes old. However, the DB server is present in a different time zone than the app server. I tried to leverage the time zone details from the timestamp column value but it seems that they do not store the time zone details in that column value (bad design?). However, i have found a way to get this information for the DB instance using a query:
select dbtimezone from dual.
However, most of the implementations in java support time zones via names and not offset information. I need to be able to translate this offset exactly to a timezone (EST etc) so that i may not miss any DST related time in my calculations. like so:
TimeZone dbZone = TimeZone.getTimeZone(10000); // offset is +10000
Calendar cal = Calendar.getInstance(dbZone);
cal.setTime(new Date());
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setTimeZone(dbZone);
Date parsedDate = df.parse(df.format(cal.getTime()));
The plan is to convert the present client/app time to the DB specific timezone time and perform the difference between the two.
This cannot be done in a query due to some restraints. Please do not ask me to write a query to get latest records etc. Must be done in Java.
Any tips?
I am guessing it might get you in the right direction. You can try the following so you know the offset from EST and can do the calculation accordingly:
SELECT TZ_OFFSET('US/Eastern') FROM DUAL;
So a return of -3:00 would mean it is PST time. Maybe you've already tried this?