0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python3 > datetime > カレンダの特定日に印を付ける (C++ BuilderのTCalendarのARow, ACol計算用)

Last updated at Posted at 2019-07-19
動作環境
ideone (Python 3 (python 3.5)

概要

  • C++ Builderで設計中の機能をPython実装でさっと確認
  • 年を通した366の要素にあらかじめマークを付け、それをもとにカレンダの該当箇所をマーク付けする
    • C++ BuilderのTCalenderのACol, ARowの計算に使う

備考

曜日の定義は

実装

# [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

備考

確認は済んだので、このコードはここまで。

さらに良いコードにする時間は今回は取らない。

関連

C++ Builderに関してはDateUtilsを使う。

0
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?