Skip to main content

Setting an Expiration Date

Each cookie has an expiration date, which tells the browser how long to keep it.

We are going to use an expiration date of one week from today. To do so, we obtain the current date and time, from the Caché system variable $H, trim off the time portion with the leading plus operator, and add 7 days to it.

Now we need to put this date into the format that the browser expects, which is a day followed by a date followed by a time in the GMT timezone. For example, at this moment it is

Fri, 10 Aug 2001 22:22:00 GMT

Caché does not have a function that produces a date in this precise format, but we can assemble one using various options of the $ZD (Date) function.

—TicketConfirm.OnPreHTTP—
TicketConfirm.OnPreHTTP
ClassMethod OnPreHTTP() As %Boolean
{
    If $data(%session.Data("Order")) {
        Set ord = ##class(Cinema.TicketOrder).%OpenId(%session.Data("Order"))
        Set itm = ord.Items.GetAt(1)
        Set cat = itm.Show.Film.Category.%Id()

        // Put a cookie on the users computer
        Set Expires = +$H + 7
        Set Expires = $ZD(Expires,11) _ ", " _ $ZD(Expires,2) _ " 00:00:00 GMT"
        // ...
    }

    Quit 1
}

Reminder: “_” is the concatenation operator.

FeedbackOpens in a new tab