Useful Commands/Date & Time: Difference between revisions
Jump to navigation
Jump to search
(Created page with "{| class="wikitable" ! 情境 || 指令 |- | 取得本地時間 || <source lang="bash">date</source> |- | 取得 UTC 時間 || <source lang="bash">date -u</source> |- | 取...") |
No edit summary |
||
| Line 2: | Line 2: | ||
! 情境 || 指令 | ! 情境 || 指令 | ||
|- | |- | ||
| 取得本地時間 || < | | 取得本地時間 || <syntaxhighlight lang="bash">date</syntaxhighlight> | ||
|- | |- | ||
| 取得 UTC 時間 || < | | 取得 UTC 時間 || <syntaxhighlight lang="bash">date -u</syntaxhighlight> | ||
|- | |- | ||
| 取得 Timestamp || < | | 取得 Timestamp || <syntaxhighlight lang="bash">date +'%s'</syntaxhighlight> | ||
|- | |- | ||
| 產生 ISO 8601 時間字串 || < | | 產生 ISO 8601 時間字串 || <syntaxhighlight lang="bash">date -u +'%FT%TZ'</syntaxhighlight> | ||
|- | |- | ||
| 顯示目前的時區 || < | | 顯示目前的時區 || <syntaxhighlight lang="bash">date +'%Z'</syntaxhighlight> | ||
|- | |- | ||
| 以日期命名備份檔 || < | | 以日期命名備份檔 || <syntaxhighlight lang="bash">BKFILE=`date +'wiki-%Y%m%d.sqlite.gz'`</syntaxhighlight> | ||
|- | |- | ||
| 取得上週的日期 || < | | 取得上週的日期 || <syntaxhighlight lang="bash">date -r $((`date +%s`-86400*7))</syntaxhighlight> | ||
|- | |- | ||
| crontab 裡面使用 date || < | | crontab 裡面使用 date || <syntaxhighlight lang="bash">date +"\%F \%T"</syntaxhighlight>(注意 % 字元之前需要使用 \ 字元 escape) | ||
|- | |- | ||
| 取得檔案的 mtime 並且輸出成日期格式 || < | | 取得檔案的 mtime 並且輸出成日期格式 || <syntaxhighlight lang="bash">stat -f '%Sm' -t '%Y%m%d' test.sql</syntaxhighlight> | ||
|- | |- | ||
| 取得檔案的 mtime 並且輸出成 ISO 8601 格式 || < | | 取得檔案的 mtime 並且輸出成 ISO 8601 格式 || <syntaxhighlight lang="bash">stat -f '%Sm' -t '%F %T' test.sql</syntaxhighlight> | ||
|- | |- | ||
| 取得檔案的 mtime 並且輸出 Timestamp || | | 取得檔案的 mtime 並且輸出 Timestamp || | ||
< | <syntaxhighlight lang="bash"> | ||
stat -f '%Dm' test.sql | stat -f '%Dm' test.sql | ||
stat -c '%Y' test.sql | stat -c '%Y' test.sql | ||
stat -c '%Y %n' *.gz | stat -c '%Y %n' *.gz | ||
</ | </syntaxhighlight> | ||
|- | |- | ||
| 與時間伺服器對時(台灣) || | | 與時間伺服器對時(台灣) || | ||
< | <syntaxhighlight lang="bash"> | ||
sudo ntpdate time.stdtime.gov.tw | sudo ntpdate time.stdtime.gov.tw | ||
sudo ntpdate clock.stdtime.gov.tw | sudo ntpdate clock.stdtime.gov.tw | ||
</ | </syntaxhighlight> | ||
|- | |- | ||
| 變更系統時區 || | | 變更系統時區 || | ||
< | <syntaxhighlight lang="bash"> | ||
sudo dpkg-reconfigure tzdata | sudo dpkg-reconfigure tzdata | ||
sudo service cron restart # 做完記得要重開 cron 服務,否則排程時間會亂掉 | sudo service cron restart # 做完記得要重開 cron 服務,否則排程時間會亂掉 | ||
</ | </syntaxhighlight> | ||
|} | |} | ||
Latest revision as of 20:20, 3 April 2025
| 情境 | 指令 |
|---|---|
| 取得本地時間 | date
|
| 取得 UTC 時間 | date -u
|
| 取得 Timestamp | date +'%s'
|
| 產生 ISO 8601 時間字串 | date -u +'%FT%TZ'
|
| 顯示目前的時區 | date +'%Z'
|
| 以日期命名備份檔 | BKFILE=`date +'wiki-%Y%m%d.sqlite.gz'`
|
| 取得上週的日期 | date -r $((`date +%s`-86400*7))
|
| crontab 裡面使用 date | date +"\%F \%T"
|
| 取得檔案的 mtime 並且輸出成日期格式 | stat -f '%Sm' -t '%Y%m%d' test.sql
|
| 取得檔案的 mtime 並且輸出成 ISO 8601 格式 | stat -f '%Sm' -t '%F %T' test.sql
|
| 取得檔案的 mtime 並且輸出 Timestamp |
stat -f '%Dm' test.sql
stat -c '%Y' test.sql
stat -c '%Y %n' *.gz
|
| 與時間伺服器對時(台灣) |
sudo ntpdate time.stdtime.gov.tw
sudo ntpdate clock.stdtime.gov.tw
|
| 變更系統時區 |
sudo dpkg-reconfigure tzdata
sudo service cron restart # 做完記得要重開 cron 服務,否則排程時間會亂掉
|