当前位置:网站首页>Chapter 11 Working with Dates and Times
Chapter 11 Working with Dates and Times
2022-08-01 22:59:00 【梦想家DBA】
1.1 Formatting Dates for Display
Use the date command with a strftime format specification.
# Setting environment variables can be helpful in scripts:
$ STRICT_ISO_8601='%Y-%m-%dT%H:%M:%S%z' # http://greenwichmeantime.com/info/iso.htm
$ ISO_8601='%Y-%m-%d %H:%M:%S %Z' # Almost ISO-8601, but more human-readable
$ ISO_8601_1='%Y-%m-%d %T %Z' # %T is the same as %H:%M:%S
$ DATEFILE='%Y%m%d%H%M%S' # Suitable for use in a file name
[[email protected] Day0801]$ STRICT_ISO_8601='%Y-%m-%dT%H:%M:%S%z'
[[email protected] Day0801]$ ISO_8601='%Y-%m-%d %H:%M:%S %Z'
[[email protected] Day0801]$ ISO_8601_01='%Y-%m-%d %T %Z'
[[email protected] Day0801]$ DATEFILE='%Y%m%d%H%M%S'
[[email protected] Day0801]$ date "+$IS0_8601"
2022-08-01 07:50:35 CST
[[email protected] Day0801]$ gawk "BEGIN {print strftime(\"$ISO_8601\")}"
2022-08-01 07:50:49 CST
[[email protected] Day0801]$ # Same as previous $ISO_8601
[[email protected] Day0801]$ date '+%Y-%m-%d %H:%M:%S %Z'
2022-08-01 07:52:08 CST
[[email protected] Day0801]$ date "+Program starting at: $ISO_8601"
Program starting at: 2022-08-01 07:52:54 CST
[[email protected] Day0801]$ printf "%b" "Program starting at: $(date '+$ISO_8601')\n"
Program starting at: $ISO_8601
[[email protected] Day0801]$ echo "I can rename a file like this: mv file.log file_$(date +$DATEFILE).log"
I can rename a file like this: mv file.log file_20220801075507.log
[[email protected] Day0801]$
1.2 Supplying a Default Date
Using the GNU date command,assign the most likely date to a variable, then allow the user to change it.
[[email protected] Day0801]$ cat default_date.sh
#!/usr/bin/env bash
# cookbook filename: default_date.sh
#Use Noon time to prevent a script running around midnight and a clock a few seconds off from causing off by one day errors.
START_DATE=$(date -d 'last week Monday 12:00:00' '+%Y-%m-%d')
while [ 1 ]; do
printf "%b" "The starting date is $START_DATE,is that correct? (Y/new date) "
read answer
# Anything other than ENTER,"Y" or "y" is validated as a new date could use "[Yy]*" to allow the user to spell out "yes"...
# validate the new date format as: CCYY-MM-DD
case "$answer" in
[Yy]) break
;;
[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9])
printf "%b" "Overriding $START_DATE with $answer\n"
START_DATE="$answer"
;;
*) printf "%b" "Invalid date, please try again...\n"
;;
esac
done
END_DATE=$(date -d "$DATE_DATE +7 days" '+%Y-%m-%d')
echo "START_DATE: $START_DATE"
echo "END_DATE: $END_DATE"
[[email protected] Day0801]$
1.3 Automating Date Ranges
The -d option allow you to specify a specific date instead of using now,but not all date commands support it.
[[email protected] Day0801]$ date '+%Y-%m-%d %H:%M:%S %Z'
2022-08-01 09:34:58 CST
[[email protected] Day0801]$ date -d 'today' '+%Y-%m-%d %H:%M:%S %z'
2022-08-01 09:35:42 +0800
[[email protected] Day0801]$ date -d 'yesterday' '+%Y-%m-%d %H:%M:%S %z'
2022-07-31 09:35:56 +0800
[[email protected] Day0801]$ date -d 'tomorrow' '+%Y-%m-%d %H:%M:%S %z'
2022-08-02 09:36:08 +0800
[[email protected] Day0801]$ date -d 'Monday' '+%Y-%m-%d %H:%M:%S %z'
2022-08-01 00:00:00 +0800
[[email protected] Day0801]$ date -d 'this Monday' '+%Y-%m-%d %H:%M:%S %z'
2022-08-01 00:00:00 +0800
[[email protected] Day0801]$ date -d 'last Monday' '+%Y-%m-%d %H:%M:%S %z'
2022-07-25 00:00:00 +0800
[[email protected] Day0801]$ date -d 'next Monday' '+%Y-%m-%d %H:%M:%S %z'
2022-08-08 00:00:00 +0800
[[email protected] Day0801]$ date -d 'last week' '+%Y-%m-%d %H:%M:%S %z'
2022-07-25 09:54:57 +0800
[[email protected] Day0801]$ date -d 'next week' '+%Y-%m-%d %H:%M:%S %z'
2022-08-08 09:55:11 +0800
[[email protected] Day0801]$ date -d '2 weeks' '+%Y-%m-%d %H:%M:%S %z'
2022-08-15 09:55:30 +0800
[[email protected] Day0801]$ date -d '-2 weeks' '+%Y-%m-%d %H:%M:%S %z'
2022-07-18 09:55:39 +0800
[[email protected] Day0801]$ date -d '2 weeks ago' '+%Y-%m-%d %H:%M:%S %z'
2022-07-18 09:55:54 +0800
[[email protected] Day0801]$ date -d '+4 days' '+%Y-%m-%d %H:%M:%S %z'
2022-08-05 09:56:27 +0800
[[email protected] Day0801]$ date -d '-6 days' '+%Y-%m-%d %H:%M:%S %z'
2022-07-26 09:56:40 +0800
[[email protected] Day0801]$ date -d '2022-08-01 +12 days' '+%Y-%m-%d %H:%M:%S %z'
2022-08-13 00:00:00 +0800
[[email protected] Day0801]$ date -d '3 months 1 day' '+%Y-%m-%d %H:%M:%S %z'
2022-11-02 09:57:59 +0800
[[email protected] Day0801]$ date '+%a %Y-%m-%d'
Mon 2022-08-01
[[email protected] Day0801]$ date -d 'today''+%a %Y-%m-%d'
date: invalid date ‘today+%a %Y-%m-%d’
[[email protected] Day0801]$ date -d 'today' '+%a %Y-%m-%d'
Mon 2022-08-01
[[email protected] Day0801]$ date -d 'Saturday' '+%a %Y-%m-%d'
Sat 2022-08-06
[[email protected] Day0801]$ date -d 'last Saturday' '+%a %Y-%m-%d'
Sat 2022-07-30
[[email protected] Day0801]$ date -d 'this Saturday' '+%a %Y-%m-%d'
Sat 2022-08-06
[[email protected] Day0801]$ date -d 'next Saturday' '+%a %Y-%m-%d'
Sat 2022-08-06
[[email protected] Day0801]$ date -d 'this week Friday' '+%a %Y-%m-%d'
Fri 2022-08-05
[[email protected] Day0801]$
The article presents five shell functions:
pn_month
Previous and next x months relative to the given month
end_month
End of month of the given month
pn_day
Previous and next x days of the given day
cur_weekday
Day of week for the given day
pn_weekday
Previous and next x day of weeks relative to the given day
And these were added not long before this book went to press:
pn_day_nr
(Non-recursive) Previous and next x days of the given day
days_between
Number of days between two dates
1.4 Converting Dates and Times to Epoch Seconds
Use the GNU date command with the nonstandard -d option and a standard %s format:
[[email protected] Day0801]$ # "Now" is easy
[[email protected] Day0801]$ date '+%s'
1659319857
[[email protected] Day0801]$ # Some other time needs the non-standard -d
[[email protected] Day0801]$ date -d '2022-08-01 12:00:00 +0000' '+%s'
1659355200
[[email protected] Day0801]$ perl -e 'print time, qq(\n);'
1659319953
[[email protected] Day0801]$ # Same as above
[[email protected] Day0801]$ perl -e 'use Time::Local; print timelocal(localtime()). qq(\n);'
1659320017
[[email protected] Day0801]$ perl -e 'use POSIX qw(strftime); print strftime("%s",localtime()). qq(\n);'
1659320091
[[email protected] Day0801]$ # The given time is in local time
[[email protected] Day0801]$ perl -e 'use Time::Local; print timelocal("49","59","06","05","10","105") . qq(\n);'
1131145189
[[email protected] Day0801]$ # The given time is in UTC time
[[email protected] Day0801]$ perl -e 'use Time::Local; print timegm("49","59","06","05","10","105") . qq(\n);'
1131173989
[[email protected] Day0801]$
1.5 Converting Epoch Seconds to Dates and Times
[[email protected] Day0801]$ EPOCH='1131173989'
[[email protected] Day0801]$ date -d '2022-08-01 UTC $EPOCH seconds' '+%Y-%m-%d %T %z'
date: invalid date ‘2022-08-01 UTC $EPOCH seconds’
[[email protected] Day0801]$ date -d '2022-08-01 UTC $EPOCH seconds' +'%Y-%m-%d %T %z'
date: invalid date ‘2022-08-01 UTC $EPOCH seconds’
[[email protected] Day0801]$ date -d '2022-08-01 UTC $EPOCH seconds' + '%Y-%m-%d %T %z'
date: extra operand ‘%Y-%m-%d %T %z’
Try 'date --help' for more information.
[[email protected] Day0801]$ date -d "1970-01-01 UTC $EPOCH seconds" +"%Y-%m-%d %T %z"
2005-11-05 14:59:49 +0800
[[email protected] Day0801]$ date -d "2022-08-01 UTC $EPOCH seconds" +"%Y-%m-%d %T %z"
2058-06-05 14:59:49 +0800
[[email protected] Day0801]$ date --utc --date "2022-08-01 $EPOCH seconds" +"%Y-%m-%d %T %z"
2058-06-05 06:59:49 +0000
[[email protected] Day0801]$ perl -e "print scalar(gmtime($EPOCH)), qq(\n);" #UTC
Sat Nov 5 06:59:49 2005
[[email protected] Day0801]$ perl -e "print scalar(localtime($EPOCH)), qq(\n);" #Your local time
Sat Nov 5 14:59:49 2005
[[email protected] Day0801]$ perl -e "use POSIX qw(strftime); print strftime('%Y-%m-%d %H:%M:%S',
> localtime($EPOCH)), qq(\n);"
2005-11-05 14:59:49
[[email protected] Day0801]$
1.6 Getting Yesterday or Tomorrow with Perl
[[email protected] Day0801]$ # Yesterday at this same time (note subtraction)
[[email protected] Day0801]$ perl -e "use POSIX qw(strftime); print strftime('%Y-%m-%d', localtime(time - 86400)), qq(\n);"
2022-07-31
[[email protected] Day0801]$ # Tomorrow at this same time (note addition)
[[email protected] Day0801]$ perl -e "use POSIX qw(strftime); print strftime('%Y-%m-%d', localtime(time + 86400)),qq(\n);"
2022-08-02
[[email protected] Day0801]$
1.7 Figuring Out Date and Time Arithmetic
[[email protected] Day0801]$ CORRECTION='172800' # 2 days worth of seconds
[[email protected] Day0801]$ # Code to extract the date portion from the data into $bad_date go here
[[email protected] Day0801]$ # Suppose it's this:
[[email protected] Day0801]$ bad_date='Jan 2 05:13:05' # syslog formated date
[[email protected] Day0801]$ # Convert to Epoch using GNU date
[[email protected] Day0801]$ bad_epoch=$(date -d "$bad_date" '+%s')
[[email protected] Day0801]$ # Apply correction
[[email protected] Day0801]$ good_epoch=$(( bad_epoch + $CORRECTION))
[[email protected] Day0801]$ # Make corrected date human-readable
[[email protected] Day0801]$ good_date=$(date -d "2022-08-01 UTC $good_epoch seconds") # GNU Date
[[email protected] Day0801]$ good_date_iso=$(date -d "1970-01-01 UTC $good_epoch seconds" +'%Y-%m-%d %T') # GNU
[[email protected] Day0801]$ echo "bad_date: $bad_date"
bad_date: Jan 2 05:13:05
[[email protected] Day0801]$ echo "bad_epoch: $bad_epoch"
bad_epoch: 1641071585
[[email protected] Day0801]$ echo "Correction: +$CORRECTION"
Correction: +172800
[[email protected] Day0801]$ echo "good_epoch: +$good_epoch"
good_epoch: +1641244385
[[email protected] Day0801]$ echo "good_date: +$good_date"
good_date: +Sat Aug 4 05:13:05 CST 2074
[[email protected] Day0801]$ echo "good_date_iso: $good_date_iso"
good_date_iso: 2022-01-04 05:13:05
[[email protected] Day0801]$
边栏推荐
- 移动端人脸风格化技术的应用
- PAM Palindromic Automata
- 深度学习Course2第一周Practical aspects of Deep Learning习题整理
- 2022-08-01 第八组 曹雨 泛型 枚举
- 复现gallerycms字符长度限制短域名绕过
- blender3.2.1 unit setting
- 分享10套开源免费的高品质源码,免费源码下载平台
- 【Verilog刷题篇】硬件工程师从0到入门1|基础语法入门
- y84.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十五)
- leetcode 204. Count Primes 计数质数 (Easy)
猜你喜欢
Go 微服务开发框架DMicro的设计思路
小程序毕设作品之微信体育馆预约小程序毕业设计成品(3)后台功能
分享10套开源免费的高品质源码,免费源码下载平台
How to add a game character to a UE4 scene
PDF转Word有那么难吗?做一个文件转换器,都解决了
03. GO language variable definition, function
xctf attack and defense world web master advanced area webshell
【Verilog刷题篇】硬件工程师从0到入门1|基础语法入门
【数据分析03】
文件查询匹配神器 【glob.js】 实用教程
随机推荐
移动端人脸风格化技术的应用
03. GO language variable definition, function
Three, mysql storage engine - building database and table operation
小程序毕设作品之微信美食菜谱小程序毕业设计成品(6)开题答辩PPT
excel edit a cell without double clicking
Prufer序列
使用Jenkins做持续集成,这个知识点必须要掌握
13、学习MySQL 分组
选择合适的 DevOps 工具,从理解 DevOps 开始
分享10套开源免费的高品质源码,免费源码下载平台
SAP Spartacus Accessibility E2E 端到端测试
三、mysql 存储引擎-建库建表操作
How to add a game character to a UE4 scene
visual studio code multiple editing
关于ETL的两种架构(ETL架构和ELT架构)
vscode hide menu bar
PHP算法之最接近的三数之和
深度学习Course2第二周Optimization Algorithms习题整理
小程序毕设作品之微信美食菜谱小程序毕业设计成品(7)中期检查报告
域名重定向工具 —— SwitchHosts 实用教程