[PYTHON] I made a class that easily performs unixtime ← → datetime conversion

I made a class that converts unixtime and datetime (string type)

Converting unixtime ← → datetime is quite troublesome, so I made a class that can be easily converted.

The unix time is set to 0 seconds on January 1, 1970 at midnight, and indicates how many seconds have passed since then, while the date time is expressed in UTC (Coordinated Universal Time), which is familiar to the human eye. For example, 23:00 on September 24, 2015 is "1441303200" in unix time and "201509242300" in date time.

You can convert ~~ unixtime to datetime type by using the datetime module, but I can't find how to convert datetime to string type, so I wrote it. ~~ It seems that you can do it by using strftime. So I decided to position this class to make the conversion convenient.

convertTime.py



import datetime
import time


class convertTime:
	
	def __init__(self,time):
		self.time = time

	def dtime(self):
		date_time =  datetime.datetime.fromtimestamp(self.time)
		date_time = date_time.strftime('%Y%m%d%H%M%S')
		return date_time


	def utime(self):
		self.time = str(self.time)
		assert len(self.time) == 14,"Argument must be 14 character"
		date_time = datetime.datetime(int(self.time[0:4]),int(self.time[4:6]),int(self.time[6:8]),int(self.time[8:10]),int(self.time[10:12]),int(self.time[12:14]))
		return int(time.mktime(date_time.timetuple()))


if __name__ == "__main__":


	d = convertTime(time = 1443103200)
	print "-----------datetime(dtime) to unixtime(utime)----------"
	print d.dtime()

	u =  convertTime(time = 20150924230000)
	print "-----------unixtime(utime) to datetime(dtime)----------"
	print u.utime()



Execution result


-----------datetime(dtime) to unixtime(utime)----------
201509242300
-----------unixtime(utime) to datetime(dtime)----------
1443103200

Throw the time you want to convert to the convertTime class and convert it with the .utime method or .dtime method. If I have time in the future, I would like to add a function that makes it easy to add and subtract with datetime.

Reference python2.7 datetime

Recommended Posts

I made a class that easily performs unixtime ← → datetime conversion
[python] I made a class that can write a file tree quickly
I made a plug-in "EZPrinter" that easily outputs map PDF with QGIS.
I tried to create a class that can easily serialize Json in Python
[Python] I made a LINE Bot that detects faces and performs mosaic processing.
python unix-time <-> datetime conversion
A story that stumbled when I made a chatbot with Transformer
I made a LINE BOT that returns parrots with Go
Conversion between unixtime and datetime
I made a module that can be glitched easily, but I can't pass arguments from entry_points
I made a fucking app that won't let you skip
I made a rigid Pomodoro timer that works with CUI
I made a python text
I made a discord bot
I made a plug-in that can "Daruma-san fell" with Minecraft
I made a neural network generator that runs on FPGA
I made a library to easily read config files with Python
[Python3] I made a decorator that declares undefined functions and methods.
I made a program that solves the spot the difference in seconds
I made a Twitter bot that mutters Pokemon caught by #PokemonGO
I made a shuffle that can be reset (reverted) with Python
I made a lo command that is more useful than ls
I made a slack bot that notifies me of the temperature
I made a library that adds docstring to a Python stub file.
I made a program that automatically calculates the zodiac with tkinter
I made a C ++ learning site
I made a Line-bot using Python!
I made a CUI-based translation script (2)
I made a wikipedia gacha bot
I made a fortune with Python.
I made a CUI-based translation script
I made a daemon with Python
I made a calendar that automatically updates the distribution schedule of Vtuber
[Python] I made a decorator that doesn't seem to have any use.
I made a web application in Python that converts Markdown to HTML
I made a note of Google colaboratory which can use Spleeter easily.
I made a Discord bot in Python that translates when it reacts
[Python] I made a utility that can access dict type like a path
I made a Chrome extension that displays a graph on an AMeDAS page
I made a simple timer that can be started from the terminal
I made a library konoha that switches the tokenizer to a nice feeling
I made a tool that makes decompression a little easier with CLI (Python3)
[IOS] I made a widget that displays Qiita trends in Pythonista3. [Python]
I made a module PyNanaco that can charge nanaco credit with python
I made something that moves (wider range)
I made a dash docset for Holoviews
I made a payroll program in Python!
I touched "Orator" so I made a note
I made a character counter with Python
Beginner: I made a launcher using dictionary
I made a conversation partner like Siri
I made a script to display emoji
I made a Hex map with Python
I made a life game with Numpy
I made a stamp generator with GAN
I made a browser automatic stamping tool.
After studying Python3, I made a Slackbot
I made a roguelike game with Python
I made a simple blackjack with Python
I made a configuration file with Python
I made a library for actuarial science