Python 有三個時間/日期的函數庫

  • datetime — Basic date and time types
  • calendar — General calendar-related functions
  • time — Time access and conversions

http://www.wklken.me/posts/2015/03/03/python-base-datetime.html

datetime

import time
from datetime import date
today = date.today()
datetime.date(2007, 12, 5)
today =date.fromtimestamp(time.time())
my_birthday = date(today.year, 6, 24)
if my_birthday < today:
    my_birthday = my_birthday.replace(year=today.year + 1)
time_to_birthday = abs(my_birthday - today)
time_to_birthday.days
datetime.timestamp()

from datetime import datetime, date, time
# Using datetime.combine()
d = date(2005, 7, 14)
t = time(12, 30)
datetime.combine(d, t)
datetime.datetime(2005, 7, 14, 12, 30)
# Using datetime.now() or datetime.utcnow()
datetime.now()

timedelta

A duration expressing the difference between two date, time, or datetime instances to microsecond resolution

#明天
dt=datetime.now() + timedelta(days=1)
print(dt)

ticks

def ticks(dt):
    return (dt - datetime(1, 1, 1)).total_seconds() * 10000000

UTC is Coordinated Universal Time (formerly known as Greenwich Mean Time, or GMT).

from time import gmtime, strftime
time.clock()
time.gmtime([secs])
strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
#'Thu, 28 Jun 2001 14:17:15 +0000'

Timezone Constants

time.altzone
#The offset of the local DST timezone, in seconds west of UTC, if one is defined. This is negative if the local DST timezone is east of UTC (as in Western Europe, including the UK). Only use this if daylight is nonzero. See note below.

time.daylight
#Nonzero if a DST timezone is defined. See note below.

time.timezone
#The offset of the local (non-DST) timezone, in seconds west of UTC (negative in most of Western Europe, positive in the US, zero in the UK). See note below.

time.tzname
#A tuple of two strings: the first is the name of the local non-DST timezone, the second is the name of the local DST timezone.

results matching ""

    No results matching ""