time module
-
功能:构建结构化时间struct_time;生成时间戳;结构化时间和时间戳之间的互相转换;生成格式化时间字符串;推迟程序的运行进程;
-
常用函数:
- time.time() >> secs 时间戳是指从1970年1月1日开始按秒计算的偏移量。
- time.localtime([secs]) >> struct_time
- time.gmtime([secs])
- time.mktime(structime) >> secs
- time.asctime([structtime]) >> string
- time.ctime([secs]) >> string
- time.perf_counter() >> timer
- time.sleep(secs)
- time.strftime(format, struct_time)
- time.strptime(string, format) >> struct_time
- time.struct_time(timetuple) >> struct_time
struct_time 的values可以使用index和value_name来获取
datetime module
功能:对日期时间对象进行增减运算;时间生成;时间的格式化输出。
date object, timedelta object, datetime object, time object
timedelta object
- timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
- timedelta.total_seconds()
date object
class methods:
- date.today()
- date.fromtimestamp(timestamp)
- date.fromisoformat(string)
- date.fromordinal(ordinal)
instance methods:
- date.isoformat()
- date.ctime()
- date.strftime(format)
- date.replace(year=self.year, month=self.month, day=self.day)
- date.timetuple() >> struct_time
- date.toordinal() >> ordinal
- date.weekday()
- date.isoweekday()
datetime object
- datetime(year=0, month=0, day=0, hour=0, minute=0, second=0, microsecond=0, zinfo=None, *, fold=0)
class methods:
- datetime.today()
- datetime.now(tz=None)
- datetime.utcnow()
- datetime.fromtimestamp(timestamp, tz=None)
- datetime.fromordinal(ordinal)
- datetime.combine(date, time, tzinfo=self.tzinfo)
- datetime.fromisoformat(datetime_string)
- datetime.strptime(datetimestring, format)
instance methods:
- datetime.time()
- datetime.date()
- datetime.timetuple()
- datetime.utctimetuple()
- datetime.repalce(year=self.year, month=self.month, day=self.day, hour=self.hour, minute=self.minute, second=self.second, …)
- datetime.timestamp()
- datetime.weekday()
- datetime.isoweekday()
- datetime.isoformat(sep=‘T’, timespec=‘auto’)
- datetime.ctime()
- datetime.strftime(format)
time object
- time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)
- time.isoformat()
- time.fromisoformat(string)
- time.replace(hour=self.hour, minute=self.minute, second=self.second, …)