date-time-string to UNIX time with milliseconds - python-3.x

I need to convert a date/time string to UNIX timestamp including the milliseconds. As the timetuple() does not include milli or microseconds I made a short workaround. But I was wondering, is there a better/nicer way to do this?
import datetime as dt
import time
timestamp = '2018-01-19 10:00:00.019' # example of input time string
tmp = timestamp.split('.')
millisec = tmp[-1] # extracting only milli-seconds
UX_time = time.mktime(dt.datetime.strptime(tmp[0], '%Y-%m-%d %H:%M:%S').timetuple()) + float(millisec)/1e3
print(UX_time)
1516352400.019
I realize my timezone is off by one hour, so you might be getting
print(UX_time)
1516356000.019

you can try this:
timestamp = '2018-01-19 10:00:00.019'
tmp=np.datetime64(timestamp)
print(tmp.view('<i8')/1e3)
output:
1516352400.019

Also possible with your current code:
import datetime as dt
import time
timestamp = '2018-01-19 10:00:00.019' # example of input time string
ts = dt.datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f')
UX_time = time.mktime(ts.timetuple()) + ts.microsecond/1e6
print "%.3f" %UX_time

Related

Change panda date format into another date format?

How do I convert this format below into the result format ?
import pandas as pd
date = pd.date_range('2022-01-01',2022-01-31', freq = 'H')
Result:
'2021-01-01T01%3A00%3A00',
What is the correct name for the result time format ? Have tried using urlilib.parse module, but it did not have T and it can take 1 date.
Thank you !
This so called url encode , so we need urllib, notice here %3A = ':'
import urllib
date.astype(str).map(urllib.parse.quote)
Out[158]:
Index(['2022-01-01%2000%3A00%3A00', '2022-01-01%2001%3A00%3A00',
....

How to calculate parts of a timestamp

I have a problem with my code. I have to datetime values as a string. I want to calculate the
time passed between them. I can archive this with the following code:
from datetime import datetime
fmt = '%Y-%m-%d %H:%M:%S'
tstamp1 = datetime.strptime('2021-02-20 12:25:08', fmt)
tstamp2 = datetime.strptime('2021-02-20 11:19:17', fmt)
td = tstamp1 - tstamp2
The result I get is 1:05:51.
Now I have another value as string: 00:58:08
Here I also want to know how many time passed between them:
1:05:51 - 00:58:08.
I need the result formated like this: 00:07:43
I really dont know how to do this. Can somebody help me?
This should help your requirement.
from datetime import datetime
fmt = '%Y-%m-%d %H:%M:%S'
tstamp1 = datetime.strptime('2021-02-20 12:25:08', fmt)
tstamp2 = datetime.strptime('2021-02-20 11:19:17', fmt)
td = tstamp1 - tstamp2
tr = '00:58:08'
tr = datetime.strptime(str(tr), '%H:%M:%S')
td = datetime.strptime(str(td), '%H:%M:%S')
ans = td - tr
print(ans)

How to insert todays date automatically?

I want to insert today's date in the following code automatically.
import shutil
shutil.copy(r"C:\Users\KUNDAN\Desktop\Backup\Cash MAR.2017 TO APR.2019.accdb",
r"F:\Cash MAR.2017 TO APR.2019 (11-09-19).accdb ")
print("DONE !!!")
First off, I'm not 100% sure of your question. If this doesn't answer it as you're expecting, please reformat your question to add clarity.
You can do this via the datetime library and string formatting. For example (using UK date format):
import shutil
from datetime import datetime as dt
today = dt.today().strftime('%d-%m-%y')
src = "C:/Users/KUNDAN/Desktop/Backup/Cash MAR.2017 TO APR.2019.accdb"
dst = "F:/Cash MAR.2017 TO APR.2019 ({today}).accdb".format(today=today)
shutil.copy(src, dst)
print("DONE !!!")
I have found these two links very useful:
string formatting
datetime formatting using strftime
import datetime
datetime_now = datetime.datetime.now()
print(datetime_now)
It will print the date and time for now and you can choose what format you like.

How to convert a/p (am/pm) time value in python to 24hr format

I am reading a csv file which has a time field as 3:50a and 4:25p. I need to convert this time values to 24 hour clock format as H:M for this I am doing:
datetime.strptime(str(time_value), '%I:%M').strftime('%H:%M')
where time_value is 3:50a or 4:25p. Now the problem is above line of code do not work because I have not inlucded the AM/PM tag which is %p in %I:%M and that is because in the csv file I am just getting a for AM and p for PM.
How can I deal with this situation. I need to convert the time_value to 24hr clock format. Thanks
Add an 'm' to the string you input to strptime and put the %p in its second argument,
>>> from datetime import datetime
>>> time_value = '3:50a'
>>> datetime.strptime(str(time_value)+'m', '%I:%M%p').strftime('%H:%M')
'03:50'
>>> time_value = '4:25p'
>>> datetime.strptime(str(time_value)+'m', '%I:%M%p').strftime('%H:%M')
'16:25'

Clearing a line of output in Python

So, I'm super super new to Python and programming in general. I'm still learning the most basic of commands and terminology. My question is I'm trying to print the datetime in an infinite loop, but only on one line.
I would like the output in the shell to display the date and time and then clear the previously displayed datetime and replace it with the new value. I can't seem to figure out what I'm missing, and reading through different questions, I can't seem to find what I'm looking for. Any thoughts, help, or guidance would be appreciated. Trying to read the Python Documentation is not helping me at all at this point. This is what I've figured out so far:
import datetime
import time
while True:
today = datetime.date.today()
now = datetime.datetime.now()
print ("The Current date is ", (today.strftime('%m/%d/%Y')))
print ("The Current Time is ", (now.strftime('%H:%M')))
time.sleep(30)
This Generates:
The Current date is 03/28/2017
The Current Time is 19:09
The Current date is 03/28/2017
The Current Time is 19:10
I'd like it to say:
The Current date is 03/28/2017
The Current Time is 19:09
Then, text above removed and replaced with
The Current date is 03/28/2017
The Current Time is 19:10
Datetime screenshot
One way is to use ANSI escape sequences:
import datetime
import time
import sys
while True:
today = datetime.date.today()
now = datetime.datetime.now()
print ("The Current date is ", (today.strftime('%m/%d/%Y')))
print ("The Current Time is ", (now.strftime('%H:%M')))
sys.stdout.write("\033[F") # Cursor up one line
sys.stdout.write("\033[F") # Cursor up one line
time.sleep(30)
Another option:
import datetime
import time
import os
while True:
today = datetime.date.today()
now = datetime.datetime.now()
print ("The Current date is ", (today.strftime('%m/%d/%Y')))
print ("The Current Time is ", (now.strftime('%H:%M')))
time.sleep(30)
os.system('clear')

Resources