From af0708dbda5b7e548180845b3a7d07195124bd41 Mon Sep 17 00:00:00 2001 From: Liquidmantis Date: Fri, 10 Dec 2021 21:18:37 -0700 Subject: [PATCH 01/11] add date offsets for note templates --- README.md | 4 ++++ lua/telekasten.lua | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 55d3eb9..97853ac 100644 --- a/README.md +++ b/README.md @@ -617,8 +617,12 @@ Currently, the following substitutions will be made during new note creation: | --- | --- | --- | | `{{title}}` | the title of the note | My new note | | `{{date}}` | date in iso format | 2021-11-21 | +| `{{yesterday}}` | yesterday's date in iso format | 2021-11-20 | +| `{{tomorrow}}` | tomorrow's date in iso format | 2021-11-22 | | `{{hdate}}` | date in long format | Sunday, November 21st, 2021 | | `{{week}}` | week of the year | 46 | +| `{{lastweek}}` | prior week of the year | 45 | +| `{{nextweek}}` | next week of the year | 47 | | `{{year}}` | year | 2021 | As an example, this is my template for new notes: diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 4267998..bf48d47 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -194,8 +194,14 @@ local monthmap = { local function calenderinfo_today() local dinfo = os.date("*t") + local oneday = 24 * 60 * 60 -- hours * days * seconds + local oneweek = 7 * oneday + local date_format = "%Y-%m-%d" + local week_format = "%V" local opts = {} - opts.date = os.date("%Y-%m-%d") + opts.date = os.date(date_format) + opts.yesterday = os.date(date_format, os.time() - oneday) + opts.tomorrow = os.date(date_format, os.time() + oneday) local wday = dinfo.wday - 1 if wday == 0 then wday = 7 @@ -211,7 +217,9 @@ local function calenderinfo_today() .. daysuffix(dinfo.day) .. ", " .. dinfo.year - opts.week = os.date("%V") + opts.week = os.date(week_format) + opts.lastweek = os.date(week_format, os.time() - oneweek) + opts.nextweek = os.date(week_format, os.time() + oneweek) opts.month = dinfo.month opts.year = dinfo.year opts.day = dinfo.day @@ -222,6 +230,10 @@ local function linesubst(line, title, calendar_info) local cinfo = calendar_info or calenderinfo_today() local substs = { date = cinfo.date, + yesterday = cinfo.yesterday, + tomorrow = cinfo.tomorrow, + lastweek = cinfo.lastweek, + nextweek = cinfo.nextweek, hdate = cinfo.hdate, week = cinfo.week, year = cinfo.year, From 626b8006763a6d960c1ed751f49aac1873f4a34b Mon Sep 17 00:00:00 2001 From: Liquidmantis Date: Sat, 11 Dec 2021 09:33:21 -0700 Subject: [PATCH 02/11] add iso formatted week tokens --- README.md | 3 +++ lua/telekasten.lua | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/README.md b/README.md index d330e54..fb648e3 100644 --- a/README.md +++ b/README.md @@ -633,6 +633,9 @@ Currently, the following substitutions will be made during new note creation: | `{{week}}` | week of the year | 46 | | `{{lastweek}}` | prior week of the year | 45 | | `{{nextweek}}` | next week of the year | 47 | +| `{{isoweek}}` | week of the year in iso format | 2021-W46 | +| `{{lastisoweek}}` | prior week of the year in iso format | 2021-W45 | +| `{{nextisoweek}}` | next week of the year in iso format| 2021-W47 | | `{{year}}` | year | 2021 | As an example, this is my template for new notes: diff --git a/lua/telekasten.lua b/lua/telekasten.lua index cfe49fd..fcde40d 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -203,6 +203,7 @@ local function calenderinfo_today() local oneweek = 7 * oneday local date_format = "%Y-%m-%d" local week_format = "%V" + local isoweek_format = "%Y-W%V" local opts = {} opts.date = os.date(date_format) opts.yesterday = os.date(date_format, os.time() - oneday) @@ -225,6 +226,9 @@ local function calenderinfo_today() opts.week = os.date(week_format) opts.lastweek = os.date(week_format, os.time() - oneweek) opts.nextweek = os.date(week_format, os.time() + oneweek) + opts.week = os.date(isoweek_format) + opts.lastisoweek = os.date(isoweek_format, os.time() - oneweek) + opts.nextisoweek = os.date(isoweek_format, os.time() + oneweek) opts.month = dinfo.month opts.year = dinfo.year opts.day = dinfo.day From cbb931b5b2f3cd98893040a30b1a9455cc698d4b Mon Sep 17 00:00:00 2001 From: Liquidmantis Date: Sat, 11 Dec 2021 12:13:09 -0700 Subject: [PATCH 03/11] add days of the week date calculations. --- README.md | 4 +- lua/telekasten.lua | 97 +++++++++++++++++++++++++++++++++------------- 2 files changed, 73 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index fb648e3..61a7fc8 100644 --- a/README.md +++ b/README.md @@ -634,8 +634,8 @@ Currently, the following substitutions will be made during new note creation: | `{{lastweek}}` | prior week of the year | 45 | | `{{nextweek}}` | next week of the year | 47 | | `{{isoweek}}` | week of the year in iso format | 2021-W46 | -| `{{lastisoweek}}` | prior week of the year in iso format | 2021-W45 | -| `{{nextisoweek}}` | next week of the year in iso format| 2021-W47 | +| `{{isolastweek}}` | prior week of the year in iso format | 2021-W45 | +| `{{isonextweek}}` | next week of the year in iso format| 2021-W47 | | `{{year}}` | year | 2021 | As an example, this is my template for new notes: diff --git a/lua/telekasten.lua b/lua/telekasten.lua index fcde40d..ef53167 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -197,17 +197,52 @@ local monthmap = { "December", } -local function calenderinfo_today() +local dateformats = { + date = "%Y-%m-%d", + week = "%V", + isoweek = "%Y-W%V" +} + +local function relativedates_today() + local now = os.time() local dinfo = os.date("*t") + local wday = dinfo.wday - 1 local oneday = 24 * 60 * 60 -- hours * days * seconds local oneweek = 7 * oneday - local date_format = "%Y-%m-%d" - local week_format = "%V" - local isoweek_format = "%Y-W%V" + local df = dateformats local opts = {} - opts.date = os.date(date_format) - opts.yesterday = os.date(date_format, os.time() - oneday) - opts.tomorrow = os.date(date_format, os.time() + oneday) + + opts.yesterday = os.date(df.date, now - oneday) + opts.tomorrow = os.date(df.date, now + oneday) + opts.lastweek = os.date(df.week, now - oneweek) + opts.nextweek = os.date(df.week, now + oneweek) + opts.isolastweek = os.date(df.isoweek, os.time() - oneweek) + opts.isonextweek = os.date(df.isoweek, os.time() + oneweek) + + -- Find the Sunday that started this week regardless of the calendar + -- display preference. Then use that as the base to calculate the dates + -- for the days of the current week. + -- Finally, adjust Sunday to suit user calendar preference. + local starting_sunday = now - (wday * oneday) + local sunday_offset = 0 + if M.Cfg.calendar_opts.calendar_monday == 1 then + sunday_offset = 7 + end + opts.monday = os.date(df.date, starting_sunday + (1 * oneday)) + opts.tuesday = os.date(df.date, starting_sunday + (2 * oneday)) + opts.wednesday = os.date(df.date, starting_sunday + (3 * oneday)) + opts.thursday = os.date(df.date, starting_sunday + (4 * oneday)) + opts.friday = os.date(df.date, starting_sunday + (5 * oneday)) + opts.saturday = os.date(df.date, starting_sunday + (6 * oneday)) + opts.sunday = os.date(df.date, starting_sunday + (sunday_offset * oneday)) + + return opts +end + +local function calenderinfo_today() + local dinfo = os.date("*t") + local opts = {} + opts.date = os.date(dateformats.date) local wday = dinfo.wday - 1 if wday == 0 then wday = 7 @@ -223,29 +258,38 @@ local function calenderinfo_today() .. daysuffix(dinfo.day) .. ", " .. dinfo.year - opts.week = os.date(week_format) - opts.lastweek = os.date(week_format, os.time() - oneweek) - opts.nextweek = os.date(week_format, os.time() + oneweek) - opts.week = os.date(isoweek_format) - opts.lastisoweek = os.date(isoweek_format, os.time() - oneweek) - opts.nextisoweek = os.date(isoweek_format, os.time() + oneweek) - opts.month = dinfo.month + opts.week = os.date(dateformats.week) + opts.isoweek = os.date(dateformats.isoweek) opts.year = dinfo.year opts.day = dinfo.day return opts end -local function linesubst(line, title, calendar_info) - local cinfo = calendar_info or calenderinfo_today() +local function linesubst(line, title, calendar_info, relative_dates) + local cinfo = calendar_info or calenderinfo_today() + local rdates = relative_dates or relativedates_today() local substs = { - date = cinfo.date, - yesterday = cinfo.yesterday, - tomorrow = cinfo.tomorrow, - lastweek = cinfo.lastweek, - nextweek = cinfo.nextweek, - hdate = cinfo.hdate, - week = cinfo.week, - year = cinfo.year, + date = cinfo.date, + hdate = cinfo.hdate, + week = cinfo.week, + isoweek = cinfo.isoweek, + year = cinfo.year, + + yesterday = rdates.yesterday, + tomorrow = rdates.tomorrow, + lastweek = rdates.lastweek, + nextweek = rdates.nextweek, + isolastweek = rdates.isolastweek, + isonextweek = rdates.isonextweek, + + sunday = rdates.sunday, + monday = rdates.monday, + tuesday = rdates.tuesday, + wednesday = rdates.wednesday, + thursday = rdates.thursday, + friday = rdates.friday, + saturday = rdates.saturday, + title = title, } for k, v in pairs(substs) do @@ -259,7 +303,8 @@ local function create_note_from_template( title, filepath, templatefn, - calendar_info + calendar_info, + relative_dates ) -- first, read the template file local lines = {} @@ -272,7 +317,7 @@ local function create_note_from_template( -- now write the output file, substituting vars line by line local ofile = io.open(filepath, "a") for _, line in pairs(lines) do - ofile:write(linesubst(line, title, calendar_info) .. "\n") + ofile:write(linesubst(line, title, calendar_info, relative_dates) .. "\n") end ofile:close() From f7b5017d05c15113bfbd72982dd8e37ffdc5fcfa Mon Sep 17 00:00:00 2001 From: Liquidmantis Date: Sat, 11 Dec 2021 12:16:31 -0700 Subject: [PATCH 04/11] restore current month from accidental deletion --- lua/telekasten.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index ef53167..ee736eb 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -260,6 +260,7 @@ local function calenderinfo_today() .. dinfo.year opts.week = os.date(dateformats.week) opts.isoweek = os.date(dateformats.isoweek) + opts.month = dinfo.month opts.year = dinfo.year opts.day = dinfo.day return opts @@ -270,7 +271,7 @@ local function linesubst(line, title, calendar_info, relative_dates) local rdates = relative_dates or relativedates_today() local substs = { date = cinfo.date, - hdate = cinfo.hdate, + hdate =cinfo.hdate, week = cinfo.week, isoweek = cinfo.isoweek, year = cinfo.year, From 1c4783c7cdb27c62cb48b812169fc4f361971644 Mon Sep 17 00:00:00 2001 From: Liquidmantis Date: Sat, 11 Dec 2021 12:52:01 -0700 Subject: [PATCH 05/11] update help txt --- doc/telekasten.txt | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/doc/telekasten.txt b/doc/telekasten.txt index 65b145b..5e8d1e6 100644 --- a/doc/telekasten.txt +++ b/doc/telekasten.txt @@ -667,16 +667,31 @@ that are used for creating new notes. The following substitutions will be made during new note creation: -+-------------+-----------------------+-----------------------------+ -| specifier | | | -| in template | expands to | example | -+-------------+-----------------------+-----------------------------+ -| `{{title}}` | the title of the note | My new note | -| `{{date}}` | date in iso format | 2021-11-21 | -| `{{hdate}}` | date in long format | Sunday, November 21st, 2021 | -| `{{week}}` | week of the year | 46 | -| `{{year}}` | year | 2021 | -+-------------+-----------------------+-----------------------------+ ++-------------------+-----------------------+-----------------------------+ +| specifier | | | +| in template | expands to | example | ++-------------------+-----------------------+-----------------------------+ +| `{{title}}` | the title of the note | My new note | +| `{{date}}` | date in iso format | 2021-11-21 | +| `{{yesterday}}` | yesterday, iso | 2021-11-20 | +| `{{tomorrow}}` | tomorrow, iso | 2021-11-22 | +| `{{hdate}}` | date in long format | Sunday, November 21st, 2021 | +| `{{week}}` | week of the year | 46 | +| `{{lastweek}}` | prior week | 45 | +| `{{nextweek}}` | next week | 47 | +| `{{isoweek}}` | week in iso format | 2021-46 | +| `{{lastisoweek}}` | last week, iso | 2021-45 | +| `{{nextisoweek}}` | next week, iso | 2021-47 | +| `{{year}}` | year | 2021 | +| `{{monday}}` | Monday, iso | 2021-11-15 | +| `{{tuesday}}` | Tuesday, iso | 2021-11-16 | +| `{{wednesday}}` | Wednesday, iso | 2021-11-17 | +| `{{thursday}}` | Thursday, iso | 2021-11-18 | +| `{{friday}}` | Friday, iso | 2021-11-19 | +| `{{saturday}}` | Saturday, iso | 2021-11-20 | +| `{{sunday}}` | Sunday, iso (see note)| 2021-11-21 | ++-------------------+-----------------------+-----------------------------+ +Note: Sunday is adjusted to match users `calendar_monday` preference. As an example, this is my template for new notes: > From 7143729708da9d0e74f56e8fe3a2ee0838acd984 Mon Sep 17 00:00:00 2001 From: Liquidmantis Date: Sat, 11 Dec 2021 12:56:11 -0700 Subject: [PATCH 06/11] fix formatting of template table in help file --- doc/telekasten.txt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/doc/telekasten.txt b/doc/telekasten.txt index 5e8d1e6..c0da577 100644 --- a/doc/telekasten.txt +++ b/doc/telekasten.txt @@ -667,10 +667,10 @@ that are used for creating new notes. The following substitutions will be made during new note creation: -+-------------------+-----------------------+-----------------------------+ -| specifier | | | -| in template | expands to | example | -+-------------------+-----------------------+-----------------------------+ ++-----------------+-----------------------+-----------------------------+ +| specifier | | | +| in template | expands to | example | ++-----------------+-----------------------+-----------------------------+ | `{{title}}` | the title of the note | My new note | | `{{date}}` | date in iso format | 2021-11-21 | | `{{yesterday}}` | yesterday, iso | 2021-11-20 | @@ -690,8 +690,8 @@ The following substitutions will be made during new note creation: | `{{friday}}` | Friday, iso | 2021-11-19 | | `{{saturday}}` | Saturday, iso | 2021-11-20 | | `{{sunday}}` | Sunday, iso (see note)| 2021-11-21 | -+-------------------+-----------------------+-----------------------------+ -Note: Sunday is adjusted to match users `calendar_monday` preference. ++-----------------+-----------------------+-----------------------------+ +Note: Sunday is adjusted to match the user's `calendar_monday` preference. As an example, this is my template for new notes: > From 3a344eb959ca43f96be3a0e491e032113c2bc93d Mon Sep 17 00:00:00 2001 From: Liquidmantis Date: Sat, 11 Dec 2021 15:48:36 -0700 Subject: [PATCH 07/11] add forgotten day of week information to readme --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 61a7fc8..fe66ada 100644 --- a/README.md +++ b/README.md @@ -637,6 +637,14 @@ Currently, the following substitutions will be made during new note creation: | `{{isolastweek}}` | prior week of the year in iso format | 2021-W45 | | `{{isonextweek}}` | next week of the year in iso format| 2021-W47 | | `{{year}}` | year | 2021 | +| `{{monday}}` | monday's date | 2021-11-16 | +| `{{tuesday}}` | tuesday's date | 2021-11-17 | +| `{{wednesday}}` | wednesday's date | 2021-11-18 | +| `{{thursday}}` | thursday's date | 2021-11-19 | +| `{{friday}}` | friday's date | 2021-11-20 | +| `{{saturday}}` | saturday's date | 2021-11-21 | +| `{{sunday}}` | sunday's date | 2021-11-22 | +Note: Sunday will be adjusted by the user's `calendar_monday` preference. As an example, this is my template for new notes: From d39ed91001c253ce9fd3eb94fda88dcfb520d3f5 Mon Sep 17 00:00:00 2001 From: Liquidmantis Date: Sun, 12 Dec 2021 12:56:10 -0700 Subject: [PATCH 08/11] improvements from PR comments --- README.md | 8 +-- doc/telekasten.txt | 10 ++-- lua/telekasten.lua | 127 ++++++++++++++++++++++----------------------- 3 files changed, 71 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index fe66ada..1d852aa 100644 --- a/README.md +++ b/README.md @@ -627,14 +627,14 @@ Currently, the following substitutions will be made during new note creation: | --- | --- | --- | | `{{title}}` | the title of the note | My new note | | `{{date}}` | date in iso format | 2021-11-21 | -| `{{yesterday}}` | yesterday's date in iso format | 2021-11-20 | -| `{{tomorrow}}` | tomorrow's date in iso format | 2021-11-22 | +| `{{prevday}}` | previous day's date in iso format | 2021-11-20 | +| `{{nextday}}` | next day's date in iso format | 2021-11-22 | | `{{hdate}}` | date in long format | Sunday, November 21st, 2021 | | `{{week}}` | week of the year | 46 | -| `{{lastweek}}` | prior week of the year | 45 | +| `{{prevweek}}` | previous week of the year | 45 | | `{{nextweek}}` | next week of the year | 47 | | `{{isoweek}}` | week of the year in iso format | 2021-W46 | -| `{{isolastweek}}` | prior week of the year in iso format | 2021-W45 | +| `{{isoprevweek}}` | previous week of the year in iso format | 2021-W45 | | `{{isonextweek}}` | next week of the year in iso format| 2021-W47 | | `{{year}}` | year | 2021 | | `{{monday}}` | monday's date | 2021-11-16 | diff --git a/doc/telekasten.txt b/doc/telekasten.txt index c0da577..e5267ff 100644 --- a/doc/telekasten.txt +++ b/doc/telekasten.txt @@ -673,15 +673,15 @@ The following substitutions will be made during new note creation: +-----------------+-----------------------+-----------------------------+ | `{{title}}` | the title of the note | My new note | | `{{date}}` | date in iso format | 2021-11-21 | -| `{{yesterday}}` | yesterday, iso | 2021-11-20 | -| `{{tomorrow}}` | tomorrow, iso | 2021-11-22 | +| `{{prevday}}` | previous day, iso | 2021-11-20 | +| `{{nextday}}` | next day, iso | 2021-11-22 | | `{{hdate}}` | date in long format | Sunday, November 21st, 2021 | | `{{week}}` | week of the year | 46 | -| `{{lastweek}}` | prior week | 45 | +| `{{prevweek}}` | previous week | 45 | | `{{nextweek}}` | next week | 47 | | `{{isoweek}}` | week in iso format | 2021-46 | -| `{{lastisoweek}}` | last week, iso | 2021-45 | -| `{{nextisoweek}}` | next week, iso | 2021-47 | +| `{{isoprevweek}}` | last week, iso | 2021-45 | +| `{{isonextweek}}` | next week, iso | 2021-47 | | `{{year}}` | year | 2021 | | `{{monday}}` | Monday, iso | 2021-11-15 | | `{{tuesday}}` | Tuesday, iso | 2021-11-16 | diff --git a/lua/telekasten.lua b/lua/telekasten.lua index ee736eb..fddd9ac 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -174,13 +174,13 @@ local function daysuffix(day) end local daymap = { + "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", - "Sunday", } local monthmap = { "January", @@ -203,27 +203,53 @@ local dateformats = { isoweek = "%Y-W%V" } -local function relativedates_today() - local now = os.time() - local dinfo = os.date("*t") - local wday = dinfo.wday - 1 +local function calculate_dates(cinfo) + local time = os.time(cinfo) -- convert date input to a timestamp + local dinfo = os.date("*t", time) local oneday = 24 * 60 * 60 -- hours * days * seconds local oneweek = 7 * oneday + local oneyear = 365 * oneday local df = dateformats - local opts = {} - opts.yesterday = os.date(df.date, now - oneday) - opts.tomorrow = os.date(df.date, now + oneday) - opts.lastweek = os.date(df.week, now - oneweek) - opts.nextweek = os.date(df.week, now + oneweek) - opts.isolastweek = os.date(df.isoweek, os.time() - oneweek) - opts.isonextweek = os.date(df.isoweek, os.time() + oneweek) + local opts = {} + opts.date = os.date(df.date) + local wday = dinfo.wday - 1 -- compensate for 1-indexed os.date output + opts.hdate = daymap[wday] + .. ", " + .. monthmap[dinfo.month] + .. " " + .. dinfo.day + .. daysuffix(dinfo.day) + .. ", " + .. dinfo.year + opts.month = dinfo.month + opts.year = dinfo.year + opts.day = dinfo.day + + opts.prevday = os.date(df.date, time - oneday) + opts.nextday = os.date(df.date, time + oneday) + opts.week = os.date(df.week, time) + opts.prevweek = os.date(df.week, time - oneweek) + opts.nextweek = os.date(df.week, time + oneweek) + opts.isoweek = os.date(df.isoweek, time) + opts.isoprevweek = os.date(df.isoweek, time - oneweek) + opts.isonextweek = os.date(df.isoweek, time + oneweek) + + -- things get a bit hairy at the year rollover. W01 only starts the first week ofs + -- January if it has more than 3 days. Partial weeks with less than 4 days are + -- considered W52, but os.date still sets the year as the new year, so Jan 1 2022 + -- would appear as being in 2022-W52. That breaks linear linking respective + -- of next/prev week, so we want to put the days of that partial week in + -- January in 2021-W52. This tweak will only change the ISO formatted week string. + if opts.week == 52 and opts.month == 1 then + opts.isoweek = os.date(df.isoweek, time - oneyear) + end -- Find the Sunday that started this week regardless of the calendar -- display preference. Then use that as the base to calculate the dates -- for the days of the current week. -- Finally, adjust Sunday to suit user calendar preference. - local starting_sunday = now - (wday * oneday) + local starting_sunday = time - (wday * oneday) local sunday_offset = 0 if M.Cfg.calendar_opts.calendar_monday == 1 then sunday_offset = 7 @@ -239,57 +265,29 @@ local function relativedates_today() return opts end -local function calenderinfo_today() - local dinfo = os.date("*t") - local opts = {} - opts.date = os.date(dateformats.date) - local wday = dinfo.wday - 1 - if wday == 0 then - wday = 7 - end - if wday == 6 then - wday = 1 - end - opts.hdate = daymap[wday] - .. ", " - .. monthmap[dinfo.month] - .. " " - .. dinfo.day - .. daysuffix(dinfo.day) - .. ", " - .. dinfo.year - opts.week = os.date(dateformats.week) - opts.isoweek = os.date(dateformats.isoweek) - opts.month = dinfo.month - opts.year = dinfo.year - opts.day = dinfo.day - return opts -end - -local function linesubst(line, title, calendar_info, relative_dates) - local cinfo = calendar_info or calenderinfo_today() - local rdates = relative_dates or relativedates_today() +local function linesubst(line, title, calendar_info) + local dates = calculate_dates(calendar_info) local substs = { - date = cinfo.date, - hdate =cinfo.hdate, - week = cinfo.week, - isoweek = cinfo.isoweek, - year = cinfo.year, + hdate = dates.hdate, + week = dates.week, + date = dates.date, + isoweek = dates.isoweek, + year = dates.year, - yesterday = rdates.yesterday, - tomorrow = rdates.tomorrow, - lastweek = rdates.lastweek, - nextweek = rdates.nextweek, - isolastweek = rdates.isolastweek, - isonextweek = rdates.isonextweek, + prevday = dates.prevday, + nextday = dates.nextday, + prevweek = dates.prevweek, + nextweek = dates.nextweek, + isoprevweek = dates.isoprevweek, + isonextweek = dates.isonextweek, - sunday = rdates.sunday, - monday = rdates.monday, - tuesday = rdates.tuesday, - wednesday = rdates.wednesday, - thursday = rdates.thursday, - friday = rdates.friday, - saturday = rdates.saturday, + sunday = dates.sunday, + monday = dates.monday, + tuesday = dates.tuesday, + wednesday = dates.wednesday, + thursday = dates.thursday, + friday = dates.friday, + saturday = dates.saturday, title = title, } @@ -304,8 +302,7 @@ local function create_note_from_template( title, filepath, templatefn, - calendar_info, - relative_dates + calendar_info ) -- first, read the template file local lines = {} @@ -318,7 +315,7 @@ local function create_note_from_template( -- now write the output file, substituting vars line by line local ofile = io.open(filepath, "a") for _, line in pairs(lines) do - ofile:write(linesubst(line, title, calendar_info, relative_dates) .. "\n") + ofile:write(linesubst(line, title, calendar_info) .. "\n") end ofile:close() @@ -1144,7 +1141,7 @@ end -- find today's daily note and create it if necessary. -- local function GotoToday(opts) - opts = opts or calenderinfo_today() + opts = opts or calculate_dates() opts.insert_after_inserting = opts.insert_after_inserting or M.Cfg.insert_after_inserting opts.close_after_yanking = opts.close_after_yanking From 8d920f96503ec250e4093fb85760cdec5d9071be Mon Sep 17 00:00:00 2001 From: Liquidmantis Date: Sun, 12 Dec 2021 15:53:07 -0700 Subject: [PATCH 09/11] resolve some errors with date calculations when creating a note from the calendar. --- lua/telekasten.lua | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index fddd9ac..f48ed5c 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -174,13 +174,13 @@ local function daysuffix(day) end local daymap = { - "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", + "Sunday", } local monthmap = { "January", @@ -212,8 +212,17 @@ local function calculate_dates(cinfo) local df = dateformats local opts = {} - opts.date = os.date(df.date) - local wday = dinfo.wday - 1 -- compensate for 1-indexed os.date output + + -- this is to compensate for the calendar showing M-Su, but os.date Su is + -- always wday = 1 + local wday = dinfo.wday - 1 + if wday == 0 then + wday = 7 + end + + opts.year = dinfo.year + opts.month = dinfo.month + opts.day = dinfo.day opts.hdate = daymap[wday] .. ", " .. monthmap[dinfo.month] @@ -222,10 +231,8 @@ local function calculate_dates(cinfo) .. daysuffix(dinfo.day) .. ", " .. dinfo.year - opts.month = dinfo.month - opts.year = dinfo.year - opts.day = dinfo.day + opts.date = os.date(df.date, time) opts.prevday = os.date(df.date, time - oneday) opts.nextday = os.date(df.date, time + oneday) opts.week = os.date(df.week, time) From 7fadab23b508571e83a8ac62edbbf9be1fede120 Mon Sep 17 00:00:00 2001 From: Liquidmantis Date: Sun, 12 Dec 2021 19:59:56 -0700 Subject: [PATCH 10/11] fix issues with dropped options --- lua/telekasten.lua | 107 +++++++++++++++++++++++---------------------- 1 file changed, 54 insertions(+), 53 deletions(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index f48ed5c..0b19a00 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -203,15 +203,15 @@ local dateformats = { isoweek = "%Y-W%V" } -local function calculate_dates(cinfo) - local time = os.time(cinfo) -- convert date input to a timestamp - local dinfo = os.date("*t", time) +local function calculate_dates(date) + local time = os.time(date) + local dinfo = os.date("*t", time) -- this normalizes the input to a full date table local oneday = 24 * 60 * 60 -- hours * days * seconds local oneweek = 7 * oneday local oneyear = 365 * oneday local df = dateformats - local opts = {} + local dates = {} -- this is to compensate for the calendar showing M-Su, but os.date Su is -- always wday = 1 @@ -220,10 +220,10 @@ local function calculate_dates(cinfo) wday = 7 end - opts.year = dinfo.year - opts.month = dinfo.month - opts.day = dinfo.day - opts.hdate = daymap[wday] + dates.year = dinfo.year + dates.month = dinfo.month + dates.day = dinfo.day + dates.hdate = daymap[wday] .. ", " .. monthmap[dinfo.month] .. " " @@ -232,15 +232,15 @@ local function calculate_dates(cinfo) .. ", " .. dinfo.year - opts.date = os.date(df.date, time) - opts.prevday = os.date(df.date, time - oneday) - opts.nextday = os.date(df.date, time + oneday) - opts.week = os.date(df.week, time) - opts.prevweek = os.date(df.week, time - oneweek) - opts.nextweek = os.date(df.week, time + oneweek) - opts.isoweek = os.date(df.isoweek, time) - opts.isoprevweek = os.date(df.isoweek, time - oneweek) - opts.isonextweek = os.date(df.isoweek, time + oneweek) + dates.date = os.date(df.date, time) + dates.prevday = os.date(df.date, time - oneday) + dates.nextday = os.date(df.date, time + oneday) + dates.week = os.date(df.week, time) + dates.prevweek = os.date(df.week, time - oneweek) + dates.nextweek = os.date(df.week, time + oneweek) + dates.isoweek = os.date(df.isoweek, time) + dates.isoprevweek = os.date(df.isoweek, time - oneweek) + dates.isonextweek = os.date(df.isoweek, time + oneweek) -- things get a bit hairy at the year rollover. W01 only starts the first week ofs -- January if it has more than 3 days. Partial weeks with less than 4 days are @@ -248,8 +248,8 @@ local function calculate_dates(cinfo) -- would appear as being in 2022-W52. That breaks linear linking respective -- of next/prev week, so we want to put the days of that partial week in -- January in 2021-W52. This tweak will only change the ISO formatted week string. - if opts.week == 52 and opts.month == 1 then - opts.isoweek = os.date(df.isoweek, time - oneyear) + if dates.week == 52 and dates.month == 1 then + dates.isoweek = os.date(df.isoweek, time - oneyear) end -- Find the Sunday that started this week regardless of the calendar @@ -261,19 +261,18 @@ local function calculate_dates(cinfo) if M.Cfg.calendar_opts.calendar_monday == 1 then sunday_offset = 7 end - opts.monday = os.date(df.date, starting_sunday + (1 * oneday)) - opts.tuesday = os.date(df.date, starting_sunday + (2 * oneday)) - opts.wednesday = os.date(df.date, starting_sunday + (3 * oneday)) - opts.thursday = os.date(df.date, starting_sunday + (4 * oneday)) - opts.friday = os.date(df.date, starting_sunday + (5 * oneday)) - opts.saturday = os.date(df.date, starting_sunday + (6 * oneday)) - opts.sunday = os.date(df.date, starting_sunday + (sunday_offset * oneday)) + dates.monday = os.date(df.date, starting_sunday + (1 * oneday)) + dates.tuesday = os.date(df.date, starting_sunday + (2 * oneday)) + dates.wednesday = os.date(df.date, starting_sunday + (3 * oneday)) + dates.thursday = os.date(df.date, starting_sunday + (4 * oneday)) + dates.friday = os.date(df.date, starting_sunday + (5 * oneday)) + dates.saturday = os.date(df.date, starting_sunday + (6 * oneday)) + dates.sunday = os.date(df.date, starting_sunday + (sunday_offset * oneday)) - return opts + return dates end -local function linesubst(line, title, calendar_info) - local dates = calculate_dates(calendar_info) +local function linesubst(line, title, dates) local substs = { hdate = dates.hdate, week = dates.week, @@ -631,7 +630,7 @@ local function FindDailyNotes(opts) opts.close_after_yanking = opts.close_after_yanking or M.Cfg.close_after_yanking - local today = os.date("%Y-%m-%d") + local today = os.date(dateformats.date) local fname = M.Cfg.dailies .. "/" .. today .. M.Cfg.extension local fexists = file_exists(fname) if @@ -676,7 +675,7 @@ local function FindWeeklyNotes(opts) opts.close_after_yanking = opts.close_after_yanking or M.Cfg.close_after_yanking - local title = os.date("%Y-W%V") + local title = os.date(dateformats.isoweek) local fname = M.Cfg.weeklies .. "/" .. title .. M.Cfg.extension local fexists = file_exists(fname) if @@ -1142,19 +1141,19 @@ local function YankLink() end -- --- GotoToday: +-- GotoDate: -- ---------- -- --- find today's daily note and create it if necessary. +-- find note for date and create it if necessary. -- -local function GotoToday(opts) - opts = opts or calculate_dates() +local function GotoDate(opts) + opts.dates = calculate_dates(opts.date_table) opts.insert_after_inserting = opts.insert_after_inserting or M.Cfg.insert_after_inserting opts.close_after_yanking = opts.close_after_yanking or M.Cfg.close_after_yanking - local word = opts.date or os.date("%Y-%m-%d") + local word = opts.date or os.date(dateformats.date) local fname = M.Cfg.dailies .. "/" .. word .. M.Cfg.extension local fexists = file_exists(fname) @@ -1169,7 +1168,7 @@ local function GotoToday(opts) word, fname, M.note_type_templates.daily, - opts + opts.dates ) opts.erase = true opts.erase_file = fname @@ -1202,6 +1201,20 @@ local function GotoToday(opts) }) end +-- +-- GotoToday: +-- ---------- +-- +-- find today's daily note and create it if necessary. +-- +local function GotoToday(opts) + opts = opts or {} + local today = os.date(dateformats.date) + opts.date_table = os.date("*t") + opts.date = today + GotoDate(opts) +end + -- -- FindNotes: -- ---------- @@ -1472,7 +1485,7 @@ local function GotoThisWeek(opts) opts.close_after_yanking = opts.close_after_yanking or M.Cfg.close_after_yanking - local title = os.date("%Y-W%V") + local title = os.date(dateformats.isoweek) local fname = M.Cfg.weeklies .. "/" .. title .. M.Cfg.extension local fexists = file_exists(fname) if @@ -1524,23 +1537,11 @@ end -- action on enter on a specific day: -- preview in telescope, stay in calendar on cancel, open note in other window on accept local function CalendarAction(day, month, year, weekday, _) - local today = string.format("%04d-%02d-%02d", year, month, day) local opts = {} - opts.date = today - opts.hdate = daymap[weekday] - .. ", " - .. monthmap[tonumber(month)] - .. " " - .. day - .. daysuffix(day) - .. ", " - .. year - opts.week = "n/a" -- TODO: calculate the week somehow - opts.month = month - opts.year = year - opts.day = day + opts.date = string.format("%04d-%02d-%02d", year, month, day) + opts.date_table = {year=year, month=month, day=day} opts.calendar = true - GotoToday(opts) + GotoDate(opts) end local function ShowCalendar(opts) From 0749d4a9a681fc518a03a05edfe815353c219a2b Mon Sep 17 00:00:00 2001 From: Rene Schallner Date: Mon, 13 Dec 2021 17:19:19 +0100 Subject: [PATCH 11/11] fixed to stylua style --- lua/telekasten.lua | 78 +++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 0b19a00..4c7238e 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -200,7 +200,7 @@ local monthmap = { local dateformats = { date = "%Y-%m-%d", week = "%V", - isoweek = "%Y-W%V" + isoweek = "%Y-W%V", } local function calculate_dates(date) @@ -212,17 +212,17 @@ local function calculate_dates(date) local df = dateformats local dates = {} - - -- this is to compensate for the calendar showing M-Su, but os.date Su is + + -- this is to compensate for the calendar showing M-Su, but os.date Su is -- always wday = 1 local wday = dinfo.wday - 1 if wday == 0 then wday = 7 end - dates.year = dinfo.year + dates.year = dinfo.year dates.month = dinfo.month - dates.day = dinfo.day + dates.day = dinfo.day dates.hdate = daymap[wday] .. ", " .. monthmap[dinfo.month] @@ -232,24 +232,24 @@ local function calculate_dates(date) .. ", " .. dinfo.year - dates.date = os.date(df.date, time) - dates.prevday = os.date(df.date, time - oneday) - dates.nextday = os.date(df.date, time + oneday) - dates.week = os.date(df.week, time) - dates.prevweek = os.date(df.week, time - oneweek) - dates.nextweek = os.date(df.week, time + oneweek) - dates.isoweek = os.date(df.isoweek, time) + dates.date = os.date(df.date, time) + dates.prevday = os.date(df.date, time - oneday) + dates.nextday = os.date(df.date, time + oneday) + dates.week = os.date(df.week, time) + dates.prevweek = os.date(df.week, time - oneweek) + dates.nextweek = os.date(df.week, time + oneweek) + dates.isoweek = os.date(df.isoweek, time) dates.isoprevweek = os.date(df.isoweek, time - oneweek) dates.isonextweek = os.date(df.isoweek, time + oneweek) -- things get a bit hairy at the year rollover. W01 only starts the first week ofs - -- January if it has more than 3 days. Partial weeks with less than 4 days are - -- considered W52, but os.date still sets the year as the new year, so Jan 1 2022 - -- would appear as being in 2022-W52. That breaks linear linking respective + -- January if it has more than 3 days. Partial weeks with less than 4 days are + -- considered W52, but os.date still sets the year as the new year, so Jan 1 2022 + -- would appear as being in 2022-W52. That breaks linear linking respective -- of next/prev week, so we want to put the days of that partial week in -- January in 2021-W52. This tweak will only change the ISO formatted week string. if dates.week == 52 and dates.month == 1 then - dates.isoweek = os.date(df.isoweek, time - oneyear) + dates.isoweek = os.date(df.isoweek, time - oneyear) end -- Find the Sunday that started this week regardless of the calendar @@ -259,41 +259,41 @@ local function calculate_dates(date) local starting_sunday = time - (wday * oneday) local sunday_offset = 0 if M.Cfg.calendar_opts.calendar_monday == 1 then - sunday_offset = 7 + sunday_offset = 7 end - dates.monday = os.date(df.date, starting_sunday + (1 * oneday)) - dates.tuesday = os.date(df.date, starting_sunday + (2 * oneday)) - dates.wednesday = os.date(df.date, starting_sunday + (3 * oneday)) - dates.thursday = os.date(df.date, starting_sunday + (4 * oneday)) - dates.friday = os.date(df.date, starting_sunday + (5 * oneday)) - dates.saturday = os.date(df.date, starting_sunday + (6 * oneday)) - dates.sunday = os.date(df.date, starting_sunday + (sunday_offset * oneday)) + dates.monday = os.date(df.date, starting_sunday + (1 * oneday)) + dates.tuesday = os.date(df.date, starting_sunday + (2 * oneday)) + dates.wednesday = os.date(df.date, starting_sunday + (3 * oneday)) + dates.thursday = os.date(df.date, starting_sunday + (4 * oneday)) + dates.friday = os.date(df.date, starting_sunday + (5 * oneday)) + dates.saturday = os.date(df.date, starting_sunday + (6 * oneday)) + dates.sunday = os.date(df.date, starting_sunday + (sunday_offset * oneday)) return dates end local function linesubst(line, title, dates) local substs = { - hdate = dates.hdate, - week = dates.week, - date = dates.date, + hdate = dates.hdate, + week = dates.week, + date = dates.date, isoweek = dates.isoweek, - year = dates.year, + year = dates.year, - prevday = dates.prevday, - nextday = dates.nextday, - prevweek = dates.prevweek, - nextweek = dates.nextweek, + prevday = dates.prevday, + nextday = dates.nextday, + prevweek = dates.prevweek, + nextweek = dates.nextweek, isoprevweek = dates.isoprevweek, isonextweek = dates.isonextweek, - sunday = dates.sunday, - monday = dates.monday, - tuesday = dates.tuesday, + sunday = dates.sunday, + monday = dates.monday, + tuesday = dates.tuesday, wednesday = dates.wednesday, - thursday = dates.thursday, - friday = dates.friday, - saturday = dates.saturday, + thursday = dates.thursday, + friday = dates.friday, + saturday = dates.saturday, title = title, } @@ -1539,7 +1539,7 @@ end local function CalendarAction(day, month, year, weekday, _) local opts = {} opts.date = string.format("%04d-%02d-%02d", year, month, day) - opts.date_table = {year=year, month=month, day=day} + opts.date_table = { year = year, month = month, day = day } opts.calendar = true GotoDate(opts) end