Search code examples
hivetimestamphiveqlutcunix-timestamp

Hive unix_timestamp is not recognizing difference between 12:07 and 00:07, and returns same Unix timestamp for both


hive to_unix_timestamp() function is not recognizing the 24hr format and returns the same value in both cases of 12th hour and zero hour.
Example query below:

select to_unix_timestamp( '2019-01-22T12:07:00.000+0000',"yyyy-MM-dd'T'hh:mm:ss.SSSZ") 12thhour,to_unix_timestamp( '2019-01-22T00:07:00.000+0000',"yyyy-MM-dd'T'hh:mm:ss.SSSZ") zerohour;

12thhour          zerohour
1548115620        1548115620

select to_unix_timestamp( '2019-01-22T12:07:00',"yyyy-MM-dd'T'hh:mm:ss") as 12thhour ,to_unix_timestamp( '2019-01-22T00:07:00',"yyyy-MM-dd'T'hh:mm:ss") as Zerohour ;

12thhour          zerohour
1548112020       1548112020

Solution

  • Use 'HH' template for 24 hours format. Lower-case 'hh' is for 12-hour.

    Test 24 hrs format:

    hive>  select unix_timestamp( '2019-01-22T12:07:00.000+0000',"yyyy-MM-dd'T'HH:mm:ss.SSSZ"), unix_timestamp( '2019-01-22T00:07:00.000+0000',"yyyy-MM-dd'T'HH:mm:ss.SSSZ") zerohour;
    OK
    1548158820      1548115620
    

    Test 12 hrs format:

    hive>  select unix_timestamp( '2019-01-22T12:07:00.000+0000',"yyyy-MM-dd'T'hh:mm:ss.SSSZ"), unix_timestamp( '2019-01-22T00:07:00.000+0000',"yyyy-MM-dd'T'hh:mm:ss.SSSZ") zerohour;
    OK
    1548115620      1548115620
    

    Read about patterns here: simpleDateFormat