Linux ntp、时区、日期、时间设置、转换

时间设置

  • date
  • date +%Y-%m-%d+%H:%M:%S:2018-08-04+12:20:23
  • date +%Y-%m-%d+%T:2018-08-04+12:18:59
  • date +%-m%d%H:80412
    By default, date pads numeric fields with zeroes.
  • date +%m%d%H:080412
  • date -Iseconds:2018-08-04T12:21:14+0800
  • date -Iseconds -r <filename>:查看指定文件的最后修改时间;

-r, --reference=FILE display the last modification time of FILE

  • uptime (开机时间)

13:48:07 up 1541 days, 10:47, 1 user, load average: 0.41, 0.26, 0.16

Uptime Command In Linux:
It is used to find out how long the system is active (running).
This command returns set of values that involve: the current time, and the amount of time system is in running state, number of users currently logged into, and the load time for the past 1, 5 and 15 minutes respectively.

  • Installation Date
    ls -ld /var/log/installer
    ls -ld /lost+found
  • date -s '2014-12-25 12:34:56' [设置系统时间];
    Linux: set date through command line
  • date -s "2014-12-25 $(date +%H:%M:%S)" 设置系统时间;

时区设置

  • /etc/localtime
    可以是链接到某个时区文件的一个 link 文件,也可以是一个拷贝。
rm /etc/localtime
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  • How can I examine the contents of /etc/localtime?: 如何查看 localtime 文件。了解 zdump 工具。
  • Linux 时区设置 @csdn
  • 系统运行时长
    uptime=$(awk '{print $1}' < /proc/uptime):系统已经运行多少秒;
    starttime=$(awk '{print $22}' < /proc/$pid/stat):进程的开始时间(返回自系统启动后的所经历的 jiffies 节拍数,通常100个节拍为1秒);
0--------------------->进程启动------------------------>now
               (进程历时jiffies节拍数)             uptime秒数
STARTTIME=$(awk '{print int($22 / 100)}' /proc/$PID/stat)
UPTIME=$(awk '{print int($1)}' /proc/uptime)
时区设置文件
  • /usr/share/zoneinfo 目录
    在该目录下,有 zone.tab 文件(文本文件)和所有的时区文件,比如 /usr/share/zoneinfo/Asia/Shanghai 时区(使用 strings 命令可查看其中的可打印字串)。查看 tz database:更多时区内容。
  • /etc/sysconfig/clock
ZONE=Asia/Shanghai
UTC=false
ARC=false

/etc/TZ

cat /etc/TZ
GMT-8
注意:一般使用 UTC(国际标准)代替 GMT(英国) 了。

通过 TZ 变量设置时区

unix time

Unix time is defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970.

  • time(NULL) 取得这个秒数;
  • localtime() 用来获取本地时间的;


    time(NULL)
./strace -f ./superd -i br0:eth2.2 -n DE30054666CD6D318 -o YHTX -p 47.15.98.115 &
内核和系统版本
cat /proc/version
cat /etc/centos-release
cat /etc/redhat-release
cat /etc/system-release
cat /etc/lsb-release
  • LSB(Linux Standard Base)
  • lsb_release -a
  • uname -a
Ubuntu
关于 CST

时区问题,最早接触 Unix 时就曾经碰到过,没想到今天又碰到。

  • date -R 命令可以查看当前时区。--rfc-2822, Example: Mon, 07 Aug 2006 12:34:56 -0600
  • date -u 命令可以显示 UTC。--utc。
  • date +%s 命令可以显示 Unix Timestamp

我本人一直习惯把 CST 作为美国中部时间看。实际上,CST 至少同时代表了下面4个时区:

  • CST Central Standard Time (USA) UT-6:00
  • CST Central Standard Time (Australia) UT+9:30
  • CST China Standard Time UT+8:00
  • CST Cuba Standard Time UT-4:00

由此,CST 同时表示这么多含义,本人觉得非常不合理。估计时间函数库都会存在 BUG。反而 UTC 看起来比较统一,不过也还是要有一个过程。

中国时区

了解一下 中国时区设置,以及 1949年前时区设置

Epoch 转换

Epoch & Unix Timestamp Conversion Tools. Convert epoch to human readable date and vice versa.


Epoch & Unix Timestamp Conversion
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容