Between the "leap" determination date and string conversions occur

A time string format conversions:

1, switch time format string time

  from time import strptime

  >>> s_time = strptime("2020-01-10 09:29:15", "%Y-%m-%d %H:%M:%S")

  >>> print(s_time)
  time.struct_time(tm_year=2020, tm_mon=1, tm_mday=10, tm_hour=9, tm_min=29, tm_sec=15, tm_wday=4, tm_yday=10, tm_isdst=-1)

  >>> print(s_time.tm_year)

  2020

 

  >>> s_time = strptime("2020-01", "%Y-%m")

  >>> print(s_time)
  time.struct_time(tm_year=2020, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=1, tm_isdst=-1)
  >>> print(s_time.tm_year)
  2020

2, time to String

  >>> from time import strptime, strftime, localtime

  >>> print(localtime())
  time.struct_time(tm_year=2020, tm_mon=1, tm_mday=10, tm_hour=9, tm_min=35, tm_sec=42, tm_wday=4, tm_yday=10, tm_isdst=0)
  >>> print(localtime().tm_year, localtime().tm_mday)
  2020 10

  >>> print(type(strftime("%Y-%m-%d %H:%M:%S", localtime())))
  <class 'str'>

  >>> print(strftime("%m-%d-%Y %H:%M:%S", localtime()))
  01-10-2020 09:42:49
  >>> print(type(strftime("%m-%d-%Y %H:%M:%S", localtime())))
  <class 'str'>

Second, leap year judgment

  import calendar

  from datetime import date

  from time import strptime, strftime, localtime

  calendar of isleap can return True (leap year) and False (not a leap year)

  >>> print(calendar.isleap(2012))

  True

  >>> print(calendar.isleap(date.today().year))
  True

  >>> print(calendar.isleap(localtime().tm_year))
  True

  ss = "2013"

  >>> print(calendar.isleap(datetime.datetime.strptime(ss, "%Y").year))
  False

Guess you like

Origin www.cnblogs.com/xinyu602/p/12174497.html