Generate random time in hour and minute format python [duplicate] - python

This question already has answers here:
Formatting timedelta objects [duplicate]
(7 answers)
Closed 5 months ago.
I want to generate a timestamp only in hour and minutes format, like this:
154h 27m
Please check my logic below
planned_downtime = timedelta(hours=random.randrange(150))
However, the result is coming in seconds. To convert it to above mentioned format, I applied this:
planned_downtime.strftime("%H %M")
But I'm getting errors. How can I convert this randomly generated time in above format?

Like this maybe:
m = random.randint(0,60)
h = random.randint(0,24)
ts = f'{h}h {m}m'
print(ts)
15h 48m

Related

Converting hh:mm:ss to float for further calculations [duplicate]

This question already has answers here:
pandas - change time object to a float?
(3 answers)
Closed 2 years ago.
I need to convert my column with records like 'hh:mm:ss' to float format in order to make further calculations.
In excel it is done in very simple way, you just multiply 'hh:mm:ss' by 24, but in Python it doesn't work out. I'm new to Python, need your help.
Any idea?
My Dataframe:
list = ['01:36:01', '00:02:18', '02:59:40', '04:16:30']
Here is what I need to achieve:
lst = [['01:36:01', 1.600], ['00:02:18', 0.038] , ['02:59:40', 2.994], ['04:16:30', 4.275]]
df = pd.DataFrame(lst, columns = ['Time', 'Float'])
print(df)
Time Float
0 01:36:01 1.600
1 00:02:18 0.038
2 02:59:40 2.994
3 04:16:30 4.275
You can use below logic to find the time difference in seconds and then convert it into hours
import datetime as d
lis = ['01:36:01', '00:02:18', '02:59:40', '04:16:30']
start_dt = dt.datetime.strptime("00:00:00", '%H:%M:%S')
[float('{:0.3f}'.format((dt.datetime.strptime(time, '%H:%M:%S') - start_dt).seconds/3600)) for time in lis]
Output:
[1.6, 0.038, 2.994, 4.275]
Have you seen this post that looks like what you are trying to do.
https://stackoverflow.com/a/47043887/14982298

How to convert current datetime into 13 digits Unix timestamp? [duplicate]

This question already has answers here:
How can I convert a datetime object to milliseconds since epoch (unix time) in Python?
(14 answers)
Closed 3 years ago.
I want to convert current datetime into Unix time stamp
My Code
import time
import datetime
d = datetime.datetime.now()
unixtime = time.mktime(d.timetuple())
print(unixtime)
My output:
1577098747.0
Expected Output:
1577098747123.0
Above code gives me timestamp upto 10 digits but I want it to be accurate till 13 digits.
Note: I don't want to convert it manually multiplying by 10**3 I want to capture accurate milliseconds.
do it like this
import time
import datetime
d = datetime.datetime.now()
unixtime = datetime.datetime.timestamp(d)*1000
print(unixtime)
or you just use time.time()

int to datetime in Python [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Parsing datetime in Python..?
(2 answers)
Closed 4 years ago.
I'm receiving data from the port.
This data has date and time.But not formatted.
Example:
'02012019', '090253'
I want to save it to the database and I want to convert this format.
02-01-2019 09:02:53
Is there a function for this? Or is it a problem if I write? It will run 200 times in 5 seconds.
Anybody have a suggestion?
from datetime import datetime
date_data ='02012019 09:02:53'
data = datetime.strptime(date_data,'%d%m%Y %H:%M:%S')
result = data.strftime('%d-%m-%Y %H:%M:%S')
# output '02-01-2019 09:02:53'

Converting string with decimals into datetime [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
How can I parse a time string containing milliseconds in it with python?
(7 answers)
Closed 4 years ago.
This question is very similar to this SO, but my data is on the form:
'13-04-2018 14:06:26.866'
'13-04-2018 14:06:27.131'
'13-04-2018 14:06:27.404'
'13-04-2018 14:06:27.674'
...
i.e. the seconds are given as decimals. My reading of the datetime documentation suggests that it doesn't support this format, so I am not sure how to best proceed.
Looks like you need
.%f == Microsecond as a decimal number, zero-padded on the left.
Ex:
import datetime
s = '13-04-2018 14:06:26.866'
print(datetime.datetime.strptime(s, "%d-%m-%Y %H:%M:%S.%f"))
Output:
2018-04-13 14:06:26.866000

Calculate difference between two datetimes odoo 10 [duplicate]

This question already has answers here:
Date difference in minutes in Python
(12 answers)
Closed 5 years ago.
Working on Odoo10, i need to calculate the difference between two fields of datetime type, start and finish, i need the difference to be in minutes. how can i do that ?
Try with this example:
from dateutil.relativedelta import relativedelta
#api.one
#api.depends('start_field','finish_field')
def _total_minutes(self):
if self.start_field and self.finish_field:
start_dt = fields.Datetime.from_string(self.start_field)
finish_dt = fields.Datetime.from_string(self.finish_field)
difference = relativedelta(finish_dt, start_dt)
days = difference.days
hours = difference.hours
minutes = difference.minutes
seconds = 0

Categories

Resources