Date table in Lua

In Lua there is a special “format” that you can pass to the function to get the table instead of string-formatted date: os.date("*t"), which is missing in Silverbullet

This returns a table like this one:

{year = 1998, month = 9, day = 16, yday = 259, wday = 4,
 hour = 23, min = 48, sec = 10, isdst = false}

I’m trying to recreate the Journal “Weekly Note” but it seems hard to work with dates without it. I guess I could try doing 'os.date(“%w”)`, parsing it to number and doing some math there, but it’s far from perfect.

Is there any other easy solution for this?

BTW: if someone is interested only in daily notes, it’s just as easy as the Quick Note:

command.define {
  name = "Journal: Open Daily Note",
  key = "Alt-d",
  run = function()
    local pageName = "Journal/Day/" .. os.date("%Y-%m-%d")
    editor.navigate(pageName)
  end
}

Ok, I did some shenanigans, and it worked, but I’d still appreciate, if the "*t" format was simply supported :wink:

EDIT: since we don’t have officially supported way of distributing libraries for v2 yet I just updated this with commands for openning previous/next note.

journal = {}

command.define {
  name = "Journal: Open Daily Note",
  key = "Alt-d",
  run = function()
    local pageName = "Journal/Day/" .. os.date("%Y-%m-%d")
    editor.navigate(pageName)
  end
}

command.define {
  name = "Journal: Previous Daily Note",
  key = "Ctrl-u",
  run = function()
    local pageName = journal.previousNote()
    editor.navigate(pageName)
  end
}

command.define {
  name = "Journal: Next Daily Note",
  key = "Ctrl-i",
  run = function()
    local pageName = journal.nextNote()
    editor.navigate(pageName)
  end
}


command.define {
  name = "Journal: Open Weekly Note",
  run = function()
    local pageName = "Journal/Week/" .. journal.getFirstDayOfWeek()
    editor.navigate(pageName)
  end
}

function journal.getFirstDayOfWeek()
  local currentTime = os.time()
  local secondsInDay = 24 * 60 * 60

  -- Find out how many seconds from *now* to subtract to the
  -- beginning of the week. I'm doing (dayOfWeek-1) because
  -- I prefer to have Monday first.
  local dayOfWeek = tonumber(os.date("%w", currentTime))
  local secondsToSubtract = (dayOfWeek-1) * secondsInDay
  
  local firstDayTime = currentTime - secondsToSubtract
  return os.date(date.date_format, firstDayTime)
end

function journal.previousNote(pageName)
  if not pageName then
    pageName = editor.getCurrentPage()
  end
  local year, month, day = string.match(pageName, "^Journal/Day/(%d+).(%d+).(%d+)$")
  local day_before = os.time { year = year, month=month, day=day } - (3600 * 24)
  local newPageName = "Journal/Day/" .. os.date(date.date_format, day_before)
  return newPageName
end

function journal.nextNote(pageName)
  if not pageName then
    pageName = editor.getCurrentPage()
  end
  local year, month, day = string.match(pageName, "^Journal/Day/(%d+).(%d+).(%d+)$")
  local day_after = os.time { year = year, month=month, day=day } + (3600 * 24)
  local newPageName = "Journal/Day/" .. os.date(date.date_format, day_after)
  return newPageName
end

Just for reference, I stumbled on the same problem and filed an issue for it:

Unfortunately there is no solution, yet :wink:

1 Like