Example

Last Modified Date/Time (Within a REBOL Script)

Author: Carl Read
Return to REBOL Cookbook

It's common for pages on the Internet to have a "last modified" date and if you're building CGI webpages using REBOL it's a simple matter to automate this feature. The key to this is REBOL's MODIFIED? function which can allow the script to examine its own file for when it was last altered.

(NOTE: The examples here can be used with standard REBOL scripts - it's not just for CGI.)

Now you could and perhaps should use the date in the REBOL script's header for this, but this requires you to always remember to update it with every edit of the script which is, ahem, an easy thing to forget. By examining the file itself we ensure the date of the last modification will always be shown.

To show MODIFIED? being used in this way we'll first use it in a REBOL/View script so it can easily be tested locally instead of on a website.

In this first example the script is assumed to be called modified-example.r. When it is run, a word MOD is given the modified date of the script which is then displayed in a View layout...


    REBOL []

    mod: modified? %modified-example.r

    view layout [
        h2 "Last Modified Example"
        across
        text join "Script last modified on: " mod
    ]

There's problems with this though. If you decide to change the file-name of the script you'll also need to change the name of the file within the script. Also, if you use CHANGE-DIR in your script, MODIFIED? could be looking in the wrong directory for the file unless you've given it its full path.

What would be better would be if we had a way of finding the script's file without needing to know its name or what directory it was in. Well REBOL allows us to do this. The SYSTEM/OPTIONS/SCRIPT object holds script's file-name and full path. So if we replace the file-name in our script with SYSTEM/OPTIONS/SCRIPT we have a method of accessing the modified date that can be used without change in any script. Doing this, we now have a script looking like this...


    REBOL []

    mod: modified? system/options/script

    view layout [
        h2 "Last Modified Example"
        across
        text join "Script last modified on: " mod
    ]

NOTE: If you're testing the above script at the REBOL Console use LAUNCH instead of DO to run it, otherwise SYSTEM/OPTIONS/SCRIPT will not contain the script's file-name.

And finally, here's MODIFIED? used in this way in an example REBOL CGI script...


    #!/usr/bin/rebol -cs

    REBOL []

    mod: modified? system/options/script

    print "Content-Type: text/html^/^/"

    print {
        <html>
            <head>
                <title>
                    Last Modified Example
                </title>
            </head>
            <body>
                <center>
                    <h2>REBOL CGI Last Modified Example</h2>
                </center>
                <center>
    }

    print join "Page last modified on: " mod

    print {
                </center>
            </body>
        </html>
    }

As you can see from that, the HTML parts of REBOL CGI scripts don't need to look much different from plain HTML documents. The CGI specific lines are these two...


    #!/usr/bin/rebol -cs

    print "Content-Type: text/html^/^/"

Following the second of those in the script is the printing of the HTML document proper, which is in three parts, the middle part adding the modified date to the document.

(If you're new to CGI but are interested in it, see the section devoted to it in the Network Protocols chapter of the REBOL/Core Guide.)

A final note about dates: As used in the scripts above, they'll be displayed something like this...


    10-Sep-2003/22:07:26+12:00

To just show the date you could use...


    join "Page last modified on: " mod/date

while the following can be used to show the date and time but without the zone...


    rejoin ["Page last modified on: " mod/date " " mod/time]

And speaking of zones, if you want to show GMT instead of local time, just subtract the zone from the time. ie...


    mod: modified? system/options/script
    mod: mod - mod/zone

2006 REBOL Technologies REBOL.com REBOL.net