简体   繁体   English

需要从 hive 中的给定时间戳中减去一些小时

[英]Need to subtract some hours from given timestamp in hive

Input: unix_timestamp('01/15/2018 15:26:37', 'mm/dd/YYYY hh:mm:ss')输入:unix_timestamp('01/15/2018 15:26:37', 'mm/dd/YYYY hh:mm:ss')

Expected output is 4 hours delay from above utc input time ie 01/15/2018 11:26:37预计 output 比 utc 输入时间延迟 4 小时,即 01/15/2018 11:26:37

I know that there is date_sub function in hive but it is only used to subtract days from the given timestamp.我知道 hive 中有 date_sub function 但它仅用于从给定时间戳中减去天数。 But I need to know if there is a way by which I can subtract hours or minutes or seconds.但我需要知道是否有一种方法可以减去小时、分钟或秒。

I have also tried something like below as EDT timezone is 4 hours behind UTC (but getting wrong output):我也尝试过类似下面的内容,因为 EDT 时区比 UTC 晚 4 小时(但输出错误):

SELECT to_date(from_UTC_timestamp(unix_timestamp('01/15/2018 15:26:37', 'mm/dd/YYYY hh:mm:ss')*1000, 'EST6EDT')) as earliest_date; -- OUTPUT: 2017-12-31 (wrong) 

So can anyone help me out with this?那么有人可以帮我解决这个问题吗?

它工作正常。

select from_unixtime(unix_timestamp('01/15/2018 15:26:37', 'MM/dd/yyyy HH:mm:ss')-4*3600, 'MM/dd/yyyy HH:mm:ss') 

In addition to the answer of @StrongYoung . 除了@StrongYoung的答案

I find it very useful to define such long expressions as a macros and place in the initialization file (eg hive -i init-file.hql ... ). 我发现将这样的长表达式定义为宏并放置在初始化文件中非常有用(例如hive -i init-file.hql ... )。

hive> create temporary macro sub_hours(dt string, hours int)
from_unixtime(unix_timestamp(dt, 'MM/dd/yyyy HH:mm:ss') - 3600 * hours, 'MM/dd/yyyy HH:mm:ss');
OK
Time taken: 0.005 seconds
hive> select sub_hours("01/01/2019 00:00:00", 5);
OK
12/31/2018 19:00:00
Time taken: 0.439 seconds, Fetched: 1 row(s)

Macros are available starting from Hive 0.12.0, further details can be found here (pay attention to the "bug fixes" section). 宏可以从Hive 0.12.0开始提供,更多详细信息可以在这里找到(注意“错误修复”部分)。

Eg Your current time in your specific timezone - 1 hour:例如,您在特定时区的当前时间 - 1 小时:

select date_format(
        from_utc_timestamp(CURRENT_TIMESTAMP(),'Europe/Madrid') - INTERVAL 1 hours,
        'yyyy-MM-dd HH:mm:ss')
     as PREVIOUS_HOUR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用PHP从时间戳中减去6小时 - Subtract 6 Hours From timestamp Using PHP 获取当前MYSQL时间戳并从24小时减去它 - Take Current MYSQL Timestamp And Subtract It From 24 Hours 使用 mysql 客户端从 mysql 中的时间戳记录中减去某些小时的最佳方法 - Best way to subtract certain hours from timestamp records in mysql using mysql client 我需要用 PHP 时间戳减去 mySQL 时间戳。 获取“date_diff() 期望参数 1 为 DateTimeInterface,给定数组”错误 - I need to subtract a mySQL timestamp with a PHP timestamp. Getting a “date_diff() expects parameter 1 to be DateTimeInterface, array given” error 使用mysql从时间戳中减去日期时间 - subtract a datetime from a timestamp with mysql 如何减去两个sql时间戳字段然后以小时为单位返回时差 - How to subtract two sql timestamp fields then return time difference in hours 如何从 MySQL 中的日期时间中减去小时数? - How to subtract hours from a datetime in MySQL? 如何从 mysql 中的时间戳中减去 1 天 - How to subtract 1 day from a timestamp in mysql 如何从当前时间戳中减去秒数 - How to subtract seconds from current timestamp 如果在夏令时之前,我如何编写一个 case 语句从日期时间减去 5 小时,如果在之后减去 4 小时 - How do i write a case statement to subtract 5 hours from datetime if it's before day light saving and subtract 4 hours if after
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM