I am trying to apply current time and date in a Windows application using Python. I used the following code:
current_time = str(time.strftime("%m.%d.%Y %H:%M"))
type(waitForObject("{name='TextBoxSampleName'}"), "Test." + current_time)
but when I run the script I see the following format
27Test.06.03.2016 10 (the hours and minutes are separated for some reason)
I want the final result to be Test.06.03.2016.10:27
Without knowing what waitForObject is, we can still get the output you desire.
First, there is a minor formatting mistake in your current_time = line:
current_time = str(time.strftime("%m.%d.%Y.%H:%M"))
^-- Add this period to get the format you want
Next, the second line can simply be (or you can assign it to a variable for later use):
print("Test."+current_time)
These two changes will print out something like this:
Test.06.03.2016.12:42
Related
First off, I am very new to all of python. I am now trying to figure out how to replace a time string in a certain column (csv) when that time is greater than the current time.
The script I am building from is relying on petl, so that is what i am using. First, the source csv is opened with petl as table1. It contains multiple columns, one of which is " End time". In this column I would like to replace that time with #time? (for HTML formatting later), only if it is greater than the current time.
the time has the format like "12:15". However, I do not see any change when running the line with >, yet with < all values in the column change.
The line I am struggling with:
current=time.localtime()
table2= petl.convert(table1, ' End time', lambda v, row: '#'+v+'?' if time.strptime(v, '%H:%M') > current else v, pass_row=True)
I would also like to know how I can print or see what time.strptime is using as values, is this possible?
Any ideas are highly appreciated!
If you only pass hour and minute to time.strptime, strptime automatically fills in the missing values for year, month, day with 1900, 1, 1 - so of course that's always less than time.localtime().
If your table contains times in 24 hour format, you can directly compare the time strings from your table with the localtime in the narrower sense (just the TIME part).
To achieve this, use time formatting like so:
current = time.strftime('%H:%M',time.localtime())
It is often helpful to start a python interpreter in a shell and play with the intermediate steps of your computations. Just type the variable, and you will see what it evaluates to:
>>> t2 = time.strptime('12:15', '%H:%M')
>>> t2
time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=12, tm_min=15, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1)
On this problem I keep getting stuck when trying several options provided.
In simple words, I'm running a script that has a starting time (several actually, based on different criteria) and in a loop I want to display the running time of that criteria in a JSON and put it in a program (using requests) that is updated every time the loop passes one of the criteria.
I was doing that by simply running:
starting_time = datetime.now() #but just a bit earlier in the script
now = datetime.now()
running_time = now-starting_time
This running_time is then used as a variable in a JSON, but that needs to be in the format of 'HH:MM:SS' else my requests doesn't allow me to put. Which caused the problem for me, because it isn't possible to use strftime on a timedelta.
The timedelta might be based on miliseconds, but those are fine as "00:00:00"... but that caused me problems when trying to convert the timedelta to string first and then convert it back to a regular datetime.
What am I missing?
A possible workaround would be:
starting_time = datetime.now()
now = datetime.now()
running_time = now-starting_time
x = datetime.timedelta(seconds=running_time.seconds)
result = str(x)
if result[1] == ":":
result = "0"+result
print(result)
Here line 4 makes sure that x only has the seconds and ignores the miliseconds of running_time. Then we add a zero at the beginning in case needed.
But also see comment to better understand timedelta.
I have a variable current_time which is current date time formatted the way I need. I need to convert the time from completed_date to be in the same format. I thought the code below would work but it throws error - replace() takes no keyword arguments. I cant seem to figure out the corrent syntax for this for some reason.
current_time = datetime.utcnow().replace(tzinfo=pytz.utc) # code i use now
completed_date - 2019-12-07T15:17:04+00:00 # print of completed_date variable
completed_date = completed_date.replace(tzinfo=pytz.utc) # how I thought I could modify completed_date to look like current time
I have a dataframe with two columns with different times in string format, I want to find the difference between the two columns so I use the following code
operational_data_clean['Pick/pack start-time'] = pd.to_datetime(operational_data_clean['Pick/pack start-time'])
operational_data_clean['Flight launched-time'] = pd.to_datetime(operational_data_clean['Flight launched-time'])
operational_data_clean['time_to_launch'] = 0
operational_data_clean['time_to_launch'] = operational_data_clean['Flight launched-time'] - operational_data_clean['Pick/pack start-time']
but this code when I run the first time I get good results but when I run the second time it add todays date on the 'Pick/pack start-time' and 'Flight launched-time' value.
How can I convert this time only to hours without getting the dates that are messing my data.
I am assuming you are running your code with jupyter notebook.
When you execute your code, your variable operational_data_clean['Pick/pack start-time'] becomes pd.to_datetime(operational_data_clean['Pick/pack start-time']).
So when you execute the block one more time, jupyter remembers your variables and therefore will perform the same function again, essentially doing this:
pd.to_datetime(pd.to_datetime(operational_data_clean['Pick/pack start-time'])).
As for pd.to_datetime() itself, I would advise to look through the pandas docs.
I am new to Python and need some help in being able to import done day old logs. Below is the script I have come up with, but not sure if it is working or if there is a better way to do this.
def fileCreation(path):
now = time.time()
oneday_ago = now - (24*60*60) ## seconds in 1 day
if fileCreation < oneday_ago:
print f
getAuditRecords(f)
I have a script that does import the whole database from mid June 2014 but only need to get day old logs.
Here is a sample of the logs I am trying to import
/mnt/hcp1/R1P/R1P_ora_982_2.xml.201409070400
/mnt/hcp1/R1P/R1P_ora_20_1.xml.201409070400
/mnt/hcp1/R1P/R1P_ora_29962_1.xml.201409070400
/mnt/hcp1/R1P/R1P_ora_15593_2.xml.201409070400
/mnt/hcp1/R1P/R1P_ora_9946_1.xml.201409070400
/mnt/hcp1/R1P/R1P_ora_10746_1.xml.201409070400
/mnt/hcp1/R1P/R1P_ora_6508_1.xml.201409070400
/mnt/hcp1/R1P/R1P_ora_17340_2.xml.201409070400
/mnt/hcp1/SCC/SCC_ora_18881_2.xml.201407090400
In order to compare the file creation time to one day ago, you need to actually get the file creation time. Your code is using fileCreation, the function; it doesn't mean anything useful to ask whether that function is less than some time.
Unfortunately, "file creation time" is not a portable concept. If you really want that, you need to write different code for different platforms, which I won't explain.
Usually, you're happy with "file modification time". This is set when the file is created, and updated only when you overwrite or append to the file. You can use getmtime to read this. So:
def fileCreation(path):
now = time.time()
oneday_ago = now - (24*60*60) ## seconds in 1 day
mtime = os.path.getmtime(path)
if mtime < oneday_ago:
print f
getAuditRecords(f)
However, it looks like there's a timestamp attached to each filename. If /mnt/hcp1/R1P/R1P_ora_982_2.xml.201409070400 means that the file was created on 7 September 2014 at 04:00 (and if the timezones, etc. aren't an issue), you may want to consider parsing those strings instead of statting the file.
And once you're parsing date strings, you might as well use the simpler and higher-level datetime library instead of the lower-level time. (You could do this with the previous version too, but since getmtime returns a time-style timestamp, you'd have to convert it manually to use it as a datetime, so there's less advantage.)
So:
def fileCreation(path):
now = datetime.datetime.now()
oneday_ago = now - datetime.timedelta(days=1)
fileext = os.path.splitext(path)[1][1:]
filetime = datetime.datetime.strptime(fileext, '%Y%m%d%H%M')
if filetime < oneday_ago:
print f
getAuditRecords(f)
(Also, I'm not sure what that f is. Maybe you meant path?)
Regarding the "two days ago" part, you should use datetime.datetime and datetime.timedelta
E.g.
import datetime
now = datetime.datetime.now()
two_days = datetime.timedelta(days=2)
two_days_ago = now - two_days