Skip to content

Commit beea088

Browse files
authored
fix: Deal with optional month and day (#46)
1 parent 24ba620 commit beea088

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

src/sphinxnotes/any/indexers.py

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,16 @@ def anchor(self, refval: str) -> str:
5959
DISPFMTS_Y = '%Y 年'
6060
DISPFMTS_M = '%m 月'
6161
DISPFMTS_YM = '%Y 年 %m 月'
62-
DISPFMTS_MD = '%m 月 %d 日,%a'
62+
DISPFMTS_DW = '%d 日,%a'
63+
ZEROTIME = strptime('0001', '%Y')
6364

65+
def _safe_strptime(datestr, fmt):
66+
if datestr is None or datestr == '':
67+
return ZEROTIME
68+
try:
69+
return strptime(datestr, fmt)
70+
except ValueError:
71+
return ZEROTIME
6472

6573
class YearIndexer(Indexer):
6674
name = 'year'
@@ -70,15 +78,15 @@ def __init__(
7078
inputfmts: list[str] = INPUTFMTS,
7179
dispfmt_y: str = DISPFMTS_Y,
7280
dispfmt_m: str = DISPFMTS_M,
73-
dispfmt_md: str = DISPFMTS_MD,
81+
dispfmt_dw: str = DISPFMTS_DW,
7482
):
7583
"""*xxxfmt* are date format used by time.strptime/strftime.
7684
7785
.. seealso:: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes"""
7886
self.inputfmts = inputfmts
7987
self.dispfmt_y = dispfmt_y
8088
self.dispfmt_m = dispfmt_m
81-
self.dispfmt_md = dispfmt_md
89+
self.dispfmt_dw = dispfmt_dw
8290

8391
def classify(self, objref: Value) -> list[Category]:
8492
entries = []
@@ -88,22 +96,31 @@ def classify(self, objref: Value) -> list[Category]:
8896
t = strptime(v, datefmt)
8997
except ValueError:
9098
continue # try next datefmt
91-
entries.append(
92-
Category(
99+
100+
if all(x not in datefmt for x in ['%m', '%b', '%B'] ): # missing month
101+
entry = Category(main=strftime(self.dispfmt_y, t))
102+
elif all(x not in datefmt for x in ['%d', '%j']): # missing day
103+
entry = Category(
93104
main=strftime(self.dispfmt_y, t),
94105
sub=strftime(self.dispfmt_m, t),
95-
extra=strftime(self.dispfmt_md, t),
106+
extra='', # TODO: leave it empty, or sub-type will not take effect
96107
)
97-
)
108+
else:
109+
entry = Category(
110+
main=strftime(self.dispfmt_y, t),
111+
sub=strftime(self.dispfmt_m, t),
112+
extra=strftime(self.dispfmt_dw, t),
113+
)
114+
entries.append(entry)
98115
return entries
99116

100117
def sort(
101118
self, data: Iterable[Indexer._T], key: Callable[[Indexer._T], Category]
102119
) -> list[Indexer._T]:
103120
def sort_by_time(x: Category):
104-
t1 = strptime(x.main, self.dispfmt_y)
105-
t2 = strptime(x.sub, self.dispfmt_m) if x.sub else None
106-
t3 = strptime(x.extra, self.dispfmt_md) if x.extra else None
121+
t1 = _safe_strptime(x.main, self.dispfmt_y)
122+
t2 = _safe_strptime(x.sub, self.dispfmt_m)
123+
t3 = _safe_strptime(x.extra, self.dispfmt_dw)
107124
return (t1, t2, t3)
108125

109126
return sorted(data, key=lambda x: sort_by_time(key(x)), reverse=True)
@@ -126,11 +143,11 @@ def __init__(
126143
self,
127144
inputfmts: list[str] = INPUTFMTS,
128145
dispfmt_ym: str = DISPFMTS_YM,
129-
dispfmt_md: str = DISPFMTS_MD,
146+
dispfmt_dw: str = DISPFMTS_DW,
130147
):
131148
self.inputfmts = inputfmts
132149
self.dispfmt_ym = dispfmt_ym
133-
self.dispfmt_md = dispfmt_md
150+
self.dispfmt_dw = dispfmt_dw
134151

135152
def classify(self, objref: Value) -> list[Category]:
136153
entries = []
@@ -140,20 +157,23 @@ def classify(self, objref: Value) -> list[Category]:
140157
t = strptime(v, datefmt)
141158
except ValueError:
142159
continue # try next datefmt
143-
entries.append(
144-
Category(
160+
161+
if all(x not in datefmt for x in ['%d', '%j']): # missing day
162+
entry = Category(main=strftime(self.dispfmt_ym, t))
163+
else:
164+
entry = Category(
145165
main=strftime(self.dispfmt_ym, t),
146-
extra=strftime(self.dispfmt_md, t),
166+
extra=strftime(self.dispfmt_dw, t),
147167
)
148-
)
168+
entries.append(entry)
149169
return entries
150170

151171
def sort(
152172
self, data: Iterable[Indexer._T], key: Callable[[Indexer._T], Category]
153173
) -> list[Indexer._T]:
154174
def sort_by_time(x: Category):
155-
t1 = strptime(x.main, self.dispfmt_ym)
156-
t2 = strptime(x.sub, self.dispfmt_md) if x.sub else None
175+
t1 = _safe_strptime(x.main, self.dispfmt_ym)
176+
t2 = _safe_strptime(x.extra, self.dispfmt_dw)
157177
return (t1, t2)
158178

159179
return sorted(data, key=lambda x: sort_by_time(key(x)), reverse=True)

0 commit comments

Comments
 (0)