#!/usr/bin/env python3 """Read vor.txt and generate vor.md, inserting dates if missing. Port of the PHP 5.4 script: on PHP < 7 the date is produced by strftime('%A, %d.%m.%Y ') with the de_DE locale, i.e. day and month are zero-padded ("Dienstag, 04.11.2026 "). """ from datetime import date, datetime, timedelta, timezone FILE_TXT = 'vor.txt' FILE_MD = 'vor.md' TUESDAY = 1 # date.weekday(): Monday == 0 WEDNESDAY = 2 def last_sunday(year, month): """Last Sunday of a 31-day month (March / October).""" d = date(year, month, 31) return d - timedelta(days=(d.weekday() + 1) % 7) def cet_today(): """Today's date in PHP's "CET" zone (= CET/CEST with EU summer time). Python 3.6 has no zoneinfo. EU summer time runs from the last Sunday of March to the last Sunday of October, switching at 01:00 UTC. """ now = datetime.now(timezone.utc).replace(tzinfo=None) start = datetime(now.year, 3, last_sunday(now.year, 3).day, 1, 0) end = datetime(now.year, 10, last_sunday(now.year, 10).day, 1, 0) offset = 2 if start <= now < end else 1 return (now + timedelta(hours=offset)).date() def nth_weekday(first_of_month, weekday, n): """n-th (1-based) given weekday counted from the first of the month. Like PHP's "fourth tuesday of this month": if n is too large for the month, the result overflows into the next month. """ offset = (weekday - first_of_month.weekday()) % 7 return first_of_month + timedelta(days=offset + 7 * (n - 1)) def add_month(d): """First-of-month date -> first day of the following month.""" if d.month == 12: return date(d.year + 1, 1, 1) return date(d.year, d.month + 1, 1) def get_tuesday(first_of_month): """Fourth Tuesday of the month, like strftime('%A, %d.%m.%Y ') in de_DE.""" t = nth_weekday(first_of_month, TUESDAY, 4) # Always a Tuesday, so no locale needed for the weekday name. return 'Dienstag, {:d}.{:d}.{:4d} '.format(t.day, t.month, t.year) def generate(lines, today): """Return the lines of vor.md for the content `lines` of vor.txt.""" # Header arr = ['| | | |', '|:------|:------|:------|'] first_day = today.replace(day=1) ts = first_day first_wedn = nth_weekday(first_day, WEDNESDAY, 1) number = 4 # Treat March differently if (first_wedn.month, first_wedn.day) == (3, 1): number = 5 tues_date = nth_weekday(first_day, TUESDAY, number) wed_date = tues_date + timedelta(days=1) cond = True if first_day.month in (2, 3): cond = (first_day != first_wedn and (tues_date.month, tues_date.day) != (3, 22)) # Are we past the fourth Tuesday of this month? if today >= wed_date and cond: ts = add_month(ts) for line in lines: # Skip empty lines if not line: continue # Remove empty fields too (PHP's array_filter also drops "0") fields = [x for x in line.split('|') if x not in ('', '0')] # Get next Tuesday independent of PHP version tuesday = get_tuesday(ts) if len(fields) == 2: # Date missing, insert fields.insert(0, tuesday) ts = add_month(ts) li = '|'.join(fields) # Insert leading and trailing spaces if required if li[:1] != ' ': li = ' ' + li if li[-1:] != ' ': li = li + ' ' arr.append('|' + li + '|') return arr def read_lines(path): """Like PHP file($path, FILE_IGNORE_NEW_LINES): split on \\n only.""" with open(path, encoding='utf-8', errors='surrogateescape', newline='\n') as f: lines = [] for line in f: if line.endswith('\n'): line = line[:-1] if line.endswith('\r'): line = line[:-1] lines.append(line) return lines def main(): arr = generate(read_lines(FILE_TXT), cet_today()) with open(FILE_MD, 'w', encoding='utf-8', errors='surrogateescape', newline='\n') as f: f.write('\n'.join(arr) + '\n') if __name__ == '__main__': main()