Difference between revisions of "HOWTO: Clock, Time, UTC, Dual boot with Windows"

From Sabayon Wiki
Jump to: navigation, search
(Added item (c) make CLOCK_SYSTOHC="yes" for /etc/conf.d/clock users, and item (b) make clock_systohc="YES" in case the as-installed file does not have that.)
 
(No difference)

Latest revision as of 00:56, 22 April 2011

Clock, Time, UTC, Dual boot with Windows

Introduction

Question.png

This article is intended for three different types of Linux user:

1) This article will be of interest to you if you only run Linux on your PC and are having trouble getting the clock on the Panel to display the correct time.
2) If you only run Linux on your PC then this article will also be of interest to you if you need to know the time in other timezones (perhaps you have friends or relatives in other timezones, or you have a laptop and take it with you to other timezones).
3) If you dual boot with Windows then this article is also applicable.


This article covers Gentoo/Sabayon Linux before and after migration to Baselayout-2 and OpenRC (as covered in the Gentoo Baselayout and OpenRC Migration Guide). Any differences to clock and timezone-related files before and after migration are indicated in the article. If you are using Sabayon Linux 3.5 or a later version then you are already using Baselayout-2 and OpenRC. If you are using Sabayon Linux 3.4 or an earlier version then, unless you have manually migrated, you are not using Baselayout-2 and OpenRC.


Terminology

First let me define a few terms I'm going to use in this article:

'Hardware clock' (also known as 'BIOS clock' or 'CMOS clock') is the battery-backed clock on your motherboard. 'System clock' is the software clock that only exists when Linux is running, and is the clock that is displayed on the Panel. 'Local Time' is the time in your timezone.


Configuration

Linux is flexible and, if you want, you can configure it to write the time of the system clock to the hardware clock when you shutdown Linux. You don't have to do this, but it's a good idea. Why? Because the hardware clock tends to drift over time, so it's good to adjust it now and again. If you either manually adjust your system clock to the correct time or use NTP (network time protocol) to do it automatically via the Internet then you will be ensuring that the hardware clock is kept at the correct time. The various Linux distributions provide different ways of configuring how the distribution behaves, but for Gentoo/Sabayon Linux there is an environment variable called CLOCK_SYSTOHC (clock_systohc if you are using Baselayout-2 and OpenRC) in a text file /etc/conf.d/clock (/etc/conf.d/hwclock if you are using Baselayout-2 and OpenRC) which is used for this purpose: making CLOCK_SYSTOHC="yes" (clock_systohc="YES" if you are using Baselayout-2 and OpenRC) will make Gentoo/SL write the time of the system clock to the hardware clock at shutdown. Conversely, making CLOCK_SYSTOHC="no" (clock_systohc="NO" if you are using Baselayout-2 and OpenRC) will mean that the time of the system clock is not written to the hardware clock at shutdown.


Now, the text file /etc/conf.d/clock (/etc/conf.d/hwclock if you are using Baselayout-2 and OpenRC) can also contain an environment variable called CLOCK (clock if you are using Baselayout-2 and OpenRC). Here's an excerpt from the file clock, followed by an excerpt from the file hwclock:


File: /etc/conf.d/clock
    # /etc/conf.d/clock

    # Set CLOCK to "UTC" if your system clock is set to UTC (also known as
    # Greenwich Mean Time).  If your clock is set to the local time, then
    # set CLOCK to "local".  Note that if you dual boot with Windows, then
    # you should set it to "local".

    CLOCK=""

    # Select the proper timezone.  For valid values, peek inside of the
    # /usr/share/zoneinfo/ directory.  For example, some common values are
    # "America/New_York" or "EST5EDT" or "Europe/Berlin".

    TIMEZONE=""


    # If you wish to pass any other arguments to hwclock during bootup,
    # you may do so here.

    CLOCK_OPTS=""

    # If you want to set the Hardware Clock to the current System Time
    # during shutdown, then say "yes" here.

    CLOCK_SYSTOHC=""

    # Newer FHS specs say this file should live in /var/lib rather than
    # /etc.  If you care about such things, feel free to change this value.
    # Note that a blank value means that you do not wish to even use the
    # adjtime facility.  This is the default behavior as adjtime can be
    # very fragile.  If the clock is updated without updating the adjtime
    # file (which is common when using services such as ntp), then the
    # clock can be screwed up when it gets updated at next boot.

    #CLOCK_ADJTIME="/var/lib/adjtime"
    #CLOCK_ADJTIME="/etc/adjtime"
    CLOCK_ADJTIME=""


    ### ALPHA SPECIFIC OPTIONS ###

    # If your alpha uses the SRM console, set this to "yes".
    SRM="no"

    # If your alpha uses the ARC console, set this to "yes".
    ARC="no"


File: /etc/conf.d/hwclock
    # Set CLOCK to "UTC" if your system clock is set to UTC (also known as
    # Greenwich Mean Time).  If your clock is set to the local time, then
    # set CLOCK to "local".  Note that if you dual boot with Windows, then
    # you should set it to "local".
    clock="local"

    # If you want to set the Hardware Clock to the current System Time
    # during shutdown, then say "YES" here.
    # You normally don't need to do this if you run a ntp daemon.
    clock_systohc="YES"

    # If you wish to pass any other arguments to hwclock during bootup,
    # you may do so here. Alpha users may wish to use --arc or --srm here.
    clock_args=""


In the case of Gentoo/Sabayon Linux, perform the following if you are not using Baselayout-2 and OpenRC:

1. Edit /etc/conf.d/clock and: a) make CLOCK="local"; b) make TIMEZONE="<your timezone>" (if you are in Spain, for example, you would enter TIMEZONE="Europe/Madrid"); c) make CLOCK_SYSTOHC="yes".

2. Set the timezone information as per Gentoo Handbook Chapter 7. If you are in Spain, for example, you would enter the following command as root user in a Terminal window:

 rm /etc/localtime && cp /usr/share/zoneinfo/Europe/Madrid /etc/localtime 


Or, if you are using Baselayout-2 and OpenRC, perform the following:

1. Edit /etc/conf.d/hwclock and a) make clock="local"; b) make clock_systohc="YES" if it is not already that.

2. Edit /etc/timezone and enter your timezone (if you are in Spain, for example, you would make the contents of the file /etc/timezone to be "Europe/Madrid" without the quotes).

3. Set the timezone information as per Gentoo Handbook Chapter 7. If you are in Spain, for example, you would enter the following command as root user in a Terminal window:

 rm /etc/localtime && cp /usr/share/zoneinfo/Europe/Madrid /etc/localtime 


The proper contents for the file /etc/timezone is the path relative to your timezone from /usr/share/zoneinfo. For example, for someone living on the east coast of the United States, the correct contents of /etc/timezone would be "America/New_York" (without the quotes).


Stop.png
Do not make /etc/localtime a symlink, even though some people make this recommendation for other Linux distributions. The "rm /etc/localtime &&" shown above is in case /etc/localtime is already a symlink to the file you are trying to copy.


Dual Boot with Windows

If you dual boot Linux and Windows, the chances are that you've already come across the problem of the on-screen clocks in Linux and Windows having a difference of one or more hours; this usually happens twice a year when the clocks change in countries that adopt Daylight Saving Time (but can also occur in between, depending on how the two operating systems have been configured). You can experience various effects depending on how your Linux and Windows have been configured, such as the on-screen clock moving forwards (or backwards) by 1 hour in Linux on the allotted day, then when you boot Windows the clock moves forwards (or backwards) by a further 1 hour.


The first thing you need to know is that Windows stores the Local Time in the hardware clock and that is what is displayed in the System Notification Area on the Taskbar, whereas Linux keeps the times in the hardware clock and system clock separate, and only reads the time from the hardware clock at startup. When you boot Linux it reads the time in the hardware clock and sets the system clock to that time and applies any timezone offset that you have defined in a configuration file. Thus the time in the hardware clock and system clock can be -- and usually is -- different.


As you can see in the configuration file in the directory /etc/conf.d/, the comment in the file recommends that you edit the file to make CLOCK/clock="local" if you dual boot with Windows. This appears to make sense, given that Windows' clock (the hardware clock) contains local time, so when you boot Linux it will copy the time in the hardware clock to the system clock, find that CLOCK/clock="local" and therefore not make any adjustment to the time in the system clock.


However, this can also cause problems. The first time you boot into Windows after Daylight Saving Time starts or finishes in your timezone, Windows will automatically detect this and, if you have 'Automatically perform Daylight Savings update' ticked, Windows will adjust the hardware clock because it assumes the hardware clock contains Local Time. Because Linux has no way of knowing this has happened, its system clock will be out by at least an hour. In addition, if you initially had the hardware clock set to UTC and are using CLOCK/clock="UTC" and have configured Linux to display the time in your timezone, the clock will be wrong under Windows too.


Question.png
Note 1: Notice that the time of the Windows clock will not change if you boot Windows first following the start/end of Daylight Saving Time. The Windows clock will only be correct again once you have booted and shutdown Linux.


Question.png
Note 2: If you have migrated to Baselayout-2 and OpenRC from a Baselayout-1 installation, you may find that you see the following error message if you boot up in Verbose Mode (press Alt-F1 during boot):
   ERROR: cannot start hwclock as fsck would not start

I happened to find the correct solution in Gentoo Bugzilla Report No. 224171, which is to delete the old script /etc/init.d/clock. The file /etc/init.d/clock is a script that is redundant after migrating to Baselayout-2 and OpenRC. Merging openrc should have removed it, but didn't.

Anyway, to summarise, here's what I did as root user in a Terminal window to fix the problem:

   # mv /etc/init.d/clock /etc/init.d/clock.bak
   # /lib/rc/bin/rc-depend -u
The "/lib/rc/bin/rc-depend -u" rebuilds your deptree. I'm not sure if I needed to do that, but it doesn't do any harm to rebuild it.


My Situation

I am based in the UK but I travel frequently with my laptop to different timezones. I also have friends, relatives and work colleagues in various timezones who I contact often, so I need to know what time it is there before telephoning. And, just to complicate matters further, I dual boot SL and Windows 7.


If I were to set the Windows clock to the timezone of the area where I live "(UTC) Dublin, Edinburgh, Lisbon, London", untick "Automatically adjust clock for Daylight Saving Time", and make the Linux variables clock="local", /etc/timezone "Europe/London" and clock_systohc="YES" then, providing I stayed in the UK, my hardware clock and my Linux system clock would be correct (with the caveat in Note 1 above). I have not ticked the 'Automatically adjust clock for Daylight Saving Time' box, in order to avoid Windows adding/subtracting an additional hour if I have used Linux before Windows after the beginning or end of Daylight Saving Time. Furthermore, in Linux, by hovering my mouse pointer over the clock on the Panel I can make the clock on the Panel display the times of all the timezones I ticked in the list Time Zones in Clock Settings.


If I were to travel to another timezone, I could change the time displayed by the clock on the Panel to the time of the new timezone I'm in by right-clicking on the clock on the Panel. This would not change the time in the system clock, which would remain as the time at the Local Timezone (UK time, as I have not changed the Local Time), just the time displayed on the clock on the Panel. If I boot into Windows it would still display the time in the UK.


If I were to change the Windows clock to a different timezone to reflect the new timezone I'm now visiting, the next time I boot into Linux it would make the Local Timezone time equal to the local time in the hardware clock and write the time in the system clock to the hardware clock on shutdown. Both the time in Local Timezone and the time in the hardware clock would then be correct. So, wherever I am, I can change the configuration of the timezone of the Windows clock to the timezone I happen to be in (but leave the Daylight Saving Time correction unticked). In Gentoo/Sabayon Linux I would also need to edit /etc/timezone to contain the new timezone, and set the timezone information as per Gentoo Handbook Chapter 7.


[This page created by Fitzcarraldo on 17.08.08]