OpenWrt Forum Archive

Topic: Setting the correct time on the router

The content of this topic has been archived on 29 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

I'm embarrassed to say it, but I just finished updating to RC5, and I forgot how to set it up to use the correct time. I have a custom script in /etc/init.d named S90mystartup. In it, I have a section that goes like this:

# set date & time
export TZ="CST6CDT"
/usr/sbin/rdate -s 192.43.244.18

I am in the Central time zone and I am using a NTP server from time.nist.gov. However, when I enter date from the command line, I get the date and time in UTC instead of CST. If I enter each line, from my script, separately, I end up with CST time. Thus, there must be something wrong with using the export TZ="CST6CDT" command within this start up script. I had this working right in RC2, 3, and 4, so I know it works, but , for the life of me, I can not remember how I did it. I know this is a really trivial issue, but it is driving me nuts. Someone please save my sanity and tell me what I am doing wrong ;-)

TIA,
Jon

Sorry, got setting time confused with displaying timezones.

(Last edited by Bill_MI on 1 Apr 2006, 13:39)

Point 1 to note: A Unix system always records the time in UTC. The 'TZ' environment variable (or /etc/localtime) just affects how it is formatted for display.

Point 2 to note: When you run a script, it gets its own local copy of the environment. Any modifications it makes are private. When the script terminates, you are left back with your original environment.

So you could for example make /etc/init.d/S90mystartup say

# set date & time
/usr/sbin/rdate -s 192.43.244.18

and then type:

# /etc/init.d/S90mystartup
# date      # shows the time in UTC
# export TZ="CST6CDT"
# date      # shows the time in your local zone

to get the right answer.

Of course, it's inconvenient to have to set the TZ environment variable before every command that makes use of the time. On a standard Unix system,  you'd do

ln -s /usr/share/zoneinfo/CST6CDT /etc/localtime

but I don't know if there's an OpenWRT package that has the timezone files.

Alternatively, you can set the TZ environment variable within your shell's startup script. This is usually a real pain on a multiuser Unix system as there are so many different shells which need to be configured, but in the case of OpenWRT where you only ever login as root, try sticking it into /etc/profile.

However, non-interactive scripts generally don't read /etc/profile; so when your machine starts up, and runs scripts to start daemons, they may still not have the TZ setting at the time of invocation, and therefore any date printing they do will still be in UTC. You may therefore need to modify those scripts.

HTH,

Brian.

(Last edited by candlerb on 1 Apr 2006, 08:54)

echo "CST6CDT" > /etc/TZ ?

- from the wiki Howto

To setup the correct time to:

echo "CST6CDT" > /etc/TZ
/usr/sbin/rdate -s 192.43.244.18

or use ntpclient.

Thanks flyashi and olli... that did the trick. I think my biggest mistake was doing my research with an old version of the wiki documentation (oops). The document I was going by was called Configuration and under section 4.1.4 TimeZone and NTP it had the example of using the export TZ="CET-1CETDST". As we have seen, this does work correctly for the current user, after boot-up, but not during bootup. Anyway, next time, I'll make sure that I have a current version of the documentation to research from ;-)

Thanks Everyone,
Jon

The discussion might have continued from here.