In the code shown below, I need to manipulate the time var in python to display a date/time stamp in python to represent that delay.
For example, when the user enters the delay time in hours, I need to set the jcarddeliver var to update itself with the value of the current date/time + delay.
Also it should update the date var as well. For example, if the date is 24 Feb and time is 15:00 hrs and the delay time is 10 hrs, the jcarddeliver date should change to 25 Feb.
jcarddate = time.strftime("%a %m/%d/%y", time.localtime())
jcardtime = time.strftime("%H:%M:%S", time.localtime())
delay = raw_input("enter the delay: ")
jcarddeliver = ??
I just hope I am making sense.
You could try the datetime module, e.g.
import datetime
now = datetime.datetime.now()
delay = float (raw_input ("enter delay (s): "))
dt = datetime.timedelta (seconds=delay)
then = now + dt
print now
print then
The result of time.time() is a floating point value of the number of seconds since the Epoch. You can add seconds to this value and use time.localtime(), time.ctime() and other functions to get the result in various forms:
>>> now = time.time()
>>> time.ctime(now)
'Fri Sep 04 16:19:59 2009' # <-- this is local time
>>> then = now + (10.0 * 60.0 * 60.0) # <-- 10 hours in seconds
>>> time.ctime(then)
'Sat Sep 05 02:19:59 2009'
" i need to set the jcarddeliver var to update itself with the value of the current date/time + delay"
How about reformulating this to
jcarddeliver should be the current date-time plus the delay.
The "update itself" isn't perfectly sensible.
Try the following:
Try the most obvious way of computing "current date-time plus the delay"
print the result.
Try using localtime() on this result. What do you get?
Try this:
now = time.time()
then = now + 365*24*3600
print time.strftime('%Y-%m-%d', time.localtime(then))
Related
I have 1 time that is formatted like this
start_time 01:13:05
I have a second time that looks like it
Current time 01:13:10
When I calculating the difference, it produces the correct answer but chops a zero off
code:
new_start_time = datetime.strptime(start_time, '%H:%M:%S')
new_current_time = datetime.strptime(str(current_time), '%H:%M:%S')
elapsed_time = new_current_time - new_start_time
produces:
elapsed time 0:00:10
The 10 is correct but what happened to the zero? How do I hack it back on? I need it.
Add:
elapsed_split = str(elapsed_time).split(":")
elapsed_time = int(elapsed_split[0]).zfill(2) + elapsed_split[1:]
I have something like this:
import datetime
a = datetime.datetime.now()
do_something_for_a_long_time()
b = datetime.datetime.now()
c = b - a
print("Doing <something> took {c}".format(c))
The problem is that this works well but we want the seconds value to be in the form ., not microseconds?
I was able to isolate the milliseconds attribute from that timedelta object, it it seems it only has microseconds available.
Given your initial code example, you could use something like this:
# From your example.
c = b - a
# Get the hours, minutes, and seconds.
minutes, seconds = divmod(c.seconds, 60)
hours, minutes = divmod(minutes, 60)
# Round the microseconds to millis.
millis = round(c.microseconds/1000, 0)
print(f"Doing <something> took {hours}:{minutes:02}:{seconds:02}.{millis}")
which results in
# c = datetime.timedelta(seconds=7, microseconds=319673)
Doing <something> took 0:00:07.320
Take a look at Python’s built-in functions round() and divmod() and please poke around this and this related thread; also, please read through this and this thread to learn more about formatting timedelta objects.
just chop off the last three digits of the microseconds if you don't want to manually round off:
def format_time():
t = datetime.datetime.now()
s = t.strftime('%Y-%m-%d %H:%M:%S.%f')
return s[:-3] `def format_time():
t = datetime.datetime.now()
s = t.strftime('%Y-%m-%d %H:%M:%S.%f')
return s[:-3]
I have a path files which are named by time (201803061500) etc. What I need is a time conversion, because I use while loop to open a few of them there is an error when I want files from for example (...1555 to ... 1615) and Python sees an obivous problem because after 1555 is 1560, but I want him to convert that to time so after (...1555 will be ... 1600) any ideas how to use it?
Btw. Time conversion must be contain 4 digits, so it cannot be 16:00/16-00 etc. it must be 1600, because it goes as an input to my pathfile. Any ideas?
UPDATE - I did this, but this code is rubbish and I think my problem might be solved by one command.
Start_time_hours = input('Enter start time (hh): ' )
Start_time_minutes = input('Enter start time (mm): ')
if Start_time_hours >= 24:
print ("Values from 00 to 23 only!")
if Start_time_minutes >= 60:
x = Start_time_hours + 1
y = Start_time_minutes - Start_time_minutes
if y == 0:
print "Ok"
print x, y
if Start_time_minutes <= 55:
print Start_time_hours, Start_time_minutes
One easy way to handle unformated time is the datetime. You can first strip your strings and then do whatever you want !
from datetime import datetime, timedelta
from time import strtime
datetime_object = datetime.strptime(file_name, '%Y%m%d%H%M')
print(datetime_object) # returns '2018-03-06 15:00:00'
delta = timedelta(minutes=5)
next_time = datetime_object + delta
print(next_time) # returns '2018-03-06 15:05:00'
Finally you can get your string back by using time.strftime() function
new_string = next_time.strftime('%Y%m%d%H%M')
print(new_string) # returns '201803061505'
Source datetime: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
Source time: https://docs.python.org/2/library/time.html
Start_time_hours += (Start_time_minutes / 60)
Start_time_minutes %= 60
Start_time_minutes += 5
Those three lines solved my problem, datetime also works, but if you put those variables to input pathfile, you'll get an error. That's why I've choosen this solution.
I need some help with my code. I have a trouble with changing the strings.
I am checking with the strings if the variable getTime3 have a string 30 then I want to replace it with 00. On my code, it will find the string 30 to replace it with 0030 which is wrong. It should be 00.
Here is the code:
if getTime3 == '11:30PM':
self.getControl(346).setLabel('12:00AM')
elif getTime3 == '12:30PM':
self.getControl(346).setLabel('1:00AM')
else:
ind = getTime3.find(':')
if getTime3[ind+1:ind+3]=='30':
getTime3 = getTime3[:ind]+':00'+getTime3[+2:]
self.getControl(346).setLabel(getTime3)
else:
getTime3 = str(int(getTime3[:ind])+1)+':30'+getTime3[+2:]
self.getControl(346).setLabel(getTime3)
What I am expect for the two special cases, when the program finds the :, it will check if 30 is present then change the current hour to the next hour and make a new string with AM/PM label, example: change from 8 to 9 and replace 30 with 00 to make it to show 9:00PM. If the ending is 00 then I want to change 00 to 30 instead. I want to add 30 in the minute section and again preserves the AM/PM part. If the getTime3 have the string 11:30AM then I want to change it to 12:00PM.
Can you please help me with how to fix the 0030 to make it to show 00 instead and add the next hour?
With Python, slice like x[a:b] in the slice starting at a (inclusive), and finishing at b (exclusive).
So: getTime3[:ind] is the slice from 0 to ind exclusive, which is the hours without the ":".
And indexes are absolute index, not relative. So getTime3[+2:] is the same as getTime3[2:], which correspond to the substring starting at index 2.
What you want is:
getTime3 = getTime3[:ind] + ':00' + getTime3[ind + 3:]
# or
getTime3 = getTime3[:ind + 1] + '00' + getTime3[ind + 3:]
Example:
getTime3 = '08:30PM'
ind = getTime3.index(":")
getTime3[:ind] + ':00' + getTime3[ind + 3:]
# -> '08:00PM'
EDIT
If you want to do some calculation on time, you can use the datetime module.
time_fmt = '%I:%M%p'
Is the format used to represent time like '09:30PM', where:
%I Hour (12-hour clock) as a zero-padded decimal number.
%M Minute as a zero-padded decimal number.
%p Locale’s equivalent of either AM or PM.
How to add 30 min:
import datetime
time3 = '09:30PM'
dt3 = datetime.datetime.strptime(time3, time_fmt)
dt3 += datetime.timedelta(minutes=30)
time3 = dt3.strftime(time_fmt)
If you want to set the minutes to 0, you can do:
dt3 = datetime.datetime.strptime(time3, time_fmt)
dt3 = d3.replace(minute=0)
Please don't use the wrong tool for the task. You are manipulating times, yet using strings to do it. Start with this to turn 11:30PM into 12:00AM:
import datetime
t3 = datetime.datetime.strptime(getTime3, '%I:%M%p')
t3 += datetime.timedelta(minutes=30)
print(t3.strftime('%I:%M%p'))
Adding timedelta(minutes=30) makes the intent perfectly clear. Some relevant documentation is at https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
Without using any libraries, I'm trying to solve the Hackerrank problem "Time Conversion", the problem statement of which is copied below.
I came up with the following:
time = raw_input().strip()
meridian = time[-2:] # "AM" or "PM"
time_without_meridian = time[:-2]
hour = int(time[:2])
if meridian == "AM":
hour = (hour+1) % 12 - 1
print ("%02d" % hour) + time_without_meridian[2:]
elif meridian == "PM":
hour += 12
print str(hour) + time_without_meridian[2:]
However, this fails on one test case:
Since the test cases are hidden to the user, however, I'm struggling to see where the problem is occurring. "12:00:00AM" is correctly converted to "00:00:00", and "01:00:00AM" to "01:00:00" (with the padded zero). What could be wrong with this implementation?
It's even simpler than how you have it.
hour = int(time[:2])
meridian = time[8:]
# Special-case '12AM' -> 0, '12PM' -> 12 (not 24)
if (hour == 12):
hour = 0
if (meridian == 'PM'):
hour += 12
print("%02d" % hour + time[2:8])
You've already solved the problem but here's another possible answer:
from datetime import datetime
def solution(time):
return datetime.strptime(time, '%I:%M:%S%p').strftime('%H:%M:%S')
if __name__ == '__main__':
tests = [
"12:00:00PM",
"12:00:00AM",
"07:05:45PM"
]
for t in tests:
print solution(t)
Although it'd be using a python library :-)
from datetime import datetime
#Note the leading zero in 05 below, which is required for the formats used below
regular_time = input("Enter a regular time in 05:48 PM format: ")
#%I is for regular time. %H is for 24 hr time, aka "military time"
#%p is for AM/PM
military_time = datetime.strptime(regtime, '%I:%M %p').strftime('%H:%M')
print(f"regular time is: {regular_time"}
print(f"militarytime is {military_time}")
The following link proved to be very helpful: https://strftime.org/
I figured it out: it was converting "12:00:00PM" to "24:00:00" and not "12:00:00". I modified the code as follows:
time = raw_input().strip()
meridian = time[-2:] # "AM" or "PM"
time_without_meridian = time[:-2]
hour = int(time[:2])
if meridian == "AM":
hour = (hour+1) % 12 - 1
print ("%02d" % hour) + time_without_meridian[2:]
elif meridian == "PM":
hour = hour % 12 + 12
print str(hour) + time_without_meridian[2:]
leading to it passing all the test cases (see below).
dt_m = datetime.datetime.fromtimestamp(m_time)
hour_m = (dt_m.hour%12)+1 #dt_m.hour+1
offset_dt = datetime.datetime(dt_m.year, dt_m.month, dt_m.day, hour_m , dt_m.minute, dt_m.second, dt_m.microsecond)