Using Python to make timestamp conversion tools
Python timestamp to date date to timestamp
In general, as a programmer, JSON and timestamps are commonly used two tools, I consulted many friends, they are generally through the online tool to format JSON, or query timestamp. This is also the way I used it before, and the disadvantages of this approach are as follows:
1. Each time you open the process is tedious, even if you have to open the browser, and then click
2. If you open a browser with enough tags, you can't find it (that is, someone like me)
3. etc.
And then I found out Hijson. After the tool can format the JSON locally, it has been looking for a small tool to find the timestamp locally. Alas, the Internet gods did not meet my needs. So I decided to write one myself.
The environment of this article
- Python 3.6
- Time Library
- Tkinter Library
Optional: You can use the Pyinstaller package to run as an EXE file. The program size is approximately 8m, and the run-time memory consumes about 15m.
First paste the code (because the time is limited, not written too neat, forgive me):
From tkinter import *
From time import *
'''
1. This program implements mutual conversion of timestamp and date format.
2, use the grid method to arrange the position of the components in a tabular manner
3. Convert and refresh operations through the Button button.
4. Get user input through the Entry.
'''
Root = Tk()
Root.title('timestamp conversion')
Root.resizable(0,0)#Forbidding stretching will become ugly
#Create variables, and initialize data
Label1 = Label(root, text='timestamp:').grid(row=0, column=0)
Label2 = Label(root, text='date:').grid(row=1, column=0)
V1 = StringVar()
P1 = StringVar()
V1.set(int(time()))
Label3 = Label(root, text='date:').grid(row=3, column=0)
Label4 = Label(root, text='timestamp').grid(row=4, column=0)
V2 = StringVar()
P2 = StringVar()
timeArray1 = localtime(int(time()))
V2.set(strftime("%Y-%m-%d %H:%M:%S", timeArray1))
P2.set(int(time()))
##timestamp converted to date
Def trans1():
E1 = Entry(root, textvariable=v1) # Entry is a control used by Tkinter to receive input such as strings.
E2 = Entry(root, textvariable=p1)
E1.grid(row=0, column=1, padx=10, pady=5) # Set the position of the input box, and the length and width properties.
E2.grid(row=1, column=1, padx=10, pady=5)
timeArray = localtime(int(e1.get()))
P1.set(strftime("%Y-%m-%d %H:%M:%S", timeArray))
#日期 converted to timestamp
Def trans2():
E3 = Entry(root, textvariable=v2) # Entry is a control used by Tkinter to receive input such as strings.
E4 = Entry(root, textvariable=p2)
E3.grid(row=3, column=1, padx=10, pady=5) # Set the position of the input box, as well as the length and width properties.
E4.grid(row=4, column=1, padx=10, pady=5)
P2.set(int(mktime(strptime(e3.get(), "%Y-%m-%d %H:%M:%S"))))
# Refresh the second module
Def refresh():
timeArray1 = localtime(int(time()))
V2.set(strftime("%Y-%m-%d %H:%M:%S", timeArray1))
P2.set(int(time()))
Button(root, text='convert', width=10, command=trans1) \
.grid(row=2, column=0, sticky=W, padx=10, pady=5)
Button(root, text='convert', width=10, command=trans2) \
.grid(row=5, column=0, sticky=W, padx=10, pady=5)
Button(root, text='refresh', width=10, command=refresh) \
.grid(row=5, column=1, sticky=W, padx=10, pady=5)
Button(root, text='exit', width=10, command=root.quit) \
.grid(row=6, column=1, sticky=E, padx=10, pady=5)
Trans1()
Trans2()
#Set the initial display position of the window
Sw = root.winfo_screenwidth()
Sh = root.winfo_screenheight()
x = (sw) / 2
y = (sh) / 2
Root.geometry("+%d+%d" %(x,y))
Mainloop()
I have always believed that the code was written well enough to require too many explanations. Take a look at the comments in the code above
Installation and use of Pyinstaller
Pip install pyinsatller
#Install directly run the following command
Pyinstaller
#If you can run the prompt option
Packaging files
#packaged commands (run on the command line, if you are not successful, remember to configure the environment variables)
Pyinsatller -F -w D:\python\timeTran.py
#Option introduction
#-F –onefile Generate a file for deployment (see XXXXX).
#-w,–windowed,–noconsole Execute using the Windows subsystem. When the program starts, it will not open the command line (only valid for Windows). It will not display a black window (too ugly, but also to manually close) I don't know what it means, I can try it myself.
GitHub Address: Github.com/vinterhe/timetransverter inside there are packaged EXE files can be directly taken to use
After the successful generation, there will be a info:appending archive to exe C:\Users\XXX\dist\timeTransverter.exe, which contains the EXE file you want. Welcome everyone and use, thank you.