動作環境
ideone (Python 3 (python 3.5)
概要
- C++ Builderで設計中の機能をPython実装でさっと確認
- 年を通した366の要素にあらかじめマークを付け、それをもとにカレンダの該当箇所をマーク付けする
- C++ BuilderのTCalenderのACol, ARowの計算に使う
備考
曜日の定義は
- 0(Monday): python
-
https://docs.python.org/3.7/library/calendar.html#calendar.weekday
Returns the day of the week (0 is Monday) for year (1970–…), month (1–12), day (1–31).
-
https://docs.python.org/3.7/library/calendar.html#calendar.weekday
- 1(Sunday): Delphi
実装
# [x] PEP8 check
from datetime import datetime
aymd = datetime(2019, 7, 18, 12, 30, 45)
def getWeekDay(adate):
return (adate.weekday() + 1) % 7 # for 0:Sunday[Delphi]
def getYearDay(adate):
return (adate.timetuple().tm_yday)
def dispCal(alist):
for idx in range(len(alist)):
print(alist[idx], end='')
if idx % 7 == 6:
print() # CRLF
# 1. preparation -------------------------
NUM_YEAR = 366 # maximum number of days in a year (including leap-year)
yrs = [[]]*NUM_YEAR
for idx in range(NUM_YEAR):
yrs[idx] = 0
yrs[getYearDay(datetime(2019, 1, 1, 0, 0, 0))] = 1 # Jan
yrs[getYearDay(datetime(2019, 2, 1, 0, 0, 0))] = 1 # Feb.
yrs[getYearDay(datetime(2019, 3, 1, 0, 0, 0))] = 1 # Mar.
NUM_MONTH = 37 # 6 lines in calendar ( 7 * 5 lines + 2 [30, 31 days])
mts = [[]]*NUM_MONTH
for idx in range(NUM_MONTH):
mts[idx] = 0
# 2. main part -------------------------
adt = datetime(2019, 2, 1, 0, 0, 0) # ymdhns
awday = getWeekDay(adt)
ayday = getYearDay(adt)
for idx in range(31): # 31: maximum for one month
mts[awday + idx] = yrs[ayday + idx]
print('Feb')
dispCal(mts)
結果
Feb
0000010
0000000
0000000
0000000
0000010
00
備考
確認は済んだので、このコードはここまで。
さらに良いコードにする時間は今回は取らない。
関連
- Python
- Delphi (C++ Builder)
- C++ Builder
-
SysUtils.DayOfWeek 関数
-
日曜が週の先頭で,土曜が 7 番めの曜日です。
-
- DateUtils.DayOfTheYear 関数
-
DateUtils.DayOfTheWeek 関数
-
1 は月曜,7 は日曜を示します。
-
-
SysUtils.DayOfWeek 関数
C++ Builderに関してはDateUtilsを使う。