简体   繁体   English

如何在Python中添加一分钟到Hive时间戳格式

[英]How to add a minute in Python to Hive timestamp format

I wanted to add a minute to the date which is in the Hive timestamp format of 'YYYY-MM-DD HH24:MI:SS.FF6' using Python 我想用Python添加一分钟到Hive时间戳格式为'YYYY-MM-DD HH24:MI:SS.FF6'的日期

Example:1) 2015-01-15 08:59:16 date after adding a minute should become 2015-01-15 09:00:16 2) 例:1)2015-01-15 08:59:16添加分钟后的日期应该成为2015-01-15 09:00:16 2)

For this, I got the minute part from the date and added 1 to it with a modulo 60: str((int(csvRowArray[1][10:12])+ 1)%60) 为此,我从日期得到了一分钟,并用模60加上1:str((int(csvRowArray [1] [10:12])+ 1)%60)

Here csvRowArray[1] is the date in string format. 这里csvRowArray [1]是字符串格式的日期。

The problem comes when the minute is 59, although it resets the minute part to 0, the hour part doesn't increase. 分钟为59时出现问题,虽然它将分钟部分重置为0,但小时部分不会增加。 This should also work when the hour is 23:59 and we add a minute, the date should change.Or if the date is at the end of the month and the hour is 23:59 then even the month should change! 当小时是23:59并且我们添加一分钟,日期应该改变时,这也应该有效。如果日期是月末,小时是23:59,那么即使是月份也应该改变!

Is there a function in Python to read the date in Hive timestamp format and add a minute to it? Python中是否有一个函数以Hive时间戳格式读取日期并为其添加一分钟?

It will help if you can convert the object into DateTime instances as that will allow you to manipulate the time directly using timedelta . 如果您可以将对象转换为DateTime实例,将会有所帮助,因为这将允许您使用timedelta直接操作时间。 Taking your example of '2015-01-15 08:59:16' 以你的例子'2015-01-15 08:59:16'为例

from datetime import datetime as dt
from datetime import timedelta
a='2015-01-15 08:59:16'
dt_obj = dt.strptime(a, '%Y-%m-%d %H:%M:%S')
dt_with_one_more_min = dt_obj + timedelta(minutes=1)
print('original time = {}'.format(dt_obj))
print('new time = {}'.format(dt_with_one_more_min))

This will give the following result: 这将得到以下结果:

original time = 2015-01-15 08:59:16
new time = 2015-01-15 09:00:16

My example is with minutes, but you can try other modifications too with timedelta and it will work. 我的例子是分钟,但你可以尝试使用timedelta其他修改,它会起作用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM