linux小窍门─设置时钟和时间

2024-04-28 02:32

本文主要是介绍linux小窍门─设置时钟和时间,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文档解释了如何从linux下设置计算机的时钟,如何设置您的时区和其它与linux如何保存时间相关的材料。
DL@bitsCN_com网管软件下载
您的计算机有两个时钟,一个是始终运行的、由电池供电的( 硬件的、BIOS或CMOS )时钟,另一个是由运行在您的计算机上的操作系统维护的( 系统 )时钟。硬件时钟通常只在操作系统启动时用来设置系统时钟,然后直到重启或关闭系统,由系统时钟来记录时间。在Linux 系统中,您可以选择用UTC/GMT 时间或本地时间来记录硬件时钟。推荐的选项是用UTC 记录,因为夏令时可以自动记录。使用UTC 记录硬件时钟的唯一不足是,如果您使用双系统,其它操作系统,如DOS 要求硬件时钟用本地时间设置,那么在那个操作系统里时间将是错误的。 www@bitscn@com
设置时区:
bbs.bitsCN.com

    Linux 下的时区是通过建立从/etc/localtime[1] 到/usr/share/zoneinfo [2] 目录下与您所在时区相符的文件的符号链结实现的。例如,由于我在南澳大利亚,/etc/localtime就是到/usr/share/zoneinfo/Australia/South的符号链结。要建立这个链结,运行:
www@bitscn@com

    ln -sf ../usr/share/zoneinfo/your/zone /etc/localtime bitsCN#com中国网管联盟
替换your/zone 为形如Australia/NSW或Australia/Perth 的文件。看看/usr/share/zoneinfo目录都有什么时区。
www@bitscn@com
[1] 这里假设/usr/share/zoneinfo 是到/etc/localtime 的链结的前提是Redhat Linux
[bitsCN_com]
[2] 在旧版本的系统里,您会发现使用/usr/lib/zoneinfo而不是/usr/share/zoneinfo。参考后面“一些应用程序中时间错误”。
bitsCN#com中国网管联盟

    设置UTC 或本地时间: DL@bitsCN_com网管软件下载
当Linux 启动时,一个启动脚本运行/sbin/hwclock 程序复制当前硬件时钟时间到系统时钟。hwclock 假定硬件时钟设置为本地时间,除非它使用了--utc 参数。在RedHat Linux下您不是编辑启动脚本,而是编辑/etc/sysconfig/clock 文件,相应的改变UTC 一行为UTC=true或UTC=false。
bbs.bitsCN.com中国网管论坛
设置系统时钟: DL@bitsCN_com网管软件下载
在Linux 下设置系统时钟使用date命令。例如,设置当前时间和日期为July 31,11:16pm,运行date 07312316 ( 注意这里的时间是24小时制) ;如果您想设置年份为1998,
bitsCN#com中国网管联盟

    应该运行date 073123161998 ;要是也想设置秒,运行date 07312316.30或date 073123161998.30。要查看Linux 当前本地时间,使用date,不带参数。 DL@bitsCN_com网管软件下载
设置硬件时钟:
[bitsCN_com]
要设置硬件时钟,我喜欢的方式是首先设置系统时钟,然后设置硬件时钟为当前系统时钟时间,使用命令/sbin/hwclock --systohc (或 /sbin/hwclock --systohc --utc ) ,如果您使用UTC 保存硬件时钟) 。要查看当前硬件时钟的设置,不带参数运行hwclock 。如果硬件时钟是UTC 保存,而您想看相应的本地时间,运行/sbin/hwclock --utc 。 bitsCN#com中国网管联盟
一些应用程序中时间错误:
www@bitscn@com

    如果一些应用程序,如date显示了正确的时间,而另一些则错误,而您运行着RedHat Linux 5.0/5.1,您很可能遇到了一个由于将时区信息从/usr/lib/zoneinfo 移动到/usr/share/zoneinfo 引起的bug 。修复的方法是建立一个从/usr/lib/zoneinfo 到/usr/share/zoneinfo 的符号链结:
www_bitscn_com中国.网管联盟
ln -s ../share/zoneinfo /usr/lib/zoneinfo 。 中国_网管联盟bitsCN.com
小结: 中国_网管联盟bitsCN.com
*/etc/sysconfig/clock 设置硬件时钟,无论是用UTC 保存还是用本地时间保存
    *建立/etc/localtime到/usr/share/zoneinfo/...的符号链结来设置时区
    *运行date MMDDhhmm 来设置当前系统日期/ 时间
    *运行/sbin/hwclock --systohc [--utc]来设置硬件时钟
BBS.bitsCN.com网管论坛
其它有趣的注解: [bitsCN_com]
Linux kernel总是按照从UTC 时间1970年1 月1 日午夜开始的秒数来储存和计算时间,无论您的硬件时钟是否用UTC 保存。转换到本地时间的工作是运行时完成的。这样做的一个妙处是,如果某人从不同的时区使用您的计算机,他可以设置时区环境变量,所有的日期和时间可以按他的时区正确显示。
bitsCN.nET*中国网管博客

    如果自UTC 1972年1 月1 日开始的秒数用保存为带正负号32位整数,如同在您的Linux/Intel 系统上一样,您的时钟将在2038年停止工作。Linux 没有Y2K 问题,但是确实存在2038年的问题。令人期望的是,那时我们都会使用64位系统来运行Linux 了。64位整数将使我们的时钟一直运行到大约2922.71亿年。 www_bitscn_com中国.网管联盟
其它值得一看的程序: [bitsCN_com]
* rdate ──从远程机器获得当前时间;可以用来设置系统时间
    * xntpd ──类似rdata ,但是它是相当精确的,并且您需要有永久的网络连结xntpd 持续地运行,记录网络延时、时钟漂移等事件但是也有一个程序( ntpdate ) 包括在内,像rdate 一样设置当前时间。
bitsCN#com中国网管联盟

    相关信息:
DL@bitsCN_com网管软件下载

    * date(1)
    * hwclock(8)
    * /usr/doc/HOWTO/mini/Clock
[bitsCN_com]

    英文原文:
[bitsCN.Com]
http://www.linuxsa.org.au/tips/time.html
bitsCN#com中国网管联盟

    引用:
    Linux Tips
    Linux, Clocks, and Time
[bitsCN_com]

    Introduction 中国_网管联盟bitsCN.com
This document explains how to set your computer's clock from Linux, how to set your timezone, and other stuff related to Linux and how it does its time-keeping.
中国_网管联盟bitsCN.com
Your computer has two timepieces; a battery-backed one that is always running (the ``hardware'', ``BIOS'', or ``CMOS'' clock), and another that is maintained by the operating system currently running on your computer (the ``system'' clock). The hardware clock is generally only used to set the system clock when your operating system boots, and then from that point until you reboot or turn off your system, the system clock is the one used to keep track of time. www_bitscn_com中国.网管联盟
On Linux systems, you have a choice of keeping the hardware clock in UTC/GMT time or local time. The preferred option is to keep it in UTC because then daylight savings can be automatically accounted for. The only disadvantage with keeping the hardware clock in UTC is that if you dual boot with an operating system (such as DOS) that expects the hardware clock to be set to local time, the time will always be wrong in that operating system.
    Setting your timezone
bbs.bitsCN.com中国网管论坛

    The timezone under Linux is set by a symbolic link from /etc/localtime[1] to a file in the /usr/share/zoneinfo[2] directory that corresponds with what timezone you are in. For example, since I'm in South Australia, /etc/localtime is a symlink to /usr/share/zoneinfo/Australia/South. To set this link, type:
DL@bitsCN_com网管软件下载

    ln -sf ../usr/share/zoneinfo/your/zone /etc/localtime
bitsCN.nET*中国网管博客
Replace your/zone with something like Australia/NSW or Australia/Perth. Have a look in the directories under /usr/share/zoneinfo to see what timezones are available.
bbs.bitsCN.com
[1] This assumes that /usr/share/zoneinfo is linked to /etc/localtime as it is under Red Hat Linux.
bbs.bitsCN.com中国网管论坛

    [2] On older systems, you'll find that /usr/lib/zoneinfo is used instead of /usr/share/zoneinfo. See also the later section ``The time in some applications is wrong''.
    Setting UTC or local time
BBS.bitsCN.com网管论坛

    When Linux boots, one of the initialisation scripts will run the /sbin/hwclock program to copy the current hardware clock time to the system clock. hwclock will assume the hardware clock is set to local time unless it is run with the --utc switch. Rather than editing the startup script, under Red Hat Linux you should edit the /etc/sysconfig/clock file and change the ``UTC'' line to either ``UTC=true'' or ``UTC=false'' as appropriate.
    Setting the system clock
bbs.bitsCN.com中国网管论坛
To set the system clock under Linux, use the date command. As an example, to set the current time and date to July 31, 11:16pm, type ``date 07312316'' (note that the time is given in 24 hour notation). If you wanted to change the year as well, you could type ``date 073123161998''. To set the seconds as well, type ``date 07312316.30'' or ``date 073123161998.30''. To see what Linux thinks the current local time is, run date with no arguments.
    Setting the hardware clock
BBS.bitsCN.com网管论坛
To set the hardware clock, my favourite way is to set the system clock first, and then set the hardware clock to the current system clock by typing ``/sbin/hwclock --systohc'' (or ``/sbin/hwclock --systohc --utc'' if you are keeping the hardware clock in UTC). To see what the hardware clock is currently set to, run hwclock with no arguments. If the hardware clock is in UTC and you want to see the local equivalent, type ``/sbin/hwclock --utc''
    The time in some applications is wrong DL@bitsCN_com网管软件下载
If some applications (such as date) display the correct time, but others don't, and you are running Red Hat Linux 5.0 or 5.1, you most likely have run into a bug caused by a move of the timezone information from /usr/lib/zoneinfo to /usr/share/zoneinfo. The fix is to create a symbolic link from /usr/lib/zoneinfo to /usr/share/zoneinfo: ``ln -s ../share/zoneinfo /usr/lib/zoneinfo''.
    Summary DL@bitsCN_com网管软件下载
* /etc/sysconfig/clock sets whether the hardware clock is stored as UTC or local time. [bitsCN_com]
* Symlink /etc/localtime to /usr/share/zoneinfo/... to set your timezone. bitsCN~com
* Run ``date MMDDhhmm'' to set the current system date/time.
DL@bitsCN_com网管软件下载
* Type ``/sbin/hwclock --systohc [--utc]'' to set the hardware clock.
[bitsCN.Com]
Other interesting notes 中国_网管联盟bitsCN.com
The Linux kernel always stores and calculates time as the number of seconds since midnight of the 1st of January 1970 UTC regardless of whether your hardware clock is stored as UTC or not. Conversions to your local time are done at run-time. One neat thing about this is that if someone is using your computer from a different timezone, they can set the TZ environment variable and all dates and times will appear correct for their timezone. DL@bitsCN_com网管软件下载
If the number of seconds since the 1st of January 1970 UTC is stored as an signed 32-bit integer (as it is on your Linux/Intel system), your clock will stop working sometime on the year 2038. Linux has no inherent Y2K problem, but it does have a year 2038 problem. Hopefully we'll all be running Linux on 64-bit systems by then. 64-bit integers will keep our clocks running quite well until aproximately the year 292271-million.
    Other programs worth looking at
www@bitscn@com

    * rdate - get the current time from a remote machine; can be used to set the system time.
[bitsCN.Com]

    * xntpd - like rdate, but it's extremely accurate and you need a permanent 'net connection. xntpd runs continuously and accounts for things like network delay and clock drift, but there's also a program (ntpdate) included that just sets the current time like rdate does. bitsCN~com
Further information www@bitscn@com
* date(1)
[bitsCN.Com]

    * hwclock(8) bitsCN~com
* /usr/doc/HOWTO/mini/Clock bbs.bitsCN.com

本文来自: 中国网管联盟(bitsCN.com) 详细出处参考: http://www.bitscn.com/linux/system_manage/200604/6898.html

这篇关于linux小窍门─设置时钟和时间的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/942120

相关文章

Python的Darts库实现时间序列预测

《Python的Darts库实现时间序列预测》Darts一个集统计、机器学习与深度学习模型于一体的Python时间序列预测库,本文主要介绍了Python的Darts库实现时间序列预测,感兴趣的可以了解... 目录目录一、什么是 Darts?二、安装与基本配置安装 Darts导入基础模块三、时间序列数据结构与

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详

防止Linux rm命令误操作的多场景防护方案与实践

《防止Linuxrm命令误操作的多场景防护方案与实践》在Linux系统中,rm命令是删除文件和目录的高效工具,但一旦误操作,如执行rm-rf/或rm-rf/*,极易导致系统数据灾难,本文针对不同场景... 目录引言理解 rm 命令及误操作风险rm 命令基础常见误操作案例防护方案使用 rm编程 别名及安全删除

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

C# LiteDB处理时间序列数据的高性能解决方案

《C#LiteDB处理时间序列数据的高性能解决方案》LiteDB作为.NET生态下的轻量级嵌入式NoSQL数据库,一直是时间序列处理的优选方案,本文将为大家大家简单介绍一下LiteDB处理时间序列数... 目录为什么选择LiteDB处理时间序列数据第一章:LiteDB时间序列数据模型设计1.1 核心设计原则

linux系统上安装JDK8全过程

《linux系统上安装JDK8全过程》文章介绍安装JDK的必要性及Linux下JDK8的安装步骤,包括卸载旧版本、下载解压、配置环境变量等,强调开发需JDK,运行可选JRE,现JDK已集成JRE... 目录为什么要安装jdk?1.查看linux系统是否有自带的jdk:2.下载jdk压缩包2.解压3.配置环境

Linux搭建ftp服务器的步骤

《Linux搭建ftp服务器的步骤》本文给大家分享Linux搭建ftp服务器的步骤,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录ftp搭建1:下载vsftpd工具2:下载客户端工具3:进入配置文件目录vsftpd.conf配置文件4:

Linux实现查看某一端口是否开放

《Linux实现查看某一端口是否开放》文章介绍了三种检查端口6379是否开放的方法:通过lsof查看进程占用,用netstat区分TCP/UDP监听状态,以及用telnet测试远程连接可达性... 目录1、使用lsof 命令来查看端口是否开放2、使用netstat 命令来查看端口是否开放3、使用telnet