4

I often have to display the date in ConTeXt, but not for today, as displayed with \date, but for the next day. How to print tomorrow's date instead?

E.g. August 7, 2019

  • 2
    \ctxlua{os.sleep(24*60*60)}\currentdate – Henri Menke Aug 7 at 5:42
8

Do it in Lua. The function os.time() gives you the current time as a POSIX timestamp, i.e. the number of seconds since 1/1/1970. Add the number of seconds in a day to that to get the same time but tomorrow. Feed that result into os.date. Normally os.date formats a date to a string, but if the format argument is "*t", it returns a table with the time stamp separated into sensible units. From that you can extract the day, month, and year and plug it into ConTeXt's date function.

Strictly speaking this only works on a POSIX system, but I don't know any system that does not use POSIX timestamps. This approach should also work for at month and year boundaries and even with leap years. The only problem are leap seconds, because if you execute this exactly at a leap second, you might end up at the same day instead of tomorrow, but these only occur every few years.

\starttext

\currentdate

\ctxlua{
    local t = os.date("*t", os.time() + 24*60*60)
    context.date{d = t.day, m = t.month, y = t.year}
}

\stoptext

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.