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.
