Linux Date Time and Localization

In Linux all metadata about file are stored in UTC (Coordinated Universal Time) time. Regardless of your location.
So when we run stat filename. The information that it show is stored in UTC. However when it show us the information then it show it in our local time.
For example:


bob@vmUbuntu:/etc$ stat at.deny 
  File: 'at.deny'
  Size: 144       	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 151430      Links: 1
Access: (0640/-rw-r-----)  Uid: (    0/    root)   Gid: (    1/  daemon)
Access: 2016-08-19 19:26:01.950906000 +0300
Modify: 2016-01-15 00:35:27.000000000 +0200
Change: 2016-08-19 19:26:01.950906000 +0300
 Birth: -

However the date stamp is stored always in UTC, when we see it, then it convert it to our local-time.
It order to do this conversion Linux need to know what it our local timezone. It do this by check the file /etc/localtime . This file is a symbol link to file inside /usr/share/zoneinfo that has the name of the city of our timezone.  zoneinfo directory contain directory for every continent in the world and inside it for all the city in that continent that represent timezone.
By changing the symbolic link /etc/localtime  and making it to point to anther file (that represent city timezone), we  de facto change our timezone.

A quick way to see our current timezone is to use the datecommand.
For example:


bob@vmUbuntu:/etc$ date 
Fri Aug 26 13:45:26 IDT 2016

So in this case we run date and it told us that our timezone is set to IDT, which is acronym for Israel Daylight Time.

If we want to set individual timezone, we can use the variable TZ, we point it the filename (with full path) of the timezone that we want.
So for example, if we want to change the timezone of a user to Oral timezone (we will set his TZ variable):

bob@vmUbuntu:/etc$ date 
Fri Aug 26 13:45:26 IDT 2016
bob@vmUbuntu:/etc$ export TZ=/usr/share/zoneinfo/Asia/Oral 
bob@vmUbuntu:/etc$ date
Fri Aug 26 16:18:15 +05 2016

As we can see that timezone was change from IDT to +05. if we want this change to be permanent, we can put it in the login script of this user.
So this allow us to set different timezone for different user.

If we want to see our local setting, we can run the command local:
for example:


bob@vmUbuntu:/etc$ locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

So we can see that in the this computer, the local is English united state and the character encoding is UTF-8.
The local define character encoding and how telephone number, paper size, time   etc are displayed.
To see all installed  locales  locale -a will show all installed locales in our computer.

We can syncronize our computer with the ntpdate command (you may need to install this).
It is better to give it cluster of server address. you can find list of ntp server cluster here: http://www.pool.ntp.org/en/
For example:


bob@vmUbuntu:~$ sudo ntpdate  il.pool.ntp.org
26 Aug 14:54:48 ntpdate[11142]: adjust time server 109.226.40.40 offset 0.010919 sec

We used ntp server in the Israeli cluster, to synchronize our computer clock.
We can use cron to run ntpdate and regularly synchronize our computer clock. A better option maybe to use ntp (time service) and it will syncronize the computer. This service is may need to be installed.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s