技术文章和资源

技术文章(时间排序)

热门类别

Python PHP MySQL JDBC Linux

Python 日历模块:yeardayscalendar() 方法

pythonserver side programmingprogramming

简介

在 Python 编程领域,日历模块是管理日期和时间操作的灵活工具包。在这个模块中,yeardayscalendar() 方法像一颗特殊的宝石一样低调地闪耀着光芒。与传统的日历函数不同,该方法通过将天组织成周来提供一种新的视角,宣传了一种理解时间流逝的可选方法。在本文中,我们将深入探讨 yeardayscalendar() 方法的深度,揭示其重点、应用以及从周角度管理日历时提供的独特体验。

探索模块 `yeardayscalendar()` 方法?

Python 日历模块以其处理世界信息的能力而闻名,即 yeardayscalendar() 方法。此方法通过将重点从月移至周,提供了一种与日历交互的另类但聪明的方法。在日历与月系统同义的世界中,此方法打开了一个查看时间的特殊门户。

在其核心,yeardayscalendar() 方法将一年的日历生成为基于周的格子列表。每个格子代表一个月的天数,分布在几周内。这种可选过程在处理遵循周模式的基于时间的数据时特别有用。人们可以将其比作看到形成更大瞬时图景的周的马赛克。

星期一到星期日的星期数字为 0-6。它默认将星期一作为一周的开始(ISO 8601)。

语法

语法是−

calendar.yeardayscalendar(year, yeardayslist(可选))

yeardayscalendar() 内部处理对齐月历、定位周的开始和结束、确定月份长度等复杂性。返回的 3D 矩阵为访问和呈现年历提供了方便的数据结构。模块化结构允许根据需要轻松访问月份、星期和日期数据。

参数和返回值

yeardayscalendar() 的参数为 −

  • year − 年份为四位整数

  • yeardayslist − 一年中每一天的星期数列表

它返回一个带有年度日历表示的 3D 矩阵。外部列表表示月份,中间列表表示星期,内部列表表示星期

示例 1

import calendar

year = 2023

cal_obj = calendar.Calendar()

year_layout = cal_obj.yeardayscalendar(year,2)

for quarter in year_layout:
   for month_group in quarter:
      # Print the quarter layout
      print(month_group)

输出

[[0, 0, 0, 0, 0, 0, 1], [2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15], [16, 17, 18, 19, 20, 21, 22], [23, 24, 25, 26, 27, 28, 29], [30, 31, 0, 0, 0, 0, 0]]
[[0, 0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26], [27, 28, 0, 0, 0, 0, 0]]
[[0, 0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26], [27, 28, 29, 30, 31, 0, 0]]
[[0, 0, 0, 0, 0, 1, 2], [3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16], [17, 18, 19, 20, 21, 22, 23], [24, 25, 26, 27, 28, 29, 30]]
[[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 21], [22, 23, 24, 25, 26, 27, 28], [29, 30, 31, 0, 0, 0, 0]]
[[0, 0, 0, 1, 2, 3, 4], [5, 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24, 25], [26, 27, 28, 29, 30, 0, 0]]
[[0, 0, 0, 0, 0, 1, 2], [3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16], [17, 18, 19, 20, 21, 22, 23], [24, 25, 26, 27, 28, 29, 30], [31, 0, 0, 0, 0, 0, 0]]
[[0, 1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12, 13], [14, 15, 16, 17, 18, 19, 20], [21, 22, 23, 24, 25, 26, 27], [28, 29, 30, 31, 0, 0, 0]]
[[0, 0, 0, 0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 0]]
[[0, 0, 0, 0, 0, 0, 1], [2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15], [16, 17, 18, 19, 20, 21, 22], [23, 24, 25, 26, 27, 28, 29], [30, 31, 0, 0, 0, 0, 0]]
[[0, 0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26], [27, 28, 29, 30, 0, 0, 0]]
[[0, 0, 0, 0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31]]

示例 2

import calendar

year = 2023

cal_obj = calendar.Calendar()

year_layout = cal_obj.yeardayscalendar(year)

for quarter_idx, quarter in enumerate(year_layout, start=1):
   print(f"Quarter {quarter_idx}")
   for month_group in quarter:
      for week in month_group:
         week_str = ""
         for day in week:
            if day == 0:
               week_str += "   "  # Print spaces for days that belong to adjacent months
            else:
               week_str += f"{day:2} "
         print(week_str)
      print()

输出

Quarter 1
                   1 
 2  3  4  5  6  7  8 
 9 10 11 12 13 14 15 
16 17 18 19 20 21 22 
23 24 25 26 27 28 29 
30 31                

       1  2  3  4  5 
 6  7  8  9 10 11 12 
13 14 15 16 17 18 19 
20 21 22 23 24 25 26 
27 28                

       1  2  3  4  5 
6  7  8  9 10 11 12 
13 14 15 16 17 18 19 
20 21 22 23 24 25 26 
27 28 29 30 31       

Quarter 2
                1  2 
 3  4  5  6  7  8  9 
10 11 12 13 14 15 16 
17 18 19 20 21 22 23 
24 25 26 27 28 29 30 

 1  2  3  4  5  6  7 
 8  9 10 11 12 13 14 
15 16 17 18 19 20 21 
22 23 24 25 26 27 28 
29 30 31             

          1  2  3  4 
 5  6  7  8  9 10 11 
12 13 14 15 16 17 18 
19 20 21 22 23 24 25 
26 27 28 29 30       

Quarter 3
                1  2 
 3  4  5  6  7  8  9 
10 11 12 13 14 15 16 
17 18 19 20 21 22 23 
24 25 26 27 28 29 30 
31                   

    1  2  3  4  5  6 
 7  8  9 10 11 12 13 
14 15 16 17 18 19 20 
21 22 23 24 25 26 27 
28 29 30 31          

1  2  3 
 4  5  6  7  8  9 10 
11 12 13 14 15 16 17 
18 19 20 21 22 23 24 
25 26 27 28 29 30    

Quarter 4
                   1 
 2  3  4  5  6  7  8 
 9 10 11 12 13 14 15 
16 17 18 19 20 21 22 
23 24 25 26 27 28 29 
30 31                

       1  2  3  4  5 
 6  7  8  9 10 11 12 
13 14 15 16 17 18 19 
20 21 22 23 24 25 26 
27 28 29 30          

             1  2  3 
 4  5  6  7  8  9 10 
11 12 13 14 15 16 17 
18 19 20 21 22 23 24 
25 26 27 28 29 30 31 

结论

Python 日历模块中的 yeardayscalendar() 方法将给定年份的星期几数字转换为表示全年日历的 3D 矩阵。这为在各种应用程序中访问、处理和呈现年度日历提供了一种方便的数据结构。通过在内部封装日历对齐逻辑,yeardayscalendar() 简化了在 Python 中以编程方式使用年度日历的过程。它是需要结构化年历数据的日历生成和日期计算的有用工具。


相关文章