Development Diaries – Entry 1

A lot has changed in web development in the last twenty years. Prepackaged goodies like jQuery and Bootstrap speed development of the look and feel of your website. The HTML standard has evolved over the years, allowing webpages to serve vastly different kinds of content that years ago would have been impossible without plugins. And then there are content management systems, which do what they say on the tin.

Some things stay the same though, like the need to timestamp your stuff. Every Facebook post, every Tweet, every article on your favorite news site, they all have the date and time on them so you have an idea of how fresh the content is. And so did every one of my web pages back then. The implementation was certainly different – whereas now everything’s a record or a document in a database that has a timestamp associated with it, back then I was working with static HTML files with no content management. So if I wanted to say when a file had been updated, I had to do it manually. That’s assuming I even remembered to do it at all.

Rather than do it the analog way, I looked at alternatives. Javascript was the first attempt:

document.write(document.lastModified);

But this presented a problem – technically, writing the last modified date to the document is itself a modification of the document, so the result is always the date and time that the browser renders the page. Useful, but not informative.

Then I looked at server-side ideas, specifically CGI scripting. It isn’t terribly complex, and writing a program in C to power a website in 2017 would be a pretty hilarious thing to do:

void getFileCreationTime(char *path) {
    struct stat attr;
    stat(path, &attr);
    printf("Last modified time: %s", ctime(&attr.st_mtime));
}
            

But it’s overkill, and I also don’t know C well enough to go down that path without spending a bunch of time looking for help online.

So, I found another option that gives me the server-side capability without writing and maintaining code: server side includes!

<!--#echo var="LAST_MODIFIED" -->

It’s a single tag, it does exactly what I want it to… but with one hitch: the date is reported in a time zone local to the server, and I’d reeeeeeally rather it report the time in GMT instead. Just a preference of mine. SSI tags don’t give me that option out of the box, but I can go back to my first attempt: Javascript can read the output from the SSI tag, parse the date, and spit it back out the way I want it to!

function lastUpdate(dateTime)
    {
        var updated = new Date(dateTime);
        return "Last updated: " + updated.toUTCString();
    }

document.write(lastUpdate('<!--#echo var="LAST_MODIFIED" -->'));
            

The result is what I want, a timestamp that automatically updates itself when I make a change to an HTML file, and reports the time in the time zone I want it reported in. Not bad!

[Historical note 2021-03-31: Publish date approximated]

Leave a Reply