I am under the impression that unix_timestamp and from_unixtime Hive functions are 'reverse' of each other.
When I try to convert timestamp string to seconds in Hive:
SELECT unix_timestamp('10-Jun-15 10.00.00.000000 AM', 'dd-MMM-yy hh.mm.ss.MS a');
I get 1418176800.
When I try to convert 1418176800 to timestamp string:
SELECT from_unixtime(1418176800, 'dd-MMM-yy hh.mm.ss.MS a');
I get 10-Dec-14 10.00.00.120 AM, which is obviously not equal to the original.
Can someone explain what's going on? Thanks.
2.2 unix_timestamp() – Gets Unix Epoch time in seconds This returns the Unix epoch time in seconds for a date and time specified in the input string. It also takes an optional pattern that is used to specify the input date string format.
SELECT from_unixtime(unix_timestamp DIV 1000) as new_timestamp from raw_data ... That converts a unix timestamp into a YYYY-MM-DD HH:MM:SS format, then you can use the following functions to get the year, month, and day: SELECT year(new_timestamp) as year, month(new_timestamp) as month, day(new_timestamp) as day ...
If you need the difference in seconds (i.e.: you're comparing dates with timestamps, and not whole days), you can simply convert two date or timestamp strings in the format 'YYYY-MM-DD HH:MM:SS' (or specify your string date format explicitly) using unix_timestamp(), and then subtract them from each other to get the ...
From the language manual:
Convert time string with given pattern to Unix time stamp (in seconds) The result of this function is in seconds.
Your result changes with the milliseconds portion of the date, but the unix functions only support seconds. For example:
SELECT unix_timestamp('10-Jun-15 10.00.00 AM', 'dd-MMM-yy hh.mm.ss a');
1433930400
SELECT from_unixtime(1433930400, 'dd-MMM-yy hh.mm.ss a');
10-Jun-15 10.00.00 AM
Don't use from unix_timestamp as you have in your second query. Additionally your statement has a formatting in it that gives the result where DEC is used in steads of 12. See dd-MM-yy
. Don't specify a format and it should work. See the examples below.
You are however correct that from_unixtime()
and unix_timestamp()
are used to convert back and forth from time string.
select unix_timestamp('2015-04-09 03:04:26') from dual;
results in "1428566666"
select from_unixtime(1428566666) from dual;
results in "2015-04-09 03:04:26"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With