from ubuntu:bionic
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y tzdata
RUN unlink /etc/localtime
RUN ln -s /usr/share/zoneinfo/America/New_York /etc/localtime
date
epoch
references:
It is the number of seconds that have elapsed since the Unix epoch, minus leap seconds; the Unix epoch is 00:00:00 UTC on 1 January 1970
$ date -u -d '1970-01-01 00:00:00' '+Normal: %F %T %:z%nUnix: %s'
Normal: 1970-01-01 00:00:00 +00:00
Unix: 0
$ date -d '1970-01-01 00:00:00' '+Normal: %F %T %:z%nUnix: %s'
Normal: 1970-01-01 00:00:00 +08:00
Unix: -28800
$ date -u -d '1970-01-01 00:00:00 UTC +1 day' '+Normal: %F %T %:z%nUnix: %s'
Normal: 1970-01-02 00:00:00 +00:00
Unix: 86400
$ date -d '2023-01-01' +%s; date -d '2023-01-01 - 1 year' +%s
1672560000
1641024000
# using now
$ date -d'now' +%s; date -d 'now - 1 day' +%s
1706952065
1706865665
$ date '+%s%3N'
1602231334983
$ date '+%s'
1602231334
timestamps
format
[!TIP]
yyyy-MM-dd'T'HH:mm:ss.SSSZ
yyyy-MM-dd'T'HH:mm:ss
DATE FORMAT OPTION
MEANING
EXAMPLE OUTPUT
date +%c
locale’s date time
Sat May 9 11:49:47 2020
date +%x
locale’s date
05/09/20
date +%X
locale’s time
11:49:47
date +%A
locale’s full weekday name
Saturday
date +%B
locale’s full month name
May
date +%m-%d-%Y
MM-DD-YYYY date format
05-09-2020
date +%D
MM/DD/YY date format
05/09/20
date +%F
YYYY-MM-DD date format
2020-05-09
date +%T
HH:MM:SS time format
11:44:15
date +%u
Day of Week
6
date +%U
Week of Year with Sunday as first day of week
18
date +%V
ISO Week of Year with Monday as first day of week
19
date +%j
Day of Year
130
date +%Z
Timezone
PDT
date +%m
Month of year (MM)
05
date +%d
Day of Month (DD)
09
date +%Y
Year (YY)
2020
date +%H
Hour (HH)
11
date +%H
Hour (HH) in 24-hour clock format
11
date +%I
Hour in 12-hour clock format
11
date +%p
locale’s equivalent of AM or PM
AM
date +%P
same as %p but in lower case
am
classical date format
$ secs=259200
$ date -u -d @${secs} +"%F"
1970-01-04
$ date -u -d @${secs} +"%T"
00:00:00
$ date -u -d @${secs} +"%F %T"
1970-01-04 00:00:00
$ date -u -d @${secs} -Is
1970-01-04T00:00:00+00:00
date format with timezone
$ date -u +"%Y-%m-%dT%H:%M:%SZ"
2020-10-09T08:14:47Z
$ date +%FT%T.%3N%:z
2020-10-09T17:27:18.491+08:00
$ date -u +"%Y-%m-%dT%H:%M:%S.%3NZ"
2020-10-09T08:14:47.167Z
$ date +%Y-%m-%d-T%H:%M:%S.%3N%z
2020-10-09-T17:27:18.491+0800
2015-12-11T20:28:30.45+01:00 or 2015-12-11T19:28:30.45Z
where:
YYYY = four-digit year
MM = two-digit month (01=January, etc.)
DD = two-digit day of month (01 through 31)
hh = two digits of hour (00 through 23) (am/pm NOT allowed)
mm = two digits of minute (00 through 59)
ss = two digits of second (00 through 59)
s = one or more digits representing a decimal fraction of a second (i.e. milliseconds)
TZD = time zone designator (Z or +hh:mm or -hh:mm)
$ date -I
2020-10-09
$ date -Is && date -Isecond
2020-10-09T16:31:47+08:00
2020-10-09T16:31:47+08:00
$ date -Ih
2020-10-09T16+08:00
$ date -Im
2020-10-09T16:31+08:00
rfc-3339
$ date --rfc-3339=date
2020-10-09
$ date --rfc-3339=ns
2020-10-09 17:32:14.158684000+08:00
$ date --rfc-3339=seconds
2020-10-09 17:32:14+08:00
utc
$ date
Fri Oct 9 17:09:34 CST 2020
$ date -u
Fri Oct 9 09:09:34 UTC 2020
timezone
[!NOTE|label:references:]
list all timezone:
$ timedatectl list-timezones | more
# or
$ tree /usr/share/zoneinfo/
$ date +"%Y-%m-%dT%H:%M:%SZ"
2020-10-09T17:16:37Z
$ date -u +"%Y-%m-%dT%H:%M:%SZ"
2020-10-09T09:16:37Z
$ date -d $(date -u +"%Y-%m-%dT%H:%M:%SZ")
Fri Oct 9 17:16:37 CST 2020
HUMAN-READABLE TIME
SECONDS
1 hour
3600 seconds
1 day
86400 seconds
1 week
604800 seconds
1 month (30.44 days)
2629743 seconds
1 year (365.24 days)
31556926 seconds
timestamps to epoch
$ echo $EPOCHSECONDS
1602235097
$ date -d $(date -u +"%Y-%m-%dT%H:%M:%SZ") +%s
1602235097
$ date --date=$(date -u +"%Y-%m-%dT%H:%M:%S.%3NZ") +%s%3N
1602235097801
$ date -d '2023-01-01' +%s; date -d '2023-01-01 - 1 day' +%s
1672560000
1672473600
$ date -d '2023-01-01' +%s; date -d '2023-01-01 - 1 year' +%s
1672560000
1641024000
# using now
$ date -d'now' +%s; date -d 'now - 1 day' +%s
1706952065
1706865665
epoch to timestamps
$ date -u +"%Y-%m-%dT%H:%M:%S.%3NZ"
2020-10-09T09:18:17.795Z
$ date -d @1602235097 +%c
Fri Oct 9 17:18:17 2020
$ date -d @1602235097
Fri Oct 9 17:18:17 CST 2020
$ date -d @1602235097 -u
Fri Oct 9 09:18:17 UTC 2020
$ date -d @1602235097 -R
Fri, 09 Oct 2020 02:18:17 -0700
$ date -d @1602235097 +%c
Fri Oct 9 02:18:17 2020
$ echo 1602235097 | awk '{ print strftime("%c", $0); }'
Fri Oct 9 02:18:17 2020
epoch with 13 digits
# wrong
$ date -d @1718731558409 +%c
Thu 08 Jun 56434 01:53:29 AM PDT
# solution: epoch/1000
$ date -d @$((1718731558409/1000)) +%c
Tue 18 Jun 2024 10:25:58 AM PDT
$ sudo timedatectl set-time "2020-02-23 12:23:01" # <<========设置系统时间,因为开启了时间同步所以报错
Failed to set time: Automatic time synchronization is enabled
$ sudo systemctl stop chronyd
$ sudo timedatectl set-time "2020-02-23 12:23:01" # <<==========stop chronyd 后修改系统时间,报错依旧
Failed to set time: Automatic time synchronization is enabled
$ sudo systemctl status chronyd
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: inactive (dead) since 四 2021-04-15 15:45:37 CST; 21s ago
Docs: man:chronyd(8)
man:chrony.conf(5)
Process: 13722 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
Process: 13716 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 13720 (code=exited, status=0/SUCCESS)
...
$ date
2021年 04月 15日 星期四 15:46:13 CST
$ sudo timedatectl set-ntp false
$ sudo timedatectl set-time "2020-02-23 12:23:01"
$ sudo systemctl status chronyd
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:chronyd(8)
man:chrony.conf(5)
...
$ sudo timedatectl status # <<============ 显示当前系统和RTC设置
Local time: 日 2020-02-23 12:23:39 CST
Universal time: 日 2020-02-23 04:23:39 UTC
RTC time: 日 2020-02-23 04:23:39
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
$ sudo timedatectl set-ntp true
$ sudo timedatectl status
Local time: 日 2020-02-23 12:24:14 CST
Universal time: 日 2020-02-23 04:24:14 UTC
RTC time: 日 2020-02-23 04:24:14
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
$ sudo systemctl start chronyd
$ sudo timedatectl status
Local time: 四 2021-04-15 15:48:52 CST
Universal time: 四 2021-04-15 07:48:52 UTC
RTC time: 日 2020-02-23 04:24:44
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
$ sudo timedatectl set-time "2020-02-23 12:23:01"
Failed to set time: Automatic time synchronization is enabled
$ sudo timedatectl set-ntp false # <<============= 禁用基于NTP的网络时间同步
$ sudo systemctl status chronyd
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:chronyd(8)
man:chrony.conf(5)
...
$ sudo timedatectl set-time "2020-02-23 12:23:01" # <<=========== 再次设置时间成功
$ sudo timedatectl set-ntp true # <<============ 启用基于NTP的网络时间同步
$ sudo systemctl status chronyd
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since 日 2020-02-23 12:23:25 CST; 4s ago
Docs: man:chronyd(8)
man:chrony.conf(5)
Process: 16110 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
Process: 16103 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 16105 (chronyd)
Tasks: 1
CGroup: /system.slice/chronyd.service
└─16105 /usr/sbin/chronyd
...
$ date
2021年 04月 15日 星期四 15:52:14 CST
systemd-timesyncd
[!NOTE]
issue in chrony with timedatactl
$ timedatectl
Local time: Tue 2024-04-02 16:11:02 PDT
Universal time: Tue 2024-04-02 23:11:02 UTC
RTC time: Tue 2024-04-02 23:11:02
Time zone: America/Los_Angeles (PDT, -0700)
System clock synchronized: yes
NTP service: n/a
RTC in local TZ: no
$ sudo timedatectl set-ntp on
Failed to set ntp: NTP not supported