Redefining [date] & [time] to match your time zone

Your server is not always in the same time zone as your web site. You can fix this.

numero = 147
interpreted = N
texte = Does your server time differ from your local time? For instance, your WebDNA host is in a different time zone from you, and you have no control over the server's time setting. Or you run your own server and can set it to whatever time you want, but you have client websites doing business in different time zones. [Date] is a very common and useful WebDNA tag, but if your "Today's Events" listing updates itself at 6 pm instead of midnight, it confuses people. Using a [function], we can redefine the [date] and [time] tags to overcome this problem, so when searching like this [search db=somedb.db&eqcaldatedatarq=[date]...] will work as expected for your local time. (A simple text variable won't work; you can't override a global tag with a text variable.) There are two ways to make the function kick in. The simplest is to put the function in the PreParse file in your sandbox or WebDNA Engine folder. This makes it effective server-wide (or Sandbox-wide) without you ever having to think about it again. But you might have different needs for different websites, or you might not want to get involved with more advanced Sandbox configuration. So the other way is to put the function in an [include] file and include it at the top of every page. (We will talk about the details of using PreParse in another section.) Here, we'll just focus on redefining the date. In these examples, we'll be changing from California time to New York time, which is a 3 hour difference. Basic function for changing the TIME: [function name=time][Math time]{[:global:Time]}+{03:00:00}[/Math][/function] NOTE: You MUST use [:global:time] and [:global:date] when defining functions with these same names. Otherwise WebDNA will get stuck in a hall of mirrors and lock up the server. Basic function for changing the DATE: But what happens at midnight, server time? Now that you've changed the time tag, you must also alter the date tag so you don't get thrown into yesterday tomorrow, when midnight rolls around. [function name=date][math date]{[:global:Date]}[showif [math]{[:global:Time]}+{03:00:00}[/math]<[math]{03:00:00}[/math]]+ {00/01/0000}[/showif][/math][/function] Here's the logic. At midnight NY time, it is still 9 pm in Los Angeles. For three hours, we need to advance the date by one. So we have to check to see if it is between midnight and 3 am in NY. (The [math] context converts HH:MM:SS to "number of seconds since midnight" so we can compare numbers to numbers.) [math]{[:global:Time]}+{03:00:00}[/math] --- This converts server time to NY time... ...and [math]{03:00:00}[/math] is simply 3 am. Our showif checks to see if NY time is less than 3 am. If so, we will advance the server date by 1 day. Daylight Savings Time NOTE: If you and the server both observe Daylight Savings Time, no problem; you can stop reading now! But if you live in an area where they don't do daylight savings, you'll have to add in another bit of [showif] code to see if it is daylight savings time in server time or not. If it is, then when it is 6 pm DST in LA, it is actually 5 pm in sun time, so you'd need to subtract an hour. For the California example, you check to see if the server is on Pacific Daylight Time like this: [showif [:global:date %Z]=PDT]-{01:00:00}[/showif] ...so the full code to subtract the hour looks like this: [function name=time][Math time]{[:global:Time]}+{03:00:00}[showif [:global:date %Z]=PDT]-{01:00:00}[/showif][/Math][/function] Add this adjustment into the date function comparison too! Okay, so what if YOU'RE the one with daylight savings, and the server is not? In that case, the server has no clue who, where, and what you are, so you'll have to be a little creative. A single showif based on the date is no good because there's no easy way to have WebDNA figure out when it's the second Sunday in March (for instance). Here's a way to achieve this, which you would update every five years or so. Check to see if today is greater than the day prior to the clock change day: [showif [math date]{[:global:date]}[/math]>03/07/2015] [text]dst=t[/text] [/showif] [showif [math date]{[:global:date]}[/math]>11/07/2015] [text]dst=f[/text] [/showif] [showif [math date]{[:global:date]}[/math]>03/06/2016] [text]dst=t[/text] [/showif] [showif [math date]{[:global:date]}[/math]>11/06/2016] [text]dst=f[/text] [/showif] ... etc. for 5 years or so Then use Showif to display either of two different functions (DST or not) based on the value of [dst] One last thing for the detail oriented. Okay, so then how about whether it is 2 am or whenever the clocks actually change? (It takes care of itself when the server uses Daylight Savings Time; this is only for this last example where the server is not on DST.) Well, since it happens in the wee hours of Sunday, and is different depending on country standards, you might say "who cares". But if it is important for you to to nail this down to the hour, then just throw on another showif above to test for the time of day. TIME ZONE INFO For a chart of where DST is used and how wacky it all is: DST chart. Time zones lists the time zones, and shows the several zones which shift by 30 minutes, and even 45 minutes. Crazy stuff! FORMATTING NOTE: The global tags [date] and [time] can be easily formatted using in-tag modifiers such as [date %b %d, %Y] to get the date in the form of Feb 11, 2009. However, our function-defined [date] tag will not recognize this, and will only display 02/11/2009. You will have to use the longer [format] method instead; i.e.: [format days_to_date %b %d, %Y][math]{[date]}[/math][/format]. FINAL NOTE: Using [date] in cookies ("expires" parameter) is a similar story, but cookie standards require the date and time to be in GMT, which does not use daylight savings time. For long term cookies, this really doesn't have much bearing, but when setting 30 minute, 1-hour, 3-hour, etc. cookies, it is critical to get the time right, especially in regard to daylight savings time. You will need to adjust the time to match GMT, not your own time, and if the server observes DST, you'll need to add that adjustment in as well. Full instructions in the COOKIE section Does your server time differ from your local time? For instance, your WebDNA host is in a different time zone from you, and you have no control over the server's time setting. Or you run your own server and can set it to whatever time you want, but you have client websites doing business in different time zones.

[date] is a very common and useful WebDNA tag, but if your "Today's Events" listing updates itself at 6 pm instead of midnight, it confuses people. Using a [function], we can redefine the [date] and [time] tags to overcome this problem, so when searching like this [search db=somedb.db&eqcaldatedatarq=[date]...] will work as expected for your local time. (A simple text variable won't work; you can't override a global tag with a text variable.)

There are two ways to make the function kick in. The simplest is to put the function in the PreParse file in your sandbox or WebDNA Engine folder. This makes it effective server-wide (or Sandbox-wide) without you ever having to think about it again. But you might have different needs for different websites, or you might not want to get involved with more advanced Sandbox configuration. So the other way is to put the function in an [include] file and include it at the top of every page. (We will talk about the details of using PreParse in another section.) Here, we'll just focus on redefining the date.

In these examples, we'll be changing from California time to New York time, which is a 3 hour difference.

Basic function for changing the TIME:
[function name=time][Math time]{[:global:Time]}+{03:00:00}[/Math][/function]

NOTE: You MUST use [:global:time] and [:global:date] when defining functions with these same names. Otherwise WebDNA will get stuck in a hall of mirrors and lock up the server.

Basic function for changing the DATE:
But what happens at midnight, server time? Now that you've changed the time tag, you must also alter the date tag so you don't get thrown into yesterday tomorrow, when midnight rolls around.

[function name=date][math date]{[:global:Date]}[showif [math]{[:global:Time]}+{03:00:00}[/math]<[math]{03:00:00}[/math]]+
{00/01/0000}[/showif][/math][/function]

Here's the logic. At midnight NY time, it is still 9 pm in Los Angeles. For three hours, we need to advance the date by one. So we have to check to see if it is between midnight and 3 am in NY. (The [math] context converts HH:MM:SS to "number of seconds since midnight" so we can compare numbers to numbers.)

[math]{[:global:Time]}+{03:00:00}[/math] --- This converts server time to NY time...

...and [math]{03:00:00}[/math] is simply 3 am. Our showif checks to see if NY time is less than 3 am. If so, we will advance the server date by 1 day.

Daylight Savings Time NOTE: If you and the server both observe Daylight Savings Time, no problem; you can stop reading now! But if you live in an area where they don't do daylight savings, you'll have to add in another bit of [showif] code to see if it is daylight savings time in server time or not. If it is, then when it is 6 pm DST in LA, it is actually 5 pm in sun time, so you'd need to subtract an hour. For the California example, you check to see if the server is on Pacific Daylight Time like this:

[showif [:global:date %Z]=PDT]-{01:00:00}[/showif]

...so the full code to subtract the hour looks like this:

[function name=time][Math time]{[:global:Time]}+{03:00:00}[showif
[:global:date %Z]=PDT]-{01:00:00}[/showif][/Math][/function]

Add this adjustment into the date function comparison too!

Okay, so what if YOU'RE the one with daylight savings, and the server is not? In that case, the server has no clue who, where, and what you are, so you'll have to be a little creative. A single showif based on the date is no good because there's no easy way to have WebDNA figure out when it's the second Sunday in March (for instance). Here's a way to achieve this, which you would update every five years or so. Check to see if today is greater than the day prior to the clock change day:

[showif [math date]{[:global:date]}[/math]>03/07/2015]
[text]dst=t[/text]
[/showif]
[showif [math date]{[:global:date]}[/math]>11/07/2015]
[text]dst=f[/text]
[/showif]
[showif [math date]{[:global:date]}[/math]>03/06/2016]
[text]dst=t[/text]
[/showif]
[showif [math date]{[:global:date]}[/math]>11/06/2016]
[text]dst=f[/text]
[/showif]
... etc. for 5 years or so

Then use Showif to display either of two different functions (DST or not) based on the value of [dst]

One last thing for the detail oriented. Okay, so then how about whether it is 2 am or whenever the clocks actually change? (It takes care of itself when the server uses Daylight Savings Time; this is only for this last example where the server is not on DST.) Well, since it happens in the wee hours of Sunday, and is different depending on country standards, you might say "who cares". But if it is important for you to to nail this down to the hour, then just throw on another showif above to test for the time of day.

TIME ZONE INFO For a chart of where DST is used and how wacky it all is: DST chart. Time zones lists the time zones, and shows the several zones which shift by 30 minutes, and even 45 minutes. Crazy stuff!

FORMATTING NOTE: The global tags [date] and [time] can be easily formatted using in-tag modifiers such as [date %b %d, %Y] to get the date in the form of Feb 11, 2009. However, our function-defined [date] tag will not recognize this, and will only display 02/11/2009. You will have to use the longer [format] method instead; i.e.:

[format days_to_date %b %d, %Y][math]{[date]}[/math][/format].

FINAL NOTE: Using [date] in cookies ("expires" parameter) is a similar story, but cookie standards require the date and time to be in GMT, which does not use daylight savings time. For long term cookies, this really doesn't have much bearing, but when setting 30 minute, 1-hour, 3-hour, etc. cookies, it is critical to get the time right, especially in regard to daylight savings time. You will need to adjust the time to match GMT, not your own time, and if the server observes DST, you'll need to add that adjustment in as well. Full instructions in the COOKIE section Terry Wilson

DOWNLOAD WEBDNA NOW!

Top Articles:

WebDNA Libraries

A list of available libraries for WebDNA...

WebDNA reference

...

AWS Raw WebDNA LAMP-Plus WebServer

Amazon Web Services (AWS) README for Machine Image ID...

F.A.Q

A compilation of some user's questions...

Tips and Tricks

A list of user-submitted tips ...

Technical Change History

This Technical Change History provides a reverse chronological list of WebDNA changes...

Related Readings:

How to keep the text variable after a function has finished

By default...

Hideif on IP range

This will show or hide stuff according to the IP...

How many working days?

This small script finds the number of working days between two dates...

Setting a 30-minute Cookie

Configuring the expires time for a short-term cookie is tricky...

Cloning a Record

Often...

Spaghetti code

How to get rid of very long search strings...