1.time模块基础示例
#encoding: utf8
import time
import datetime
def main():
t = (2017, 5, 15, 17, 3, 38, 1, 48, 0)
print dir(time)
print "time.gmtime(): ",time.gmtime() # time.struct_time(tm_year=2017, tm_mon=5, tm_mday=15, tm_hour=4, tm_min=10, tm_sec=57, tm_wday=0, tm_yday=135, tm_isdst=0)
print "time.time(): ", time.time()
print "time.clock(): ", time.clock()
print "time.sleep(): ..."
print "time.localtime(): ",time.localtime()# time.struct_time(tm_year=2017, tm_mon=5, tm_mday=15, tm_hour=12, tm_min=29, tm_sec=42, tm_wday=0, tm_yday=135, tm_isdst=0)
print "time.asctime(): ",time.asctime() # convert time tuple to string
print "time.ctime(): ",time.ctime() # convert time in seconds to string
print "time.mktime(): ",time.mktime(t)
print "time.strftime(): ", time.strftime("%Y-%m-%d %H:%M:%S %p %a", time.gmtime())
print "time.strptime(): ", time.strptime(time.ctime(), "%a %b %d %H:%M:%S %Y")
print time.strftime('%X %x %Z').decode('gbk') #str
print "-----struct_time: "
print dir(time.gmtime())
print "year: (for example, 1993)",time.gmtime().tm_year
print "day of year:range[1,366]",time.gmtime().tm_yday
print "day of week:range[0-6]", time.gmtime().tm_wday
print "seconds:range[0-61]", time.gmtime().tm_sec
print "month of year:range[1-12]", time.gmtime().tm_mon
print "minutes:range[0-59]", time.gmtime().tm_min
print "hours:range[1-23]", time.gmtime().tm_hour
print "1 if summer time is in effect, 0 if not, and -1 if unknown", time.gmtime().tm_isdst
if __name__ == '__main__':
main()
2.运行结果:
3.datetime模块基础示例
#encoding: utf8
import time
import datetime
def datetime_main():
print "dir datetime: ", dir(datetime)
# output: ['MAXYEAR', 'MINYEAR', '__doc__', '__name__', '__package__', 'date', 'datetime', 'datetime_CAPI', 'time', 'timedelta', 'tzinfo']
print "MAXYEAR : ",datetime.MAXYEAR
print "MINYEAR : ",datetime.MINYEAR
print "date of datetime: ",datetime.date(year=2017, month=5,day=15)
print "dir date of datetime: ", dir(datetime.date)
print "--------method of date: "
print "ctime of date: ", datetime.date.ctime(datetime.date(year=2017, month=5,day=15))
print "day of date: ", datetime.date(year=2017, month=5,day=15).day
print "month of date: ", datetime.date(year=2017, month=5,day=15).month
print "year of date: ", datetime.date(year=2017, month=5,day=15).year
print "weekday of date: ", datetime.date(year=2017, month=5,day=16).weekday()
print "today of date: ", datetime.date.today()
print "max of date: ",datetime.date.max
print "min of date: ",datetime.date.min
print "timetuple of date: ", datetime.date.timetuple(datetime.date(year=2017, month=5,day=15))
print "strftime of date: ", datetime.date.strftime(datetime.date(year=2017, month=5,day=15), '%Y-%m-%d')
print "toordinal of date: ", datetime.date.toordinal(datetime.date(year=2017, month=5,day=15))
print "isoweekday of date: ", datetime.date.isoweekday(datetime.date(year=2017, month=5,day=15))
print "isoformat of date: ", datetime.date.isoformat(datetime.date(year=2017, month=5,day=15))
print "isocalendar of date: ", datetime.date.isocalendar(datetime.date(year=2017, month=5,day=15))
print "fromordinal of date: ", datetime.date.fromordinal(datetime.date.toordinal(datetime.date(year=2017, month=5,day=15)))
print "fromordinal of date: ", datetime.date.fromtimestamp(time.time())
print "replace of date: ", datetime.date.replace(datetime.date(year=2017, month=5,day=15), year=2011)
print "--------method of datetime: "
print "dir datetime of datetime: ",dir(datetime.datetime)
print "today of datetime: ",datetime.datetime.today()
print "now of datetime: ", datetime.datetime.now()
print "ctime of datetime: ", datetime.datetime(year=2017, month=5, day=15, hour=14, minute=38, second=56).ctime() #str
print "date of datetime: ", datetime.datetime(year=2017, month=5, day=15, hour=14, minute=38, second=56).date()
print "fromtimestamp of datetime: ", datetime.datetime.fromtimestamp(time.time())
print "combine of datetime: ", datetime.datetime.combine(datetime.date(year=2017, month=5,day=15), datetime.time())
print "isoformat of datetime: ", datetime.datetime.isoformat(datetime.datetime(year=2017, month=5, day=15, hour=14, minute=38, second=56))
print "replace of datetime: ", datetime.datetime.replace(datetime.datetime(year=2017, month=5, day=15, hour=14, minute=38, second=56), year=2001)
print "strptime of datetime: ", datetime.datetime.strptime(time.ctime(), "%a %b %d %H:%M:%S %Y")
print "time of datetime: ", datetime.datetime.time(datetime.datetime.now())
print "timetuple of datetime: ", datetime.datetime.timetuple(datetime.datetime.now())
print "timetz of datetime: ", datetime.datetime.timetz(datetime.datetime.now())
print "utcfromtimestamp of datetime: ", datetime.datetime.utcfromtimestamp(time.time())
print "utcnow of datetime: ", datetime.datetime.utcnow()
print "utctimetuple of datetime: ", datetime.datetime.utctimetuple(datetime.datetime.now())
print "hour of datetime: ", datetime.datetime.now().hour
print "microsecond of datetime: ", datetime.datetime.now().microsecond
print "minute of datetime: ", datetime.datetime.now().minute
print "tzinfo of datetime: ", datetime.datetime.now().tzinfo
print "second of datetime: ", datetime.datetime.now().second
print "--------method of time: "
print "dir of time: ", dir(datetime.time)
print "isoformat of time: ", datetime.time(hour=15,minute=07,second=40).isoformat()
print "strftime of time: ", datetime.time(hour=15,minute=07,second=40).strftime('%H-%M-%S')
print "replace of time: ", datetime.time(hour=15,minute=07,second=40).replace(hour=14)
print "--------method of timedelta: "# 表示时间间隔,即两个时间点之间的长度
print "dir of time: ", dir(datetime.timedelta)
print "max of timedelta: ", datetime.timedelta.max
print "min of timedelta: ", datetime.timedelta.min
print "days of timedelta: ", datetime.timedelta(days=100).days
print "one thousand days of now: ", datetime.datetime.now() - datetime.timedelta(days=100)
print "one min later of now: ", datetime.datetime.now() + datetime.timedelta(minutes=1)
print "one thousand sec later of now: ", datetime.datetime.now() + datetime.timedelta(seconds=100)
print "the type of time-time: ",type((datetime.datetime.now() + datetime.timedelta(minutes=1)) -(datetime.datetime.now() - datetime.timedelta(days=100)))
print "the total_seconds of time-time: ",((datetime.datetime.now() + datetime.timedelta(minutes=1)) -(datetime.datetime.now() - datetime.timedelta(days=100))).total_seconds()
if __name__ == '__main__':
# main()
datetime_main()
4.运行结果: