Format Dates Calendar.Txt Style
Format your dates Calendar.txt style, everywhere! Like 2024-01-04 w01 Thu.
I printed every day from year 1700 to 2400, using Python 3, Go, PHP, Django templates and 'date'. Then I checked that results match.
# Python
Django Templates and Go are a bit more verbose, read the full programs below. Or download them all (calendartxt-date-2024-01-04.zip, 5.5 MB).
Looking for Calendar.txt? This article is just about date formats. If you want to keep your calendar in a plain text file, head to Calendar.txt
Test output # ↑
For each language or command, a test script is generated.
Test script prints each date to standard output.
- Start date: 1700-01-01 w53 Fri (inclusive)
- End date: 2400-01-01 w52 Sat (inclusive)
Rationale for the testing period
- Must be considerably longer than the lifetime of any living person now (2024).
- Should not go near the edge of clearly defined dates, e.g. dates outside gregorian calendar. This is to avoid irrelevant errors outside the range of real-life usable dates.
Rationale for dates
- Start date 1700-01-01 w53 Fri
- 1700 is much later than "Inter gravissimas" bulla in 1582
- 1700 is over 300 years ago
- Conveniently, it's on w53 (most years have only 52 weeks)
- End date 2400-01-01 w52 Sat
- 2400 is over 300 years into the future
Corrrect output file # ↑
The correct output file is dategen-go.txt. 4.7 MB, 255 670 dates, 255 670 lines. SHA256 hash is 343b12bae2b459f0bfe2f676f7350210fae9a19aa54f756cc0a4063098b29b37.
When test output is saved into a file, this is the correct result.
The file ends with a newline "\n". There are no other empty lines. There must be no whitespace at the end of lines.
$
343b12bae2b459f0bfe2f676f7350210fae9a19aa54f756cc0a4063098b29b37 out/dategen-django.txt
343b12bae2b459f0bfe2f676f7350210fae9a19aa54f756cc0a4063098b29b37 out/dategen-go.txt
343b12bae2b459f0bfe2f676f7350210fae9a19aa54f756cc0a4063098b29b37 out/dategen-php.txt
343b12bae2b459f0bfe2f676f7350210fae9a19aa54f756cc0a4063098b29b37 out/dategen-python.txt
343b12bae2b459f0bfe2f676f7350210fae9a19aa54f756cc0a4063098b29b37 out/dategen-sh.txt
As all correct output files are identical, we can further look at any one file
$
255670 dategen-go.txt
So it has 255 670 lines, each representing a date.
$ ;
1700-01-01 w53 Fri
2400-01-01 w52 Sat
Debugging incorrect output # ↑
If sha256sum does not match, the file is incorrect.
A count of diff lines gives an idea if all lines are bad. If all are bad, you can look at the first line to see if there is a major problem. Also, white space at the end of line is a hard to see candidate.
diff correct.txt new.txt |wc -l
If only some lines are incorrect, check if zero padding in weeks is missing.
In addition to these obvious things, there could be incorrect or weird date calculations.
Full Date Format Programs # ↑
Python - dategen.py # ↑
#!/usr/bin/python3
# Copyright 2024 Tero Karvinen https://TeroKarvinen.com
=
=
=
# "%W" is not ISO8601 week, as it includes week zero (0).
# Correct ISO8601 week is "%V", https://bugs.python.org/issue12006 fixed in 2015.
+=
PHP - dategen.php # ↑
Bash / date - dategen.sh # ↑
#!/usr/bin/bash
# Copyright 2024 Tero Karvinen https://TeroKarvinen.com
STARTDATE="1700-01-01" # first date, inclusive, YYYY-MM-DD, e.g. "1700-01-01"
# STARTDATE="2399-12-01" # for testing the last date
ENDDATE="2400-01-02" # one day past last date, YYYY-MM-DD, it's inclusive "2400-01-01"
# OUTFILE="out/dategen-sh-out.txt"
FORMAT="%Y-%m-%d w%V %a"
DATE=
# echo "" > "$OUTFILE"
until [
do
# echo "$DATE" >> "$OUTFILE"
DATE=
done
Golang Go - dategen-go.go # ↑
// Copyright 2020-2024 Tero Karvinen http://TeroKarvinen.com
package main
Django Templates - dategen-django.py # ↑
Django Template solution is not that pretty.
#!/usr/bin/python3
# Copyright 2024 Tero Karvinen https://TeroKarvinen.com
# sudo apt-get install python3-django
# django-admin --version # prints "3.2.19"
## Set up Single file Django
## Print dates
=
# end = datetime(1700, 1, 18)
=
=
# https://docs.djangoproject.com/en/3.2/ref/templates/builtins/#date picks, dashes added, reordered """
# - Uses a similar format to PHP’s date() function with some differences.
# - o ISO-8601 week-numbering year, corresponding to the ISO-8601 week number (W) which uses leap weeks. See Y for the more common year format. '1999'
# - W SO-8601 week number of year, with weeks starting on Monday. 1, 53
# """
# Not the same as in PHP. This works in PHP "Y-m-d \wW D", but lacks week zero padding in Django.
# It seems that Django template filter "date" does not have a zero padded week number as of 2024-01-04 w01 Thu.
# To get zero padded ISO week number, 'day | date:"W"' prints week number as a string without padding "2",
# which is then converted to integer '| add:"0"' and padded with leading zero 'stringformat:"02d"'
# "Y" seems to be the correct year for ISO8601 dates. The suggested "o" gives nonsensical results.
=
=
=
+=
Next Steps # ↑
Keep your calendar in a plain text file, head to Calendar.txt.
Adminstrivia # ↑
2024-01-11 w02 Thu: fixed some typos.
