当前位置:网站首页>Chapter 11 Working with Dates and Times
Chapter 11 Working with Dates and Times
2022-08-01 23:12:00 【Dreamer 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 dates1.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]$ 边栏推荐
猜你喜欢
随机推荐
excel edit a cell without double clicking
Graph Theory - Strongly Connected Component Condensation + Topological Sort
IDEA common plugins
Use Jenkins for continuous integration, this knowledge point must be mastered
sys_kill系统调用
10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享
excel split text into different rows
Nacos配置中心之加载配置
Solve the port to take up
【好书推荐】第一本无人驾驶技术书
域名重定向工具 —— SwitchHosts 实用教程
Calculate the midpoint between two points
美赞臣EDI 940仓库装运订单详解
xctf attack and defense world web master advanced area webshell
xss相关知识点以及从 XSS Payload 学习浏览器解码
img 响应式图片的实现(含srcset属性、sizes属性的使用方法,设备像素比详解)
6133. Maximum number of packets
华为无线设备配置全局双链路冷备份(AC全局配置方式)
CAKE:一个用于多视图知识图谱补全的可扩展性常识感知框架
ping no reply


![Thesis understanding [RL - Exp Replay] - Experience Replay with Likelihood-free Importance Weights](/img/f1/9824f32dd4fe4b3e94af3f945b1801.png)






