Skip to main content
Library homepage
 
Engineering LibreTexts

08-A.2: Localization - date/timdatectl Command

  • Page ID
    32994
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Types of System Clocks

    Modern operating systems distinguish between the following two types of clocks:

    • A real-time clock (RTC), commonly referred to as a hardware clock, (typically an integrated circuit on the system board) that is completely independent of the current state of the operating system and runs even when the computer is shut down.
    • A system clock, also known as a software clock, that is maintained by the kernel and whose initial value is based on the real-time clock. Once the system is booted and the system clock is initialized, the system clock is completely independent of the real-time clock.

    The system time is always kept in Coordinated Universal Time (UTC) and converted in applications to local time as needed. Local time is the actual time in your current time zone, taking into account daylight savings time (DST). The real-time clock can use either UTC or local time. UTC is recommended.

    Linux offers three command line tools that can be used to configure and display information about the system date and time: the date command; the timedatectl utility, which is part of systemd; and the hwclock utility for accessing the hardware clock.

    The video delves into localization configuration. It covers topics that an administrator need to know to manage localizations.

    The date Command

    The date command is used to display the system date and time. It is also used to set date and time of the system. By default the date command displays the date in the time zone for which the system is configured. You must be the super-user (root) to change the date and time.

    Syntax:

    date [ OPTION ] [+FORMAT]
    date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
    

    Command Options:

    Option Option Meaning
    -d, --date=,STRING/ display time described by STRING, not 'now'
    -s, --set=,STRING/ set time described by STRING
    -u, --utc, --universal print or set Coordinated Universal Time (UTC)

    Usually the date command is used to simply display the current date and time, as shown in the example. However, there are other options that can give other output as well.

    pbmac@pbmac-server $ date
    Tue Aug 11 17:29:40 PDT 2020
    pbmac@pbmac-server $ date -u
    Wed Aug 12 00:29:43 UTC 2020
    pbmac@pbmac-server $ date --date="1 month ago"
    Sat Jul 11 17:30:19 PDT 2020
    pbmac@pbmac-server $ date --date="last week"
    Tue Aug  4 17:30:43 PDT 2020
    pbmac@pbmac-server $ date --date="10 days ago"
    Sat Aug  1 17:31:02 PDT 2020
    

    In addition to the options outlined above, there is a syntax that can be used to display the date and or time in a user specified format.

    %D: Display date as mm/dd/yy.       
    %d: Display the day of the month (01 to 31).       
    %a: Displays the abbreviated name for weekdays (Sun to Sat).
    %A: Displays full weekdays (Sunday to Saturday).
    %h: Displays abbreviated month name (Jan to Dec).
    %b: Displays abbreviated month name (Jan to Dec).
    %B: Displays full month name(January to December).
    %m: Displays the month of year (01 to 12).
    %y: Displays last two digits of the year(00 to 99).
    %Y: Display four-digit year. 
    %T: Display the time in 24 hour format as HH:MM:SS.
    %H: Display the hour.
    %M: Display the minute.
    %S: Display the seconds.

    Using these syntactical components it is possible to display the date and time and even day of the week. Notice that if you string more than one you need to enclose them in quotation marks.

    pbmac@pbmac-server $ date +%D
    08/11/20
    pbmac@pbmac-server $ date "+%D %T"
    08/11/20 17:44:02
    pbmac@pbmac-server $ date "+%Y-%m-%d"
    2020-08-11
    pbmac@pbmac-server $ date "+%A %B %d %T %y"
    Tuesday August 11 17:44:24 20
    

    The timedatectl Command

    The timedatectl utility is distributed as part of the systemd system and service manager and allows you to review and change the configuration of the system clock. You can use this tool to change the current date and time, set the time zone, or enable automatic synchronization of the system clock with a remote server.

    Syntax:

    timedatectl [ OPTIONS ] {COMMAND}

    Command Options:

    Option Option Meaning
    --no-ask-password Do not query the user for authentication for privileged operations.
    --adjust-system-clock If set-local-rtc is invoked and this option is passed, the system clock is synchronized from the RTC again, taking the new setting into account. Otherwise, the RTC is synchronized from the system clock.
    -M, --machine= Execute operation on a local container. Specify a container name to connect to.
    -H, --host= Execute the operation remotely.

    Command Line Commands

    There is also a set of sub-commands that can be used with timedatectl to perform a variety of actions.

    Sub-command Explanation
    status Show current settings of the system clock and RTC, including whether network time synchronization is active. If no command is specified, this is the implied default.
    set-time [TIME] Set the system clock to the specified time. This will also update the RTC time accordingly. The time may be specified in the format "2012-10-30 18:17:16."
    set-timezone [TIMEZONE] Set the system time zone to the specified value. Available time zones can be listed with list-timezones. If the RTC is configured to be in the local time, this will also update the RTC time. This call will alter the /etc/localtime symlink. See localtime for more information.
    list-timezones List available time zones, one per line. Entries from the list can be set as the system time zone with set-timezone.

    So, to use the above mentioned commands, we simply combine them with the timedatectl command itself and if appropriate we can add the options as well.

    pbmac@pbmac-server $ timedatectl 
                          Local time: Wed 2020-08-12 11:49:59 PDT
                      Universal time: Wed 2020-08-12 18:49:59 UTC
                            RTC time: Wed 2020-08-12 18:49:59
                           Time zone: America/Los_Angeles (PDT, -0700)
           System clock synchronized: yes
    systemd-timesyncd.service active: yes
                     RTC in local TZ: no
    pbmac@pbmac-server $ timedatectl set-time 23:26:00
    pbmac@pbmac-server $ timedatectl set-time "2013-06-02 23:26:00"
    pbmac@pbmac-server $ timedatectl set-timezone America/Los_Angeles
    

    The hwclock Command

    hwclock is a utility for accessing the hardware clock, also referred to as the Real Time Clock (RTC). The hardware clock is independent of the operating system you use and works even when the machine is shut down. This utility is used for displaying the time from the hardware clock. hwclock also contains facilities for compensating for systematic drift in the hardware clock.

    The hardware clock stores the values of: year, month, day, hour, minute, and second. It is not able to store the time standard, local time or Coordinated Universal Time (UTC), nor set the Daylight Saving Time (DST).

    The hwclock utility saves its settings in the /etc/adjtime file, which is created with the first change.

    Syntax:

    hwclock [FUNCTION] [OPTION...]
    

    Command Options:

    Option Option Meaning
    a, --adjust Add or subtract time from the Hardware Clock to account for systematic drift since the last time the clock was set or adjusted.
    -s, --hctosys Set the System Clock from the Hardware Clock.
    -set Set the Hardware Clock to the time given by the --date option, and update the timestamps in /etc/adjtime.
    w, --systohc Set the Hardware Clock from the System Clock, and update the timestamps in /etc/adjtime.

    Adapted from:
    "date command in Linux with examples" by saloni1297, Geeks for Geeks is licensed under CC BY-SA 3.0
    "Chapter 3. Configuring the Date and Time" by Multiple Contributors, Fedora Docs is licensed under CC BY-SA 3.0
    "3.3. Using the hwclock Command" by Multiple Contributors, Fedora Docs is licensed under CC BY-SA 3.0


    08-A.2: Localization - date/timdatectl Command is shared under a CC BY-NC license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?