time

python时间和日历处理

  1. datetime.date类
    导入date模块: from datetime import date

    • ctime(self) return ctime style string.
      >>> date(2017, 5, 15).ctime()
      'Mon May 15 00:00:00 2017'
      
    • fromtimestamp(cls, timestamp) local date from a POSIX timestamp.
      >>> date.fromtimestamp(1494830875.615)
      datetime.date(2017, 5, 15)
      
    • isocalendar(self) return a 3-tuple containing ISO year, week number, and weekday.
      >>> date(2017, 5, 15).isocalendar()
      (2017, 20, 1)
      
    • isoformat(self) return string in ISO 8601 format, YYYY-MM-DD.
       >>> date(2017, 5, 15).isoformat()
       '2017-05-15'
      
    • isoweekday(self) return the day of the week represented by the date. Monday1 ... Sunday7.
      >>> date(2017, 5, 15).isoweekday()
      1
      
    • replace(self, year=None, month=None, day=None) return date with new specified fields.
      >>> date(2017, 5, 15).replace(day=10)
      datetime.date(2017, 5, 10)
      
    • strftime(self, format) return format style string.
      >>> date(2017, 5, 15).strftime("%Y-%m-%d")
      '2017-05-15'
      
    • timetuple(self) return time tuple. compatible with time.localtime().
      >>> date(2017, 5, 15).timetuple()
      time.struct_time(tm_year=2017, tm_mon=5, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=135, tm_isdst=-1)
      
    • today(cls) return current date or datetime.
      >>> date.today()
      datetime.date(2017, 5, 15
      
    • weekday(self) return the day of week represented by the date. Monday0 ... Sunday6.
      >>> date(2017,5,15).weekday()
      0
      
  2. datetime.time类
    导入time类: from datetime import time

    • isoformat(self) return string in ISO 8601 format, HH:MM:SS[.mmmmmm].
      >>> time(15,13,30).isoformat()
      '15:13:30'
      
    • replace(self, hour=None, minute=None, second=None, microsecond=None, tzinfo=None) return time with new specified fields
      >>> time(15,13,30, 599).replace(second=10)
      datetime.time(15, 13, 10, 599)
      
    • strftime(self, format) return format style string.
      >>> time(15,13,30, 599).strftime("%H:%M:%S")
      '15:13:30'
      
  3. datetime.datetime类
    导入datetime类: from datetime import datetime, date, time

    • combine(cls, date, time) return date + time.
      >>> datetime.combine(date(2017, 5, 15), time(15, 36, 20))
      datetime.datetime(2017, 5, 15, 15, 36, 20)
      
    • ctime(self) return ctime style string.
      >>> datetime(2017, 5, 15, 15, 25, 20).ctime()
      'Mon May 15 15:25:20 2017'
      
    • date(self) return date object with same year, month and day.
      >>> datetime(2017, 5, 15, 15, 25, 20).date()
      datetime.date(2017, 5, 15)
      
    • fromtimestamp(self, timestamp) return local time from POSIX timestamp.
      >>> datetime.fromtimestamp(1494833491.141)
      datetime.datetime(2017, 5, 15, 15, 31, 22, 68000)
      
    • isoformat(self, sep='T') return string in ISO 8601 format, YYY-MM-DDTHH:MM:SS[.mmmmmm]. sep is used to separate the year from time. and defaults to "T".
      >>> datetime(2017, 5, 15, 15, 26, 20).isoformat(sep="T")
      '2017-05-15T15:26:20'
      
    • now(cls, tz=None) return local date and time.
      >>> datetime.now()
      datetime.datetime(2017, 5, 15, 15, 38, 25, 563000)
      
    • replace(self, year=None, month=None, day=None, hour=None, minute=None, second=None, microsecond=None, tzinfo=None) return datetime with new specified fields.
      >>> datetime(2017, 5, 15, 15, 29, 30).replace(year=2019, month=2)
      datetime.datetime(2019, 2, 15, 15, 29, 30)
      
    • strptime(cls, date_string, format) return new datetime parsed from a string.
      >>> datetime.strptime("2017-05-15 15:20:20", "%Y-%m-%d %H:%M:%S")
      datetime.datetime(2017, 5, 15, 15, 20, 20)
      
    • time(self) return time object with same time but with tzinfo=None.
      >>> datetime(2017, 5, 15, 15, 29, 30).time()
      datetime.time(15, 29, 30)
      
    • timetuple(self) return time tuple.
      >>> datetime(2017, 5, 15, 15, 29, 30).timetuple()
      time.struct_time(tm_year=2017, tm_mon=5, tm_mday=15, tm_hour=15, tm_min=29, tm_sec=30, tm_wday=0, tm_yday=135, tm_isdst=-1)
      
    • utcfromtimestamp(self, timestamp) return UTC datetime from a POSIX timestamp.
      >>> datetime.utcfromtimestamp(1494834531.324)
      datetime.datetime(2017, 5, 15, 7, 48, 43, 100000)
      
    • utcnow(cls) return a new datetime representing UTC day and time.
      >>> datetime.utcnow()
      datetime.datetime(2017, 5, 15, 7, 50, 8, 171000)
      
    • utctimetuple(self) return UTC time tuple.
      >>> datetime.utcnow().utctimetuple()
      time.struct_time(tm_year=2017, tm_mon=5, tm_mday=15, tm_hour=7, tm_min=50, tm_sec=44, tm_wday=0, tm_yday=135, tm_isdst=0)
      
  4. time模块
    导入time模块: import time

    • asctime(p_tuple=None) convert a time tuple to a string, when the time tuple not present, current times as return by localtime().
      >>> time.asctime()
      'Mon May 15 16:13:52 2017'
      
      >>> time.asctime(datetime.now().timetuple())
      'Mon May 15 16:12:21 2017'
      
    • clock() return the CPU time or real time since the start of the process or since the first call to clock(). This has as much precision as the system records.
      >>> time.clock()
      1102.1906228205012
      
    • ctime(seconds=None) convert a time in seconds since Epoch to a string in local time.
      >>> time.ctime()
      'Mon May 15 16:20:44 2017'
      
    • gmtime(seconds=None) convert seconds since the Epoch to a time tuple expressing UTC.when seconds is not passed in, convert the current time instead.
      >>> time.gmtime()
      time.struct_time(tm_year=2017, tm_mon=5, tm_mday=15, tm_hour=8, tm_min=24, tm_sec=3, tm_wday=0, tm_yday=135, tm_isdst=0)
      
    • localtime(seconds=None) convert seconds since the Epoch to a time tuple expressing local time. where seconds is not passed in, convert the current time instead.
      >>> time.localtime()
      time.struct_time(tm_year=2017, tm_mon=5, tm_mday=15, tm_hour=16, tm_min=26, tm_sec=28, tm_wday=0, tm_yday=135, tm_isdst=0)
      
    • mktime(p_tuple) convert a time tuple in local time to seconds since the Epoch.
      >>> time.mktime(time.localtime())
      1494836918.0
      
      >>> time.mktime(time.gmtime())
      1494808126.0
      
    • sleep(seconds) delay execution for a given number of seconds
      >>> time.sleep(1)
      
    • strftime(format, p_tuple=None) convert a time tuple to a string according to a format specification.when the time tuple is not present, current time as returned by localtime() is used.
      >>> time.strftime("%Y-%m-%d %H:%M:%S")
      '2017-05-15 16:34:20'
      
      >>> time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
      '2017-05-15 16:34:35'
      
    • strptime(string, format) parse a string to a time tuple according to a format specification.
      >>> time.strptime("2017-05-15 15:20:12", "%Y-%m-%d %H:%M:%S")
      time.struct_time(tm_year=2017, tm_mon=5, tm_mday=15, tm_hour=15, tm_min=20, tm_sec=12, tm_wday=0, tm_yday=135, tm_isdst=-1)
      
    • time() return the current time in seconds since the Epoch.
      >>> time.time()
      1494837455.418
      
  5. timedelta类
    表示时间间隔,即两个时间点之间的长度.时间相加减得到timedelta对象. timedelta对象可以与datetime对象相加减得到的是datetime对象.

    • datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0) 构造函数.
    • 获取相对当前时间,前一天的日期.
      >>> datetime.now() + timedelta(days=-1)
      datetime.datetime(2017, 5, 14, 17, 7, 25, 303000)
      
      >>> datetime.now() - timedelta(days=1)
      datetime.datetime(2017, 5, 14, 17, 7, 41, 430000)
      
    • 计算两个时间,相差的秒数.
      >>> (datetime(2017, 5, 15, 15, 15, 15) - datetime(2017, 5, 15, 16, 16, 16)).total_seconds()
      -3661.0
      
  6. calendar模块
    导入calendar模块: import calendar, calendar是第三方库, 使用前需要安装pip install calendar

    • isleap(year) return True for leap year, False for non-leap year.
      >>> calendar.isleap(2017)
      False
      >>> calendar.isleap(2020)
      True
      
    • leapdays(y1, y2) return number of leap years in range[y1, y2). assume y1 <= y2
      >>> calendar.leapdays(2017, 2021)
      1
      
    • weekday(year, month, day) return weekday(0-6 ~ Mon-Sun)
      >>> calendar.weekday(2017, 5, 15)
      0
      
    • monthrange(year, month) return weekday(0-6 ~ Mon-Sun) and number of days(28-31)
      >>> calendar.monthrange(2017, 5)
      (0, 31)
      
posted @ 2017-06-22 22:25  zane_zong  阅读(130)  评论(0编辑  收藏  举报