Calendar module

#usr/bin/python3
#! -*-conding : utf-8 -*-


#2018.3.14
"""
Calendar module
The functions of this module are all calendar related, such as printing a character monthly calendar for a certain month.
Monday is the default first day of the week and Sunday is the default last day. To change the setting, call the calendar.setfirstweekday() function. The module contains the following built-in functions:
"""
import time
import calendar

# calendar.calendar(year,w=2,l=1,c=6) returns a year calendar in multi-line string format, 3 months per line, and the interval distance is c. The daily width interval is w characters. The length of each line is 21* W+18+2* C. l is the number of rows per week. 
print (calendar.calendar(2018,w=2,l=1,c=6 ))

# calendar.firstweekday( ) Returns the current weekly start date setting. By default, 0 is returned when the caendar module is first loaded, which is Monday. 
print (calendar.firstweekday())

# calendar.isleap(year) returns True for leap years, false otherwise. 
print (calendar.isleap(2016 ))

# calendar.leapdays(y1,y2) Returns the total number of leap years between Y1 and Y2 
print (calendar.leapdays(1999,2018 ))

# calendar.month(year,month,w=2,l=1) Returns a calendar year month month month in a multi-line string format, with two header lines and one line per week. The daily width interval is w characters. The length of each line is 7*w+6. l is the number of rows per week. 
print (calendar.month(2018,3,w=2,l=1 ))

# calendar.monthcalendar(year,month) Returns a single-level nested list of integers. Each sublist is loaded with an integer representing a week. Years outside the month month are set to 0; days within the range are indicated by the day of the month, starting at 1. 
print (calendar.monthcalendar(2018,3 ,))

# calendar.monthrange(year,month) returns two integers. The first is the date code for the day of the week for the month, and the second is the date code for the month. Day is from 0 (Monday) to 6 (Sunday); month is from 1 to 12. 
print (calendar.monthrange(2018,4 ))

# calendar.prcal(year,w=2,l=1,c=6) is equivalent to print calendar.calendar(year,w,l,c).

# calendar.prmonth(year,month,w=2,l=1) is equivalent to print calendar.calendar(year,w,l,c).

# calendar.setfirstweekday(weekday) Set the start date code of the week. 0 (Monday) to 6 (Sunday). 
print (calendar.setfirstweekday(5 ))
 print (calendar.month(2018,3,w=2,l=1 ))

# calendar.timegm(tupletime) Opposite of time.gmtime: accepts a time tuple as a time tuple and returns the time stamp (floating-point seconds elapsed since the 1970 epoch) at that moment. 
print (calendar.timegm(time.localtime()))

# calendar.weekday(year,month,day) Returns the date code for the given day. 0 (Monday) to 6 (Sunday). Month is 1 (January) to 12 (December) 
print (calendar.weekday(2018,6,23 ))



# calendar in python

#Return a month of the specified year 
def get_month(year, month):
     return calendar.month(year, month)

#Return the calendar of the specified year 
def get_calendar(year):
     return calendar.calendar(year)

#Determine whether a year is a leap year, if so, return True, if not, return False 
def is_leap(year):
     return calendar.isleap(year)

#Return the first day of the weekday of a month and all the days of the month 
def get_month_range(year, month):
     return calendar.monthrange(year, month)

#Return a sequence of a month with each week as an element 
def get_month_calendar(year, month):
     return calendar.monthcalendar(year, month)

def main():
    year = 2016
    month = 11
    test_month = get_month(year, month)
    print(test_month)
    print('#' * 50)
    #print(get_calendar(year))
    print('{0}这一年是否为闰年?:{1}'.format(year, is_leap(year)))
    print(get_month_range(year, month))
    print(get_month_calendar(year, month))

if __name__ == '__main__':
    main()

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325122820&siteId=291194637