forked from 6tail/lunar-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolarMonth.py
More file actions
85 lines (71 loc) · 2.14 KB
/
SolarMonth.py
File metadata and controls
85 lines (71 loc) · 2.14 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# -*- coding: utf-8 -*-
from . import Solar, SolarWeek
from .util import SolarUtil
class SolarMonth:
"""
阳历月
"""
def __init__(self, year, month):
self.__year = year
self.__month = month
@staticmethod
def fromDate(date):
return SolarMonth(date.year, date.month)
@staticmethod
def fromYm(year: int, month: int):
return SolarMonth(year, month)
def getYear(self):
return self.__year
def getMonth(self):
return self.__month
def toString(self):
return "%d-%d" % (self.__year, self.__month)
def toFullString(self):
return "%d年%d月" % (self.__year, self.__month)
def __str__(self):
return self.toString()
def getDays(self):
"""
获取本月的阳历日期列表
:return: 阳历日期列表
"""
days = []
d = Solar.fromYmd(self.__year, self.__month, 1)
days.append(d)
for i in range(1, SolarUtil.getDaysOfMonth(self.__year, self.__month)):
days.append(d.next(i))
return days
def getWeeks(self, start):
"""
获取本月的阳历日期列表
:param start: 星期几作为一周的开始,1234560分别代表星期一至星期天
:return: 阳历日期列表
"""
weeks = []
week = SolarWeek.fromYmd(self.__year, self.__month, 1, start)
while True:
weeks.append(week)
week = week.next(1, False)
first_day = week.getFirstDay()
if first_day.getYear() > self.__year or first_day.getMonth() > self.__month:
break
return weeks
def next(self, months):
"""
获取往后推几个月的阳历月,如果要往前推,则月数用负数
:param months: 月数
:return: 阳历月
"""
n = 1
if months < 0:
n = -1
m = abs(months)
y = self.__year + int(m / 12) * n
m = self.__month + m % 12 * n
if m > 12:
m -= 12
y += 1
elif m < 1:
m += 12
y -= 1
return SolarMonth.fromYm(y, m)