Skip to content

Commit cee028e

Browse files
committed
update interface
1 parent 90293d4 commit cee028e

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

demo/dateutils_test.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,26 @@
100100
time_date = dateutils.date(time_year, time_month, time_day) # 2021-07-17
101101
res = dateutils.dateadd(dateutils.dateadd(dateutils.now(), 10), 10) # 2022-03-03 11:13:02
102102

103-
print(dateutils.to_quarter(time_str))
103+
print(dateutils.to_quarter(time_str)) # <DateQuarter-2022,3Q>
104104
time_str2 = "2022-07-28"
105105

106106
q1 = dateutils.to_quarter(time_str)
107107
q2 = dateutils.to_quarter(time_str2)
108-
print(q1 < time_str2)
109-
print(q1 + 1)
108+
print(q1 < time_str2) # False
109+
print(q1 + 1) # <DateQuarter-2022,4Q>
110110

111-
# print(list(DateQuarter.between(q1, q2)))
112-
print(list(dateutils.between_quarter(time_str, time_str2, include_last=True)))
113111

112+
time_str = "2022-07-17"
113+
q1 = dateutils.to_quarter(time_str)
114+
print(q1.year) # 2022
115+
print(q1.quarter) # 3
116+
117+
print(q1.start_date) # 2022-07-01
118+
print(q1.end_date) # 2022-09-30
119+
120+
print("2022-8-28" in q1) # True
121+
122+
print(list(dateutils.quarters_within("2021-03-28", "2022-07-17", include_last=True))) # [<DateQuarter-2021,1Q>, ...,<DateQuarter-2022,3Q>]
123+
124+
print(dateutils.quarter_from_yq(2022, 4)) # <DateQuarter-2022,4Q>
125+
print(dateutils.quarter_from_ym(2022, 4)) # <DateQuarter-2022,2Q>

seatable_api/date_utils.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class DateQuarter:
6767
_year = 0
6868
_quarter = 0
6969

70-
def __init__(self, year: int, quarter: int):
70+
def __init__(self, year, quarter):
7171
year = year + (quarter-1) // 4
7272
quarter = (quarter-1) % 4 + 1
7373

@@ -187,25 +187,29 @@ def __sub__(self, other):
187187
else:
188188
raise ArithmeticError()
189189

190+
@property
190191
def year(self):
191192
return self._year
192193

194+
@property
193195
def quarter(self):
194196
return self._quarter
195197

196198
def days(self):
197-
start = self.start_date()
198-
end = self.end_date()
199+
start = self.start_date
200+
end = self.end_date
199201
curr = start
200202
while curr <= end:
201203
yield curr
202204
curr = curr + datetime.timedelta(days=1)
203205

206+
@property
204207
def start_date(self):
205208
return datetime.date(year=self._year, month=(self._quarter-1)*3+1, day=1)
206209

210+
@property
207211
def end_date(self):
208-
return (self+1).start_date() - datetime.timedelta(days=1)
212+
return (self+1).start_date - datetime.timedelta(days=1)
209213

210214
@classmethod
211215
def between(cls, start, end, include_last=False):
@@ -408,11 +412,18 @@ def isomonth(self, time_str):
408412
date_str = self.date(year, month, 1)
409413
return date_str[0:-3]
410414

415+
def quarter_from_yq(self, year, quarter):
416+
return DateQuarter(year, quarter)
417+
418+
def quarter_from_ym(self, year, month):
419+
dt = self.date(year, month, 1)
420+
return self.to_quarter(dt)
421+
411422
def to_quarter(self, time_str):
412423
dt_obj, _ = _str2datetime(time_str)
413424
return DateQuarter.from_date(dt_obj)
414425

415-
def between_quarter(self, time_start, time_end, include_last=False):
426+
def quarters_within(self, time_start, time_end, include_last=False):
416427
q_start = self.to_quarter(time_start)
417428
q_end = self.to_quarter(time_end)
418429
return DateQuarter.between(q_start, q_end, include_last=include_last)

0 commit comments

Comments
 (0)