SoFunction
Updated on 2024-11-12

Python scripts to handle timestamps, time calculations, etc. shared

Due to practical needs, briefly wrote a small script, and packaged to generate exe, for use in the absence of network environment

Script 1: Display the current time and timestamp, and the time and timestamp 10 minutes from now

# -*- coding: utf-8 -*- 
"""
Project: pyWorkspace
Creator: Administrator -haochuang
Create time: 2021-05-12 09:24
IDE: PyCharm
Introduction:

"""
import time
import datetime


t=()

# Current date
t1 =('%Y-%m-%d %H:%M:%S')
# to second time stamps
ts1=((t1, '%Y-%m-%d %H:%M:%S'))
# to milliseconds
end_time=int(str(ts1*1000).split(".")[0])


#10 minutes later
t2 = (t+(minutes=10)).strftime("%Y-%m-%d %H:%M:%S")
# t2=((hours=1)).strftime("%Y-%m-%d %H:%M:%S")
# to second time stamps
ts2=((t2, '%Y-%m-%d %H:%M:%S'))
# to milliseconds
start_time=int(str(ts2*1000).split(".")[0])

#print("\n","*"*30)
print("\n")
print("*"*30)
print("Current timestamp:")
print(start_time)
print("Current time:")
print(("%Y-%m-%d %H:%M:%S", (ts2)))
print("*"*30,"\n")

print("Timestamped in 10 minutes:")
print(end_time)
print("Time in 10 minutes:")
print(("%Y-%m-%d %H:%M:%S", (ts1)))

print("*"*30,"\n")

Script 2: Displays the current time with a timestamp, as well as the time with a timestamp 10 minutes from now, allowing a timestamp to be generated for how long from now, based on the specified time entered

# -*- coding: utf-8 -*- 
"""
Project: pyWorkspace
Creator: Administrator -haochuang
Create time: 2021-05-12 09:24
IDE: PyCharm
Introduction:

"""
import time
import datetime


t=()

# Current date
t1 =('%Y-%m-%d %H:%M:%S')
# to second time stamps
ts1=((t1, '%Y-%m-%d %H:%M:%S'))
# to milliseconds
end_time=int(str(ts1*1000).split(".")[0])


#10 minutes later
t2 = (t+(minutes=10)).strftime("%Y-%m-%d %H:%M:%S")
# t2=((hours=1)).strftime("%Y-%m-%d %H:%M:%S")
# to second time stamps
ts2=((t2, '%Y-%m-%d %H:%M:%S'))
# to milliseconds
start_time=int(str(ts2*1000).split(".")[0])

#print("\n","*"*30)
print("\n")
print("*"*30)
print("Current timestamp:")
print(start_time)
print("Current time:")
print(("%Y-%m-%d %H:%M:%S", (ts2)))
print("*"*30,"\n")

# Time stamp in 10 minutes
print("Timestamp 10 minutes later:")
print(end_time)
print("Time in 10 minutes:")
print(("%Y-%m-%d %H:%M:%S", (ts1)))
print("*"*30,"\n")

# User-defined time
time_user = input("How many minutes after the timestamp is needed(Please enter the correctintType value):")
t3 = (t+(minutes=int(time_user))).strftime("%Y-%m-%d %H:%M:%S")
ts3=((t3, '%Y-%m-%d %H:%M:%S'))
# to milliseconds
start_time=int(str(ts3*1000).split(".")[0])

print(time_user + " Timestamp in minutes:")
print(end_time)
print(time_user + " Minutes after the time:")
print(("%Y-%m-%d %H:%M:%S", (ts3)))
print("*"*30,"\n")

Script 3: Display partial times with timestamps, etc.

# -*- coding: utf-8 -*- 
"""
Project: pyWorkspace
Creator: Administrator -haochuang
Create time: 2021-05-12 09:24
IDE: PyCharm
Introduction:

"""
import time
import datetime
from datetime import timezone
from datetime import timedelta

# Display current second timestamp vs. millisecond timestamp, microsecond timestamp
t = ()
#print(t) # raw time data
#print(int(t)) # second timestamps
#print(int(round(t * 1000))) # millisecond timestamps
#print(int(round(t * 1000000))) # microsecond timestamps


# Displays the current date:
dt = ().strftime('%Y-%m-%d %H:%M:%S')
dt_ms = ().strftime('%Y-%m-%d %H:%M:%S.%f') # Date and time in microseconds, source: Bitwise quantization.
print("current date(s):     " + dt)
print("current date(ms):    " + dt_ms)


# Convert dates to second time stamps
#dtt = '2018-01-01 10:40:30'
#dtts = int(((dt, "%Y-%m-%d %H:%M:%S")))
#ts_ms = int(((dt, "%Y-%m-%d %H:%M:%S")))
t=()
print("current timestamp(s):    " + t)
print("current timestamp(ms):   " + (int(round(t * 1000))))


# International Standard Time
print("International Standard Time:"+('%Y-%m-%d %H:%M:%S', ()))
# Local time
print("Local current time: "+('%Y-%m-%d %H:%M:%S', ()))

# Convert the current date to a second time stamp
dt = ('%Y-%m-%d %H:%M:%S', ())
dt_ts = int(((dt, "%Y-%m-%d %H:%M:%S")))
print("Current time:" + dt)
print("Current timestamp: " + dt_ts)

# Will get a second timestamp ten minutes from now #
#dt_10 = int((()+(minutes=10)).strftime("%Y-%m-%d %H:%M:%S"))
#ts_10 = int(((dt_10, "%Y-%m-%d %H:%M:%S")))
after10 = (()+(minutes=10)).strftime("%Y-%m-%d %H:%M:%S")
after10_ts = int(((t1,after10)))
print("Time in 10 minutes:" + after10)
print("Time stamp in 10 minutes:"

Script 4: Show partial times with timestamps, etc.

# -*- coding: utf-8 -*- 
"""
Project: pyWorkspace
Creator: Administrator -haochuang
Create time: 2021-05-12 09:08
IDE: PyCharm
Introduction:

"""

import datetime
import time

print('*'*30 +"Getting the way of time")
# Get current time: Thu Nov 03 16:40:00 2016
print(("%a %b %d %H:%M:%S %Y", ()))

# Get current time: 2016-11-03 16:40:00
print(().strftime('%Y-%m-%d %H:%M:%S'))

# Accessed Year, Month, Day: 2016-11-03
print(())

# Get current time: 2016-11-03 16:43:14.550000
print(())

# Without parameters is 00:00, parameter days=1 means one day: 1 day, 0:00:00
print((days=1))

#GetYesterday Date: 2016-11-02
nowtime=()
oldtime=(days=1)
print(nowtime-oldtime)

# Get the exact date of yesterday
oldtime=(days=1)
print (() - oldtime)

print ('*'*30 + 'python time module for time processing')

import time
# Return timestamp
# print(())

# Return the current time
print(())

# Return to time of day ago
print((()-86400))

# Function returns an object of type time.struct_time
time_obj = ()
print(time_obj)
#in the end:time.struct_time(tm_year=2016, tm_mon=7, tm_mday=27, tm_hour=8, tm_min=52, tm_sec=26, tm_wday=2, tm_yday=209, tm_isdst=0)
# Formatted output:
print(time_obj.tm_year,time_obj.tm_mon,time_obj.tm_mday)

print("{year}-{month}".format(year=time_obj.tm_year,month=time_obj.tm_mon))

# Print local time as time.struct_time type
print(())

# Convert to timestamp
time_obj = ()
print((time_obj))

# Delay 2 seconds
(2)

# Print UTC, Universal Standard Time, Beijing time zone is the Eastern 8 zones, eight hours ahead of UTC
print(("%Y-%m-%d %H:%M:%S",()))

# Local time
print(("%Y-%m-%d %H:%M:%S",()))

# Take the time.struct_time type time and convert it to a timestamp
tm = ("2016-05-6 15:06:33","%Y-%m-%d %H:%M:%S")
print(tm)
print((tm))


print ('*'*30 + '3-python time processing of datetime module')

import datetime

# Print current, year, month, day
print(())

# Print the current time to the nearest microsecond
current_time = ()
print(current_time)

# Convert to time.struct_time format time
current_time = ()
print(current_time.timetuple())

# Plus ten days
print(() +(days=10))
# Minus ten days
print(() +(days=-10))
# Minus ten hours
print(() +(hours=-10))
# Plus 120s
print(() +(seconds=120))

# Replace with specified time
cr_time = ()
print(cr_time.replace(2014,9,12))
# Result: 2014-09-12 17:28:17.522893

# Formatted output
print(("21/11/06 16:30","%d/%m/%y %H:%M"))

# The type is <class ''> after replacing it with the specified time.
current_time = ()
time_obj = current_time.replace(2015,5)
print(time_obj,type(time_obj))
# Results : 2015-05-27 17:34:13.350245 <class ''>''

# Compare time sizes for use in a specified time range
current_time = ()
time_obj = current_time.replace(2015,5)
print(current_time>time_obj)

import datetime
def getYesterday():
    today=()
    oneday=(days=1)
    yesterday=today-oneday
    return yesterday

# Output
print(getYesterday())

Script 5: On timestamp processing

# -*- coding: utf-8 -*- 
"""
Project: pyWorkspace
Creator: Administrator -haochuang
Create time: 2021-05-12 09:24
IDE: PyCharm
Introduction:

"""
import time
import datetime
from datetime import timezone
from datetime import timedelta

# Display current second timestamp vs. millisecond timestamp, microsecond timestamp
t = ()
print(t)  # Raw time data
print(int(t))  # Second time stamps
print(int(round(t * 1000)))  # millisecond timestamps
print(int(round(t * 1000000)))  # Microsecond time stamps


# Displays the current date:
dt = ().strftime('%Y-%m-%d %H:%M:%S')
dt_ms = ().strftime('%Y-%m-%d %H:%M:%S.%f') # Date and time in microseconds, source: Bitwise quantization.
print(dt)
print(dt_ms)


# Convert dates to second time stamps
dt = '2018-01-01 10:40:30'
ts = int(((dt, "%Y-%m-%d %H:%M:%S")))
print(ts)


# Conversion of second timestamps to dates
ts = 1515774430
dt = ("%Y-%m-%d %H:%M:%S", (ts))
print(dt)

# Time zone conversion
# Display UTC time
utc_now = ()
print(utc_now)
# Universal Standard Time
# utc_time = datetime(2019, 7, 30, 7, 50, 0)
print(().strftime('%Y-%m-%d %H:%M:%S'))
# UTC+8 Beijing time
# cst_time =utc_time.astimezone(timezone(timedelta(hours=-8))).strftime("%Y-%m-%d %H:%M:%S")

# International Standard Time
print("International Standard Time:"+('%Y-%m-%d %H:%M:%S', ()))
# Local time
print("Local time:"+('%Y-%m-%d %H:%M:%S', ()))

to this article on Python processing timestamps and time calculations and other scripts to share the article is introduced to this, more related Python timestamps time calculations, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!