Introduction

  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 its 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 saving time (DST). The real-time clock can use either UTC or local time. UTC is recommended.

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

1. Using 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.

  For information on how to display the current date and time in a custom format, see also Section 2, “Using the date Command”.

1.1. Displaying the Current Date and Time

  To display the current date and time along with detailed information about the configuration of the system and hardware clock, run the timedatectl command with no additional command line options:

timedatectl

  This displays the local and universal time, the currently used time zone, the status of the Network Time Protocol (NTP) configuration, and additional information related to DST.

  Example 1. Displaying the Current Date and Time

  The following is an example output of the timedatectl command on a system that does not use NTP to synchronize the system clock with a remote server:

~]$ timedatectl
      Local time: Mon 2013-09-16 19:30:24 CEST
  Universal time: Mon 2013-09-16 17:30:24 UTC
        Timezone: Europe/Prague (CEST, +0200)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: yes
 Last DST change: DST began at
                  Sun 2013-03-31 01:59:59 CET
                  Sun 2013-03-31 03:00:00 CEST
 Next DST change: DST ends (the clock jumps one hour backwards) at
                  Sun 2013-10-27 02:59:59 CEST
                  Sun 2013-10-27 02:00:00 CET

1.2. Changing the Current Time

  To change the current time, type the following at a shell prompt as root:

timedatectl set-time HH:MM:SS

  Replace HH with an hour, MM with a minute, and SS with a second, all typed in two-digit form.

  This command updates both the system time and the hardware clock. The result it is similar to using both the date --set and hwclock --systohc commands.

  Example 2. Changing the Current Time

  To change the current time to 11:26 p.m., run the following command as root:

~]# timedatectl set-time 23:26:00

  By default, the system is configured to use UTC. To configure your system to maintain the clock in the local time, run the timedatectl command with the set-local-rtc option as root:

timedatectl set-local-rtc boolean

  To configure your system to maintain the clock in the local time, replace boolean with yes (or, alternatively, y, true, t, or 1). To configure the system to use UTC, replace boolean with no (or, alternatively, n, false, f, or 0). The default option is no.

1.3. Changing the Current Date

  To change the current date, type the following at a shell prompt as root:

timedatectl set-time YYYY-MM-DD

  Replace YYYY with a four-digit year, MM with a two-digit month, and DD with a two-digit day of the month.

  Note that changing the date without specifying the current time results in setting the time to 00:00:00.

  Example 3. Changing the Current Date

  To change the current date to 2 June 2013 and keep the current time (11:26 p.m.), run the following command as root:

~]# timedatectl set-time "2013-06-02 23:26:00"

1.4. Changing the Time Zone

  To list all available time zones, type the following at a shell prompt:

timedatectl list-timezones

  To change the currently used time zone, type as root:

timedatectl set-timezone time_zone

  Replace time_zone with any of the values listed by the timedatectl list-timezones command.

  Example 4. Changing the Time Zone

  To identify which time zone is closest to your present location, use the timedatectl command with the list-timezones command line option. For example, to list all available time zones in Europe, type:

~]# timedatectl list-timezones | grep Europe
Europe/Amsterdam
Europe/Andorra
Europe/Athens
Europe/Belgrade
Europe/Berlin
Europe/Bratislava
…

  To change the time zone to Europe/Prague, type as root:

~]# timedatectl set-timezone Europe/Prague

1.5. Synchronizing the System Clock with a Remote Server

  As opposed to the manual setup described in the previous sections, the timedatectl command also allows you to enable automatic synchronization of your system clock with a group of remote servers using NTP. Enabling NTP enables the chronyd or ntpd service, depending on which of them is installed.

  To enable or disable NTP, type the following at a shell prompt as root:

timedatectl set-ntp boolean

  To configure your system to synchronize the system clock with a remote NTP server, replace boolean with yes (the default option). To disable this feature, replace boolean with no.

  Example 5. Synchronizing the System Clock with a Remote Server

  To enable automatic synchronization of the system clock with a remote server, type:

~]# timedatectl set-ntp yes

2. Using the date Command

  The date utility is available on all Linux systems and allows you to display and configure the current date and time. It is frequently used in scripts to display detailed information about the system clock in a custom format.

  For information on how to change the time zone or enable automatic synchronization of the system clock with a remote server, see Section 1, “Using the timedatectl Command”.

2.1. Displaying the Current Date and Time

  To display the current date and time, run the date command with no additional command line options:

date

  This displays the day of the week followed by the current date, local time, abbreviated time zone, and year.

  By default, the date command displays the local time. To display the time in UTC, run the command with the --utc or -u command line option:

date --utc

  You can also customize the format of the displayed information by providing the +"format" option on the command line:

date +"format"

  Replace format with one or more supported control sequences as illustrated in Example 6, “Displaying the Current Date and Time”. See Table 1, “Commonly Used Control Sequences” for a list of the most frequently used formatting options, or the date(1) manual page for a complete list of these options.

  Table 1. Commonly Used Control Sequences

Control Sequence Description
%H The hour in the HH format (for example, 17).
%M The minute in the MM format (for example, 30).
%S The second in the SS format (for example, 24).
%d The day of the month in the DD format (for example, 16).
%m The month in the MM format (for example, 09).
%Y The year in the YYYY format (for example, 2013).
%Z The time zone abbreviation (for example, CEST).
%F The full date in the YYYY-MM-DD format (for example, 2013-09-16). This option is equal to %Y-%m-%d.
%T The full time in the HH:MM:SS format (for example, 17:30:24). This option is equal to %H:%M:%S

  Example 6. Displaying the Current Date and Time

  To display the current date and local time, type the following at a shell prompt:

~]$ date
Mon Sep 16 17:30:24 CEST 2013

  To display the current date and time in UTC, type the following at a shell prompt:

~]$ date --utc
Mon Sep 16 15:30:34 UTC 2013

  To customize the output of the date command, type:

~]$ date +"%Y-%m-%d %H:%M"
2013-09-16 17:30

2.2. Changing the Current Time

  To change the current time, run the date command with the --set or -s option as root:

date --set HH:MM:SS

  Replace HH with an hour, MM with a minute, and SS with a second, all typed in two-digit form.

  By default, the date command sets the system clock to the local time. To set the system clock in UTC, run the command with the --utc or -u command line option:

date --set HH:MM:SS --utc

  Example 7. Changing the Current Time

  To change the current time to 11:26 p.m., run the following command as root:

~]# date --set 23:26:00

2.3. Changing the Current Date

  To change the current date, run the date command with the --set or -s option as root:

date --set YYYY-MM-DD

  Replace YYYY with a four-digit year, MM with a two-digit month, and DD with a two-digit day of the month.

  Note that changing the date without specifying the current time results in setting the time to 00:00:00.

  Example 8. Changing the Current Date

  To change the current date to 2 June 2013 and keep the current time (11:26 p.m.), run the following command as root:

~]# date --set 2013-06-02 23:26:00

3. Using 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 you make, for example, when you set the time manually or synchronize the hardware clock with the system time.

  In Fedora 6, the hwclock command was run automatically on every system shutdown or reboot, but it is not in Fedora 22. When the system clock is synchronized by the Network Time Protocol (NTP) or Precision Time Protocol (PTP), the kernel automatically synchronizes the hardware clock to the system clock every 11 minutes.

  For details about NTP, see Chapter 13, Configuring NTP Using the chrony Suite and Chapter 14, Configuring NTP Using ntpd. For information about PTP, see Chapter 15, Configuring PTP Using ptp4l. For information about setting the hardware clock after executing ntpdate, see Section 14.18, “Configuring the Hardware Clock Update”.

3.1. Displaying the Current Date and Time

  Running hwclock with no command line options as the root user returns the date and time in local time to standard output.

hwclock

  Note that using the --utc or --localtime options with the hwclock command does not mean you are displaying the hardware clock time in UTC or local time. These options are used for setting the hardware clock to keep time in either of them. The time is always displayed in local time. Additionally, using the hwclock --utc or hwclock --local commands does not change the record in the /etc/adjtime file. This command can be useful when you know that the setting saved in /etc/adjtime is incorrect but you do not want to change the setting. On the other hand, you may receive misleading information if you use the command an incorrect way. See the hwclock(8) manual page for more details.

  Example 9. Displaying the Current Date and Time

  To display the current date and the current local time from the hardware clock, run as root:

~]# hwclock
Tue 15 Apr 2014 04:23:46 PM CEST     -0.329272 seconds

  CEST is a time zone abbreviation and stands for Central European Summer Time.

  For information on how to change the time zone, see Section 1.4, “Changing the Time Zone”.

3.2. Setting the Date and Time

  Besides displaying the date and time, you can manually set the hardware clock to a specific time.

  When you need to change the hardware clock date and time, you can do so by appending the --set and --date options along with your specification:

hwclock --set --date "dd mmm yyyy HH:MM"

  Replace dd with a day (a two-digit number), mmm with a month (a three-letter abbreviation), yyyy with a year (a four-digit number), HH with an hour (a two-digit number), MM with a minute (a two-digit number).

  At the same time, you can also set the hardware clock to keep the time in either UTC or local time by adding the --utc or --localtime options, respectively. In this case, UTC or LOCAL is recorded in the /etc/adjtime file.

  Example 10. Setting the Hardware Clock to a Specific Date and Time

  If you want to set the date and time to a specific value, for example, to "21:17, October 21, 2014", and keep the hardware clock in UTC, run the command as root in the following format:

~]# hwclock --set --date "21 Oct 2014 21:17" --utc

3.3. Synchronizing the Date and Time

  You can synchronize the hardware clock and the current system time in both directions.

  • Either you can set the hardware clock to the current system time by using this command:
    hwclock --systohc
    Note that if you use NTP, the hardware clock is automatically synchronized to the system clock every 11 minutes, and this command is useful only at boot time to get a reasonable initial system time.
  • Or, you can set the system time from the hardware clock by using the following command:
    hwclock --hctosys

  When you synchronize the hardware clock and the system time, you can also specify whether you want to keep the hardware clock in local time or UTC by adding the --utc or --localtime option. Similarly to using --set, UTC or LOCAL is recorded in the /etc/adjtime file.

  The hwclock --systohc --utc command is functionally similar to timedatectl set-local-rtc false and the hwclock --systohc --local command is an alternative to timedatectl set-local-rtc true.

  Example 11. Synchronizing the Hardware Clock with System Time

  To set the hardware clock to the current system time and keep the hardware clock in local time, run the following command as root:

~]# hwclock --systohc --localtime

  To avoid problems with time zone and DST switching, it is recommended to keep the hardware clock in UTC. The shown Example 11, “Synchronizing the Hardware Clock with System Time” is useful, for example, in case of a multi boot with a Windows system, which assumes the hardware clock runs in local time by default, and all other systems need to accommodate to it by using local time as well. It may also be needed with a virtual machine; if the virtual hardware clock provided by the host is running in local time, the guest system needs to be configured to use local time, too.

4. Additional Resources

  For more information on how to configure the date and time in Fedora 22, see the resources listed below.

  Installed Documentation

  • timedatectl(1) — The manual page for the timedatectl command line utility documents how to use this tool to query and change the system clock and its settings.
  • date(1) — The manual page for the date command provides a complete list of supported command line options.
  • hwclock(8) — The manual page for the hwclock command provides a complete list of supported command line options.

  See Also

Fedora 22中的日期和时间配置的更多相关文章

  1. Java中的日期和时间

    Java中的日期和时间 Java在java.util包中提供了Date类,这个类封装了当前的日期和时间. Date类支持两种构造函数.第一个构造函数初始化对象的当前日期和时间. Date() 下面的构 ...

  2. Fedora 22中的RPM软件包管理工具

    Introduction The RPM Package Manager (RPM) is an open packaging system that runs on Fedora as well a ...

  3. Fedora 22中的用户和用户组管理

    The control of users and groups is a core element of Fedora system administration. This chapter expl ...

  4. PHP中的日期和时间

    日期和时间    1.UNIX时间戳        以32位整数表示的格林威治标准时间        这个UNIX时间戳整数是从1970年1月1日0时0分0秒(计算机元年)到现在的秒数         ...

  5. 【转】C/C++中的日期和时间 TIME_T与STRUCT TM转换——2013-08-25 16

    http://www.cnblogs.com/Wiseman/archive/2005/10/24/260576.html 摘要: 本文从介绍基础概念入手,探讨了在C/C++中对日期和时间操作所用到的 ...

  6. MySQL中的日期和时间:使用和说明,以及常用函数

    1.首先需要注意: 1.1 MySQL中把日期和时间是分开的. 以字符串2007-12-31 00:59:59为例: 日期部分:2007-12-31.这部分也可以叫做一个日期表达式. 时间部分:00: ...

  7. Day029 JDK8中新日期和时间API (二)

    # JDK8中新日期和时间API (二) Instant介绍 Instant:时间线上的一个瞬时点. 这可能被用来记录应用程序中的事件时间 戳. 在处理时间和日期的时候,我们通常会想到年,月,日,时, ...

  8. Day029 JDK8中新日期和时间API (四)

    JDK8中新日期和时间API 其他的一些API ZoneId:该类中包含了所有的时区信息,一个时区的ID,如 Europe/Paris ZonedDateTime:一个在ISO-8601日历系统时区的 ...

  9. JavaScript 中的日期和时间

    前言 本篇的介绍涵盖以下部分: 1. 时间标准指的是什么?UCT和GMT 的概念.关联和区别? 2. 时间表示标准有哪些? 3. JS 中时间的处理 日期时间标准 日期的标准就不多说了 -- 公元纪年 ...

随机推荐

  1. 12306官方火车票Api接口

    2017,现在已进入春运期间,真的是一票难求,深有体会.各种购票抢票软件应运而生,也有购买加速包提高抢票几率,可以理解为变相的黄牛.对于技术人员,虽然写一个抢票软件还是比较难的,但是还是简单看看123 ...

  2. 【原】nodejs全局安装和本地安装的区别

    来微信支付有2年多了,从2年前的互联网模式转变为O2O模式,主要的场景是跟线下的商户去打交道,不像以往的互联网模式,有产品经理提需求,我们帮忙去解决问题. 转型后是这样的,团队成员更多需要去寻找业务的 ...

  3. dotNET跨平台相关文档整理

    一直在从事C#开发的相关技术工作,从C# 1.0一路用到现在的C# 6.0, 通常情况下被局限于Windows平台,Mono项目把我们C#程序带到了Windows之外的平台,在工作之余花了很多时间在M ...

  4. 运用php做投票题,例题

    要求大概是这样的,有一个题目,题目下面是复选框,要求点完复选框提交后会变成进度条,各选项的进度条百分比,和投票数量 首先还是要在数据库建两张表,如下: 要完成这个题目,需要建两个页面 <!DOC ...

  5. .NET Core采用的全新配置系统[10]: 配置的同步机制是如何实现的?

    配置的同步涉及到两个方面:第一,对原始的配置文件实施监控并在其发生变化之后从新加载配置:第二,配置重新加载之后及时通知应用程序进而使后者能够使用最新的配置.要了解配置同步机制的实现原理,先得从认识一个 ...

  6. 谈谈一些有趣的CSS题目(六)-- 全兼容的多列均匀布局问题

    开本系列,谈谈一些有趣的 CSS 题目,题目类型天马行空,想到什么说什么,不仅为了拓宽一下解决问题的思路,更涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题 ...

  7. Postman - 功能强大的 API 接口请求调试和管理工具

    Postman 是一款功能强大的的 Chrome 应用,可以便捷的调试接口.前端开发人员在开发或者调试 Web 程序的时候是需要一些方法来跟踪网页请求的,用户可以使用一些网络的监视工具比如著名的 Fi ...

  8. HTML5笔记2——HTML5音/视频标签详解

    音视频的发展史 早期:<embed>+<object>+文件 问题:不是所有浏览器都支持,而且embed不是标准. 现状:Realplay.window media.Quick ...

  9. 易用BPM时代,企业如何轻松驾驭H3?

    众所周知,BPM作为企业发展的推动力,能敏捷高效的融合业务流程和信息资源.通过综合考虑流程的成本.效率.质量等方面因素,用IT系统将调整后的流程固化下来,从而降低企业管理成本,提高内部运营效率,提升企 ...

  10. H3 BPM引擎API接口

    引擎API接口通过 Engine 对象进行访问,这个是唯一入口. 示例1:获取组织机构对象 this.Engine.Organization.GetUnit("组织ID"); 示例 ...