How to add a minute in Python to Hive timestamp format - python

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
Example:1) 2015-01-15 08:59:16 date after adding a minute should become 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)
Here csvRowArray[1] is the date in string format.
The problem comes when the minute is 59, although it resets the minute part to 0, the hour part doesn't increase. 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!
Is there a function in Python to read the date in Hive timestamp format and add a minute to it?

It will help if you can convert the object into DateTime instances as that will allow you to manipulate the time directly using timedelta. Taking your example of '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.

Related

Date to datetime with timezone

How would I convert
"2022-02-01" to something like
"2022-02-01T()" there are no brackets I'd like to convert each 30 minute interval from 2022-02-01 00:30:00 H:M:S and so forth.
I wanted to use something like
startDates = [{'start': {'date': '2022-02-01'}}] # Minimal Reproducible Example
timeZone = pytz.timezone('America/Vancouver')
for event in startDates:
newStartDateTime = datetime.strptime(event['start']['date'], '%Y-%m-%d' )
print(newStartDateTime)
datetime_ist = datetime.strptime(event['start']['date'], '%Y-%m-%d' ).replace(tzinfo=timeZone)
print("Date & Time in : ",datetime_ist.strftime('%Y-%m-%dT%H:%M:%S%z'))
Which does produce a time slot at the first H:M:S I'd to obtain 48 of these
2022-02-01 00:00:00
Date & Time in : 2022-02-01T00:00:00-0812
First, make your datetime object timezone aware, using py.timezone.localize(datetime). And then convert it using astimezone().
For the 30-minute increments, use datetime.timedelta to keep adding 30 mins to your newStartDateTime.
from datetime import datetime, timedelta
import pytz
vancouver_tz = pytz.timezone('America/Vancouver')
ist_tz = pytz.timezone('Asia/Calcutta')
startDates = [{'start': {'date': '2022-02-01'}}] # Minimal Reproducible Example
for event in startDates:
newStartDateTime = datetime.strptime(event['start']['date'], '%Y-%m-%d')
vancouver_time = vancouver_tz.localize(newStartDateTime)
india_time = vancouver_time.astimezone(tz=ist_tz)
print("first Date & Time in IST:", india_time.strftime('%Y-%m-%dT%H:%M:%S%z'))
# now add increasing timedeltas in minute chunks
for delta in range(0, 30 * 48, 30):
offsetted_ist = india_time + timedelta(minutes=delta)
print("Date & Time in IST:", offsetted_ist.strftime('%Y-%m-%dT%H:%M:%S%z'))
Output:
first Date & Time in IST: 2022-02-01T13:30:00+0530
Date & Time in IST: 2022-02-01T13:30:00+0530
Date & Time in IST: 2022-02-01T14:00:00+0530
Date & Time in IST: 2022-02-01T14:30:00+0530
...
Date & Time in IST: 2022-02-02T12:00:00+0530
Date & Time in IST: 2022-02-02T12:30:00+0530
Date & Time in IST: 2022-02-02T13:00:00+0530
Btw, choose if you want to use camelCase or lowercase_with_underscores for your variables. Pick one. Python is typically lowercase_with_underscores, except classes which are UpperCamelCase.

How do I take end date as 11 months ahead of start date in the datetime format in Python?

I am taking start date as user input in the datetime format in python. Using the start date, I want to take end date as 11 months from start date.
start_date= month_dt
end_date =
Here, month_dt is taken in taken as user input in the datetime format, e.g. 2020-01-01 . How do I take end_date as 11 months ahead of the start date?
import datetime
from dateutil.relativedelta import relativedelta
print(datetime.date.today() - relativedelta(months=+11))
Current date reduced by 11 months

Extract day of the week and hour from iso datetime stamp in Python

How can I extract the day of the week and the hour of the day from a timestamp in this format?
2020-08-17T01:54:38.000Z
So for the example above I would get Monday and 01 in return.
You could parse the string into a datetime object using strptime with a custom format, and then extract the info you need from it:
from datetime import datetime
dateStr = '2020-08-17T01:54:38.000Z'
dt = datetime.strptime(dateStr,'%Y-%m-%dT%H:%M:%S.%fZ')
hour = dt.strftime('%H')
dayOfWeek = dt.strftime('%A')

Converting Epoch time format to standard time format

I am having an issue with converting the Epoch time format 1585542406929 into the 2020-09-14 Hours Minutes Seconds format.
I tried running this, but it gives me an error
from datetime import datetime
DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
datetime.utcfromtimestamp(df2.timestamp_ms).strftime('%Y-%m-%d %H:%M:%S')
error : cannot convert the series to <class 'int'>
What am I not understanding about this datetime function? Is there a better function that I should be using?
edit: should mention that timestamp_ms is my column from my dataframe called df.
Thanks to #chepner for helping me understand the format that this is in.
A quick solution is the following:
# make a new column with Unix time as #ForceBru mentioned
start_date = '1970-01-01'
df3['helper'] = pd.to_datetime(start_date)
# convert your column of JSON dates / numbers to days
df3['timestamp_ms'] = df3['timestamp_ms'].apply(lambda x: (((x/1000)/60)/60/24))
# add a day adder column
df3['time_added'] = pd.to_timedelta(df3['timestamp_ms'],'d')
# add the two columns together
df3['actual_time'] = df3['helper'] + df3['time_added']
Note that you might have to subtract some time off from the actual time stamp. For instance, I had sent my message at 10: 40 am today when it is central time (mid west USA), but the timestamp was putting it at 3:40 pm today.

Convert weird Python date format to readable date

I am using Python to access the mobile API of some web service and the response contains the following weird Date notation: u'/Date(1409522400000+0200)/' This should be the 1st of September, 2014.
I am not sure which format this is, but I would like to convert this to something readable, i.e. a date or a datetime or Unix time.
Can anybody help me with this?
The time string looks like OData version 2 JSON verbose format for Datetime that may be seen in old ASP.NET or WCF applications:
“/Date(<ticks>[“+” | “-” <offset>])/”
<ticks> = number of milliseconds
since midnight Jan 1, 1970
<offset> = utc offset
#!/usr/bin/env python3
import re
from datetime import datetime, timedelta, timezone
time_string = u"/Date(1409522400000+0200)/"
epoch = datetime(1970, 1, 1, tzinfo=timezone.utc)
ticks, offset = re.match(r'/Date\((\d+)([+-]\d{4})?\)/$', time_string).groups()
utc_dt = epoch + timedelta(milliseconds=int(ticks))
print(utc_dt)
if offset:
offset = int(offset)
hours, minutes = divmod(abs(offset), 100)
if offset < 0:
hours, minutes = -hours, -minutes
dt = utc_dt.astimezone(timezone(timedelta(hours=hours, minutes=minutes)))
print(dt)
Output
2014-08-31 22:00:00+00:00
2014-09-01 00:00:00+02:00
where timezone is defined here.
you received a (java?) timestamp in milliseconds. you can convert it to something more readable like so:
from datetime import date
d=1409522400000/1000.0 # divide by 1000 to get seconds
print date.fromtimestamp(d) # -> 2014-09-01

Categories