Using Lua to make a custom date stamp

Post here any hints or tips you have for using Trunk

Using Lua to make a custom date stamp

Postby bsmith40 » Sat Jun 09, 2012 8:47 pm

One of my main uses for Trunk Notes is as a personal log. I use one note per month, keeping a {toc} at the top of the page and using each day's date as the header so it's easy to get to any particular day. I've been using a snippet to fill in starting information for each day including the date in the format yyyy-mm-dd, and I've been manually adding a 2-letter code for the day of the week (eg. 2012-06-09 Sa for today).

This has worked reasonably well, except for 2 minor problems. First, it's a small nuisance to have to manually enter the day of the week. Second, if I want to fill in some information for the next day (say, before I head to bed the night before), I have to manually correct the date.

I wrote the following Lua script to fix my minor problems; I'm sharing it here in case anyone else might find it useful.

Code: Select all
-- Intelligent date stamp

-- get date and time, set up custom 'days' array.
-- the extra "Su" is for instances where I'm incrementing the days of the week past Saturday
temp = os.date ("*t")
days = {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"}

-- If it's after 8pm, I want the date stamp to be for the next day,
-- therefore, increment the date and day
if temp.hour > 19 then
   temp.wday = temp.wday + 1
   temp.day = temp.day + 1
end

-- if today is the last day of the month, then we need to
-- increment the month as well.
if temp.month == 2 then  -- if it's February
   if (temp.year %4 == 0) and (temp.day > 29) then -- leap year check
      temp.month = 3
      temp.day = 1
   elseif temp.day > 28 then
      temp.month = 3
      temp.day = 1
   end
end
if temp.month == (4 or 6 or 9 or 11) then  -- for 30-day months
   if temp.day > 30 then
      temp.month = temp.month + 1
      temp.day = 1
   end
end
if temp.day > 31 then
   temp.month = temp.month + 1
   temp.day = 1
end
if temp.month > 12 then  -- if it was December 31st, increment year
   temp.year = temp.year + 1
   temp.month = 1
end
-- format the date stamp
if temp.month < 10 then
   mymonth = "0" .. tostring(temp.month)
else
   mymonth = tostring(temp.month)
end
if temp.day < 10 then
   myday = "0" .. tostring(temp.day)
else
   myday = tostring(temp.day)
end

mydate = tostring(temp.year) .. "-" .. mymonth .. "-" .. myday .. " " .. days[temp.wday]
return mydate


That script is saved as DateStamp.lua and called from the snippet I use to start a new day's log.

I haven't done any programming since high school - the last script I wrote was probably an autoexec.bat on a 286. As such, I'm sure this script could be written more elegantly. If anyone has suggestions for improvement, feel free to comment. :-)
bsmith40
 
Posts: 2
Joined: Sat Jun 09, 2012 6:29 pm

Re: Using Lua to make a custom date stamp

Postby bsmith40 » Thu Jun 14, 2012 4:37 am

Came across the string.format command. Turns out, I can replace everything between the '--format the date' comment and the 'return mydate' command with:
Code: Select all
mydate = string.format ("%d-%02d-%02d %s", temp.year, temp.month, temp.day, days[temp.wday])

I knew there must be a more elegant way to do it. :)
bsmith40
 
Posts: 2
Joined: Sat Jun 09, 2012 6:29 pm


Return to Hints and tips

Who is online

Users browsing this forum: No registered users and 1 guest

cron