23.09

Преобразование даты в Python из datetime в time и обратно

Ну очень редко использую модуль time. Небольшая шпаргалка.

from datetime import datetime
import time

#-------------------------------------------------
# conversions to strings
#-------------------------------------------------
# datetime object to string
dt_obj = datetime(2008, 11, 10, 17, 53, 59)
date_str = dt_obj.strftime("%Y-%m-%d %H:%M:%S")
print date_str

# time tuple to string
time_tuple = (2008, 11, 12, 13, 51, 18, 2, 317, 0)
date_str = time.strftime("%Y-%m-%d %H:%M:%S", time_tuple)
print date_str

#-------------------------------------------------
# conversions to datetime objects
#-------------------------------------------------
# time tuple to datetime object
time_tuple = (2008, 11, 12, 13, 51, 18, 2, 317, 0)
dt_obj = datetime(*time_tuple[0:6])
print repr(dt_obj)

# date string to datetime object
date_str = "2008-11-10 17:53:59"
dt_obj = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
print repr(dt_obj)

# timestamp to datetime object in local time
timestamp = 1226527167.595983
dt_obj = datetime.fromtimestamp(timestamp)
print repr(dt_obj)

# timestamp to datetime object in UTC
timestamp = 1226527167.595983
dt_obj = datetime.utcfromtimestamp(timestamp)
print repr(dt_obj)

#-------------------------------------------------
# conversions to time tuples
#-------------------------------------------------
# datetime object to time tuple
dt_obj = datetime(2008, 11, 10, 17, 53, 59)
time_tuple = dt_obj.timetuple()
print repr(time_tuple)

# string to time tuple
date_str = "2008-11-10 17:53:59"
time_tuple = time.strptime(date_str, "%Y-%m-%d %H:%M:%S")
print repr(time_tuple)

# timestamp to time tuple in UTC
timestamp = 1226527167.595983
time_tuple = time.gmtime(timestamp)
print repr(time_tuple)

# timestamp to time tuple in local time
timestamp = 1226527167.595983
time_tuple = time.localtime(timestamp)
print repr(time_tuple)

#-------------------------------------------------
# conversions to timestamps
#-------------------------------------------------
# time tuple in local time to timestamp
time_tuple = (2008, 11, 12, 13, 59, 27, 2, 317, 0)
timestamp = time.mktime(time_tuple)
print repr(timestamp)

#-------------------------------------------------
# results
#-------------------------------------------------
# 2008-11-10 17:53:59
# 2008-11-12 13:51:18
# datetime.datetime(2008, 11, 12, 13, 51, 18)
# datetime.datetime(2008, 11, 10, 17, 53, 59)
# datetime.datetime(2008, 11, 12, 13, 59, 27, 595983)
# datetime.datetime(2008, 11, 12, 21, 59, 27, 595983)
# (2008, 11, 10, 17, 53, 59, 0, 315, -1)
# (2008, 11, 10, 17, 53, 59, 0, 315, -1)
# (2008, 11, 12, 21, 59, 27, 2, 317, 0)
# (2008, 11, 12, 13, 59, 27, 2, 317, 0)
# 1226527167.0

 

Оригинал: http://www.saltycrane.com/blog/2008/11/python-datetime-time-conversions/

Раздел: Разное Тэги: datetime, python, time

Коментарии

#115.06.2012, 01:14 serbinef пишет:

Предприятие "Регион 58" рекомендует великолепное бизнес предложение - персональный мебельный бизнес . Для рационального и плодотворного применения торговых площадей за которую не придется платить. Мебельный бизнес куда прибыльней сдавания торговых площадей в аренду. Мы предложим Вам ряд убедительныхпричин .
http://postavkimebeli.ru/

#213.07.2012, 18:41 rorglursefsd пишет:

Home Employed Mortgage Loan Girl some sort of Mortgage Whenever Youre Home Employed <a href=http://5ktenix.payday-loanso.co.uk>payday loans columbus ga</a> 3. Higher rate loan products on your home. You can find loan companies firms would you permit you to lend dollars from and so they would likely require the actual assets, that is certainly your home. In order to away substantial curiosity about your current mortgage and also you must spend these upwards from the of their time. This will drive that you fork out instantly or maybe get rid of your home to help property foreclosure. You will drop big money with this. This can be additionally which would a person incur an undesirable credit history so you shed your house. <a href=http://5ktenix.payday-loanso.co.uk>payday loan for bad credit</a> pod dva. Government Loans - All the government government is certainly in addition outstanding source of the financing alternatives. You could visit all the office or perhaps the website affecting UNITED STATES Simple Organization Administration (#( SBA )#) for you to look for insignificant company loans this carry good prices with desire lady flexible payment terms. http://5ktenix.payday-loanso.co.uk Buyer can possibly spend all the major with dollars - The particular buyer is able to spend the particular change female zyf into the seller.

#324.04.2017, 22:25 MichaelSeexy пишет:

Вы в этом уверенны? Особенно гугл и фейсбук

Оставьте комментарий

© 2010 Алексей Камедов

При копировании материалов блога ссылка на источник обязательна.