UTC和RTC
理解时间:硬件时间、系统时间(UTC时间)、本地时间、时区与夏令时
处理和管理时间是计算机科学的重要方面,但也是最复杂和容易混淆的方面之一。本文将详细介绍硬件时间、系统时间(UTC时间)、本地时间、时区和夏令时,希望能帮助读者更好地理解这些概念。
1. 硬件时间(RTC time)
1.1 硬件时间简介
硬件时间,也被称为实时时钟(RTC),是指计算机主板上的一个独立于操作系统的设备,它在电源关闭甚至断电情况下也能保持运行。其功能是记录当前的日期和时间。
1.2 如何使用硬件时间
大部分操作系统在启动时会从RTC读取时间,然后设置系统时间。同样,操作系统也可以将系统时间写回到RTC中。
在Unix和类Unix系统中,hwclock命令可以用来查询和设置RTC时间。例如:
# 查询硬件时间
1 | hwclock --show |
# 将系统时间设置为硬件时间
1 | hwclock --systohc |
# 将硬件时间设置为系统时间
1 | hwclock --hctosys |
在Windows系统中,可以通过日期和时间设置对话框或time命令查询和设置RTC时间。
2. 系统时间(UTC时间)(Universal time)
2.1 系统时间简介
系统时间是计算机内部使用的时间,它通常在启动时从RTC设置,然后由系统时钟进行跟踪。系统时钟是操作系统内核的一部分,可以以毫秒或纳秒级别提供精确时间。
2.2 UTC时间
系统时间通常使用协调世界时(UTC)表示。UTC是一种基于原子时钟的时间标准,全球各地的科学家通过精密测量来维护它。它与格林尼治标准时间(GMT)非常接近,但不受地球自转速度变化的影响。
在Unix和类Unix系统中,date命令可以用来查询和设置系统时间:
# 查询系统时间(UTC)
1 | date -u |
# 查询本地时间(由系统时间UTC和时区共同决定)
1 | date |
# 设置本地时间(后面那串是本地时间)(设置本地时间会同样设置系统时间,只不过会自动帮你做时区转换)
1 | date -s "2022-01-01 00:00:00" |
# 直接设置系统时间(不推荐,一般通过设置本地时间来设置系统时间,因为这样设置你还得把后面那串时间计算一下时区)
1 | date -u -s "2022-01-01 00:00:00" |
在Windows系统中,可以通过日期和时间设置对话框或time命令查询和设置系统时间。
2、hwclock源码
//LA.QSSI.12.0.r1/external/toybox/toys/other/hwclock.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | /* hwclock.c - get and set the hwclock * * Copyright 2014 Bilal Qureshi <bilal.jmi@gmail.com> * * No standard, but see Documentation/rtc.txt in the linux kernel source.. * USE_HWCLOCK(NEWTOY(hwclock, ">0(fast)f(rtc):u(utc)l(localtime)t(systz)s(hctosys)r(show)w(systohc)[-ul][!rtsw]", TOYFLAG_SBIN)) config HWCLOCK bool "hwclock" default y help usage: hwclock [-rswtluf] Get/set the hardware clock. -f FILE Use specified device file instead of /dev/rtc0 (--rtc) -l Hardware clock uses localtime (--localtime) -r Show hardware clock time (--show) -s Set system time from hardware clock (--hctosys) -t Set the system time based on the current timezone (--systz) -u Hardware clock uses UTC (--utc) -w Set hardware clock from system time (--systohc) */ #define FOR_hwclock #include "toys.h" #include <linux/rtc.h> GLOBALS( char *f; ) void hwclock_main() { struct timezone tzone; struct timeval timeval; struct tm tm ; int fd = -1, utc; if (FLAG(u)) utc = 1; else if (FLAG(l)) utc = 0; else utc = !readfile( "/etc/adjtime" , toybuf, sizeof (toybuf)) || !! strstr (toybuf, "UTC" ); if (!FLAG(t)) { if (!TT.f) TT.f = "/dev/rtc0" ; fd = xopen(TT.f, O_WRONLY*FLAG(w)); // Get current time in seconds from rtc device. todo: get subsecond time if (!FLAG(w)) { xioctl(fd, RTC_RD_TIME, & tm ); timeval.tv_sec = xmktime(& tm , utc); timeval.tv_usec = 0; // todo: fixit } } if (FLAG(w) || FLAG(t)) { if (gettimeofday(&timeval, 0)) perror_exit( "gettimeofday failed" ); if (!(utc ? gmtime_r : localtime_r)(&timeval.tv_sec, & tm )) error_exit(utc ? "gmtime_r failed" : "localtime_r failed" ); } if (FLAG(w)) { /* The value of tm_isdst is positive if daylight saving time is in effect, * zero if it is not and negative if the information is not available. * todo: so why isn't this negative...? */ tm .tm_isdst = 0; xioctl(fd, RTC_SET_TIME, & tm ); } else if (FLAG(s)) { tzone.tz_minuteswest = timezone / 60 - 60 * daylight; } else if (FLAG(t)) { // Adjust seconds for timezone and daylight saving time // extern long timezone is defined in header sys/time.h tzone.tz_minuteswest = timezone / 60; if ( tm .tm_isdst) tzone.tz_minuteswest -= 60; if (!utc) timeval.tv_sec += tzone.tz_minuteswest * 60; } else { strftime (toybuf, sizeof (toybuf), "%F %T%z" , & tm ); xputs(toybuf); } if (FLAG(t) || FLAG(s)) { tzone.tz_dsttime = 0; if (settimeofday(&timeval, &tzone)) perror_exit( "settimeofday failed" ); } xclose(fd); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2023-01-10 展锐ADC
2018-01-10 makefile实例