-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate-pretty.1m.py
executable file
·170 lines (146 loc) · 6.44 KB
/
date-pretty.1m.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python
import datetime
import calendar
from lunarcalendar.converter import Converter, Solar, Lunar
import os.path
import pickle
from itertools import cycle
today = datetime.datetime.today()
# today = datetime.datetime(2019, 3, 31)
today_date = today.date()
year = today.year
month = today.month
def pstatus_bar():
'''print status bar'''
bitbar="|font=iosevka size=14 color=white dropdown=false"
birthdays = {"0102": "Som"}
gap_numbers = {0: "!", 1: "'", 2: "╎", 3: "┆", 4: "┊"}
is_important= False
lunar_year = Lunar.from_date(today_date).year
is_leap = (lunar_year % 4 == 0 and (lunar_year % 100 != 0 or lunar_year % 400 == 0))
days_this_year = 366 if is_leap else 365
for lunar_date in birthdays.keys():
lunar_month, lunar_day = int(lunar_date[-4:-2]), int(lunar_date[-2:])
lunar = Lunar(lunar_year, lunar_month, lunar_day, is_leap)
solar = Converter.Lunar2Solar(lunar)
if solar.to_date() < today_date:
lunar.year += 1
solar = Converter.Lunar2Solar(lunar)
gap = (datetime.date(solar.year, solar.month, solar.day) - today_date).days
if gap <= 4:
print(f"{birthdays[lunar_date]}🎂{gap_numbers[gap]}{today.hour:02d}:{today.minute:02d}{bitbar}")
if not is_important:
is_important = True
# father's day and mother's day
festivals = {5: ["Mom"], 6: ["Dad"]}
if month == 5 or month == 6:
for idx, solar_date in enumerate(solar2fest[month]):
gap = (solar_date - today_date).days
if 0 <= gap <= 4:
print(f"{festivals[month][idx]}🍰{gap_numbers[gap]}{today.hour:02d}:{today.minute:02d}{bitbar}")
if not is_important:
is_important = True
if not is_important:
print(f"{list(calendar.day_abbr)[today.weekday()]}{today.day:02d} {today.hour:02d}:{today.minute:02d}{bitbar}")
def pdropdown():
'''print dropdown'''
cal = calendar.Calendar()
cal.setfirstweekday(calendar.SUNDAY)
to_show = []
# print("<u>星期.7 星期.1 星期.2 星期.3 星期.4 星期.5 星期.6</u>" + bitbar)
# print("--~~~"*6 + "--~~" + bitbar)
to_show.append("<u>星期.7 星期.1 星期.2 星期.3 星期.4 星期.5 星期.6</u>")
s = ""
c_day = ("","初一","初二","初三","初四","初五",
"初六","初七","初八","初九","初十",
"十一","十二","十三","十四","十五",
"十六","十七","十八","十九","二十",
"廿一","廿二","廿三","廿四","廿五",
"廿六","廿七","廿八","廿九","三十"
)
c_mon = ("","正","二","三","四","五","六","七","八","九","十","十一","腊")
count_days_to_show = 0
for weekday, day, date in zip(cycle(cal.iterweekdays()), cal.itermonthdays(year, month), cal.itermonthdates(year, month)):
count_days_to_show += 1
if date in solar2fest:
lunar_calendar_info = solar2fest[date][0][:2]
if day == today.day:
s += f"<font color=\"yellow\">{day:02d}{lunar_calendar_info}</font> "
else:
s += f"{day:02d}{lunar_calendar_info} "
else:
solar = Solar.from_date(date)
lunar = Converter.Solar2Lunar(solar)
if lunar.day == 1:
lunar_calendar_info = c_mon[lunar.month] + "月"
else:
lunar_calendar_info = c_day[lunar.day]
if day == today.day:
s += f"<font color=\"yellow\">{day:02d}{lunar_calendar_info}</font> "
elif day == 0:
s += f"<font color=\"gray\">{date.day:02d}{lunar_calendar_info}</font> "
else:
s += f"{day:02d}{lunar_calendar_info} "
if weekday == calendar.SATURDAY:
# s += bitbar
# print(s)
to_show.append(s)
s = ""
to_show = [pstring.rstrip() for pstring in to_show]
bitbar = "| font='FiraMono' size=6 color=white"
if count_days_to_show != 42:
print(bitbar)
bitbar = "| font='FiraMono' size=14 color=white"
print("\\n".join(to_show) + bitbar)
print(bitbar)
bitbar = "| font='FiraMono' size=16 color=white"
lunar = Lunar.from_date(today_date)
lunar_year = lunar.year
lunar_month = lunar.month
lunar_day = lunar.day
if today_date in solar2fest:
today_festival = "╎".join(solar2fest[today_date])
print(f"{year}-{month:02d}-{today.day:02d}╎农历{get_lunar_year_name(lunar_year)}年{c_mon[lunar_month]}月{c_day[lunar_day]}╎{today_festival}{bitbar}")
else:
print(f"{year}-{month:02d}-{today.day:02d}╎农历{get_lunar_year_name(lunar_year)}年{c_mon[lunar_month]}月{c_day[lunar_day]}{bitbar}")
def get_lunar_year_name(lunar_year):
"""return Heavenly Stem and Earthly Branch of this year"""
tiangan = "甲乙丙丁戊己庚辛壬癸"
dizhi = "子丑寅卯辰巳午未申酉戌亥"
# 公元4年是一个甲子年
# -4表示从甲子年又过了ly个年头,ly%10:表示从天干上算过了几个天干,如果为0表示天干还是甲,如果为1表示到了乙
ly = (lunar_year - 4) % 60
return tiangan[ly % 10] + dizhi[ly % 12]
def get_festival(month, cache_name):
"""return lunar festivals"""
from lunarcalendar.festival import zh_festivals, FatherDay, MotherDay
from lunarcalendar.solarterm import zh_solarterms
sol2fes = {}
for fest in zh_festivals + zh_solarterms:
solar_date = fest(year)
if solar_date.month == month:
if solar_date in sol2fes:
sol2fes[solar_date].append(fest.get_lang('zh_hans'))
else:
sol2fes[solar_date] = [fest.get_lang('zh_hans')]
sol2fes['month'] = month
if month == 5:
sol2fes[5] = [MotherDay(year)]
elif month == 6:
sol2fes[6] = [FatherDay(year)]
with open(cache_name, 'wb') as handle:
pickle.dump(sol2fes, handle, protocol=pickle.HIGHEST_PROTOCOL)
return sol2fes
if __name__ == "__main__":
cache_name = ".lunardate.cache"
global solar2fest
if os.path.exists(cache_name):
with open(cache_name, 'rb') as handle:
solar2fest = pickle.load(handle)
if month != solar2fest['month']:
solar2fest = get_festival(month, cache_name)
else:
solar2fest = get_festival(month, cache_name)
print('---')
pstatus_bar()
pdropdown()