Long path to Go Python 5

Source: Internet
Author: User
Tags local time timedelta


Time & DateTime Module

Time Module

Import TimePrint("Time Applications". Center (80,"*"))Print(Time.process_time ())#measuring processor operation time, not including sleep timePrint(Time.time ())#Timestamp , 1970 time up to now, in secondsPrint(Time.gmtime ())#sturct time in UTC (GMT)Print("---------local time-----------")Print(Time.altzone)#returns the time difference from UTC, calculated in secondsPrint(Time.localtime ())#returns the struct time object format for local timesPrint(Time.localtime (Time.time ()))#returns the struct time object format for local timesPrint(Time.localtime (Time.time () +3600*3))#modifies the local time and returns the format of the struct date objectT1=Time.localtime (Time.time ())Print(T1.tm_year,t1.tm_yday)#Year-to-day month -to-dayPrint("-----------Time Format------------")#return time format Week month day h:m:s yearPrint(Time.asctime ())Print(Time.asctime (Time.localtime ()))Print(Time.ctime ())Print("----------Date string to turn time object to timestamp-----------")#Date string goto time ObjectStruct_time = Time.strptime ("2016-11-11 23:30","%y-%m-%d%h:%m")#struct_time = Time.strptime ("16-11-11 23:30", "%y-%m-%d%h:%m")Print(struct_time)#The time object is acquired, but it cannot be operated on, and it has to be converted into a timestamp to operateStruct_time_stamp = Time.mktime (struct_time)#Turn time stampPrint(Struct_time_stamp)Print("-----------Timestamp to time object to string-----------")#Timestamp to time objectStruct_time2 =time.localtime (Struct_time_stamp)Print(struct_time2)#time object to stringString_time = Time.strftime ("%y_%m_%d_%h_%m.log", Struct_time2)Print(String_time)
View Code

Time Applications ******************************0.1560011494746244.919801Time.struct_time (Tm_year=2017, tm_mon=5, tm_mday=14, tm_hour=7, tm_min=17, tm_sec=24, tm_wday=6, tm_yday=134, tm_isdst=0)---------local time------------3600Time.struct_time (Tm_year=2017, tm_mon=5, tm_mday=14, Tm_hour=8, tm_min=17, tm_sec=24, tm_wday=6, tm_yday=134, tm_isdst=1) time.struct_time (tm_year=2017, tm_mon=5, tm_mday=14, Tm_hour=8, tm_min=17, tm_sec=24, tm_wday=6, tm_yday=134, tm_isdst=1) time.struct_time (tm_year=2017, tm_mon=5, tm_mday=14, tm_hour=11, tm_min=17, tm_sec=24, tm_wday=6, tm_yday=134, tm_isdst=1)134----------Time Format------------Sun May14 08:17:24 2017Sun May14 08:17:24 2017Sun May08:17:24----------Date string to time object to timestamp-----------Time.struct_time (Tm_year=2016, tm_mon=11, tm_mday=11, tm_hour=23, tm_min=30, tm_sec=0, tm_wday=4, tm_yday=316, Tm_isdst=-1)1478907000.0----------Timestamp to time object to string-----------Time.struct_time (Tm_year=2016, tm_mon=11, tm_mday=11, tm_hour=23, tm_min=30, tm_sec=0, tm_wday=4, tm_yday=316, tm_isdst=0) 2016_11_11_23_30.log
View Code

DateTime module

ImportdatetimePrint("datetime Applications". Center (80,"*"))Print("---------------local time----------------")Print(Datetime.datetime.now ())#Current local timePrint(Datetime.datetime.fromtimestamp (Time.time ()))#Timestamp direct to date format#Print (Datetime.datetime.fromtimestamp (time.time ()-3600)) # Timestamp direct to date formatPrint("---------------time Operation----------------")#Time ArithmeticPrint(Datetime.datetime.now () + Datetime.timedelta (days = 3))#Current time + 3 daysPrint(Datetime.datetime.now ()-Datetime.timedelta (days = 3))#Current Time-3 daysPrint(Datetime.datetime.now () + Datetime.timedelta (hours = 3))#Current time + 3 hoursPrint(Datetime.datetime.now () + datetime.timedelta (minutes = 30))#Current time + 30 minutesPrint("---------------time to replace----------------")#Time Substitutionnow =Datetime.datetime.now ()Print(Now.replace (month=1,day=3))
View Code
  datetime applications  ****************************---------------  local time  ----------------2017-05-14 08:17:24.9708032017-05-14 08:17:24.970804---------------  time Operation  ---------- ------2017-05-17 08:17:24.9708032017-05-11 08:17:24.9708032017-05-14 11:17:24.9708032017-05-14 08:47:24.970803---- -----------  Time Replace  ----------------2017-01-03 08:17:24.970803
View Code

String & Timestamp Conversions:

String---------------------------------------> Time Object (stuct_time)---------------> Timestamp (stuct_time_stamp)

time.strptime ("Date string Contents", "date format to be transferred") Time.mktime (stuct_time)

Timestamp--------------------------------------> Time Object (stuct_time)----------------> string (string_time)

Time.gmtime (Stuct_time_stamp) time.strftime ("Want to go date format", Stuct_time)


Time.localtime (Stuct_time_stamp)

Long path to Go Python 5

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.