Skip to content

Instantly share code, notes, and snippets.

@marchildmann
Created December 7, 2022 17:21
Show Gist options
  • Select an option

  • Save marchildmann/ee2ce2e80c147063ab122794f6e6de88 to your computer and use it in GitHub Desktop.

Select an option

Save marchildmann/ee2ce2e80c147063ab122794f6e6de88 to your computer and use it in GitHub Desktop.
SQLite calendar
CREATE TABLE IF NOT EXISTS calendar (
d date UNIQUE NOT NULL,
days_in_month INT NOT NULL,
day_of_week INT NOT NULL,
weekday TEXT NOT NULL,
quarter INT NOT NULL,
year INT NOT NULL,
month INT NOT NULL,
day INT NOT NULL
);
INSERT
OR ignore INTO calendar (d, days_in_month, day_of_week, weekday, quarter, year, month, day)
SELECT *
FROM (
WITH RECURSIVE dates(d) AS (
VALUES('1980-01-01')
UNION ALL
SELECT date(d, '+1 day')
FROM dates
WHERE d < '2039-01-01'
)
SELECT d,
CAST(JULIANDAY(STRFTIME('%Y-%m-01', d), '+1 month') - JULIANDAY(STRFTIME('%Y-%m-01', d)) AS INTEGER) as days_in_month,
(CAST(strftime('%w', d) AS INT) + 6) % 7 AS day_of_week,
CASE
(CAST(strftime('%w', d) AS INT) + 6) % 7
WHEN 0 THEN 'Monday'
WHEN 1 THEN 'Tuesday'
WHEN 2 THEN 'Wednesday'
WHEN 3 THEN 'Thursday'
WHEN 4 THEN 'Friday'
WHEN 5 THEN 'Saturday'
ELSE 'Sunday'
END AS weekday,
CASE
WHEN CAST(strftime('%m', d) AS INT) BETWEEN 1 AND 3 THEN 1
WHEN CAST(strftime('%m', d) AS INT) BETWEEN 4 AND 6 THEN 2
WHEN CAST(strftime('%m', d) AS INT) BETWEEN 7 AND 9 THEN 3
ELSE 4
END AS quarter,
CAST(strftime('%Y', d) AS INT) AS year,
CAST(strftime('%m', d) AS INT) AS month,
CAST(strftime('%d', d) AS INT) AS day
FROM dates
);
@marchildmann
Copy link
Author

Generates a simple table with some useful values like "days_in_month" or "quarter" for a given date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment