OpsaC - Operating as PowerShell code
published: June 18, 2021 author: Tinu tags: Python - Scripting categories: Python
Das Mysterium Python datetime. Was mir in PowerShell simple erscheint, ist mir in Python immer noch nicht ganz klar - convert string to datetime und umgekehrt, was wann wie und warum?
Mit diesem Beispiel wandle ich ein String-Datum in ein DateTime-Object um.
def convert_string_to_datetime(dt_string, dt_format):
"""Convert string to datetime-object"""
import datetime as dt
return dt.datetime.strptime(dt_string, dt_format)
if __name__ =="__main__":
dt_format = "%Y-%m-%dT%H:%M:%S"
dt_string = "2021-06-20T09:15:00"
print(type(dt_string), dt_string)
dt_object = convert_string_to_datetime(dt_string, dt_format)
print(type(dt_object), dt_object)
Output:
<class 'str'> 2021-06-20T09:15:00
<class 'datetime.datetime'> 2021-06-20 09:15:00
Mit diesem Beispiel extrahiere ich aus dem aktuellen TimeStamp (DateTime-Object) den Wochentag als String.
def convert_datetime_to_string(dt_object, dt_format):
"""Convert datetime to string"""
import datetime as dt
return dt_object.strftime(dt_format)
if __name__ =="__main__":
import datetime as dt
dt_object = dt.datetime.now()
dt_format = "%A"
dt_string = convert_datetime_to_string(dt_object, dt_format)
if dt_string == "Saturday" or dt_string == "Sunday":
print("Thanks good, its weekend:", type(dt_string), dt_string, type(dt_object), dt_object)
else:
print("Oh no, it's weekday:", type(dt_string), dt_string, type(dt_object), dt_object)
Output:
Oh no, it's weekday: <class 'str'> Friday <class 'datetime.datetime'> 2021-06-18 09:22:49.396187
Damit aus einem String-Datum der Wochentag extrahiert werden kann, muss das String-Datum zuerst in ein DateTime-Object umgewandelt werden und danach wieder in ein String.
if __name__ =="__main__":
import datetime as dt
dt_string = "2021-06-20T09:15:00.123456"
dt_format = "%Y-%m-%dT%H:%M:%S.%f"
dt_object = convert_string_to_datetime(dt_string, dt_format)
print(type(dt_object), dt_object)
dt_format = "%A"
dt_string = convert_datetime_to_string(dt_object, dt_format)
print("Day of week:", type(dt_string), dt_string)
Output:
<class 'datetime.datetime'> 2021-06-20 09:15:00
Day of week: <class 'str'> Sunday
The epoch time is also called Unix time, POSIX time, and Unix timestamp. The epoch time means the number of seconds that have passed since January 1 1970 excluding leap seconds. The Unix time has 10 digits. Unix time can represent all timezone at once.
import os, time
from sys import platform
if platform == "linux" or platform == "linux2":
home_dir = os.environ['HOME']
elif platform == "darwin":
home_dir = os.environ['HOME']
elif platform == "win32":
home_dir = os.environ['USERPROFILE']
downloads = os.path.join(home_dir,'downloads')
dt_format = "%Y-%m-%d %H:%M:%S"
# Get the Python epoch (timestamp in seconds) of the file
mod_time_since_epoc = os.path.getctime(os.path.join(downloads, "testfile.txt"))
print("mod_time_since_epoc:", type(mod_time_since_epoc), mod_time_since_epoc)
# Convert the timestamp to string-datetime in localtime
dt_string_mod_time = time.strftime(dt_format, time.localtime(mod_time_since_epoc))
print("dt_string_mod_time :", type(dt_string_mod_time), dt_string_mod_time)
# Convert string-datetime to datetime-object
dt_file_datetime = dt.datetime.strptime(dt_string_mod_time, dt_format)
print("dt_file_datetime :", type(dt_file_datetime), dt_file_datetime)
Output:
mod_time_since_epoc: <class 'float'> 1624005612.6895187
dt_string_mod_time : <class 'str'> 2021-06-18 10:40:12
dt_file_datetime : <class 'datetime.datetime'> 2021-06-18 10:40:12
import datetime as dt
time_span = dt.datetime.now() - dt.timedelta(days=3)
print(type(time_span), time_span)
Output:
<class 'datetime.datetime'> 2021-06-15 10:36:45.500677