当前位置:网站首页>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 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]$ 边栏推荐
- 联邦学习入门
- 欧拉路径与欧拉回路
- 华为无线设备配置全局双链路冷备份(AC全局配置方式)
- 复现gallerycms字符长度限制短域名绕过
- APP special test: traffic test
- JS prototype hasOwnProperty in Add method Prototype end point Inherit Override parent class method
- 最短路模板
- 关于ETL的两种架构(ETL架构和ELT架构)
- [深入研究4G/5G/6G专题-48]: 5G Link Adaption链路自适应-4-下行链路自适应DLLA-PDCCH信道
- excel split text into different rows
猜你喜欢

PHP算法之电话号码的字母组合

【SeaTunnel】从一个数据集成组件演化成企业级的服务

Postman batch test interface detailed tutorial

(Translation) How the contrasting color of the button guides the user's actions

JS prototype hasOwnProperty in Add method Prototype end point Inherit Override parent class method

隔离和降级

2022年最新河北建筑八大员(机械员)模拟考试题库及答案

系统可用性:SRE口中的3个9,4个9...到底是个什么东西?

如何给 UE4 场景添加游戏角色

一种灵活的智能合约协作方式
随机推荐
Ten years after graduation, financial freedom: those things that are more important than hard work, no one will ever teach you
基于 OData 模型和 JSON 模型的 SAP UI5 表格控件行项目的添加和删除实现
解决yolov5训练时出现:“AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train ”
小程序毕设作品之微信体育馆预约小程序毕业设计成品(4)开题报告
visual studio code multiple editing
一种灵活的智能合约协作方式
将vim与系统剪贴板的交互使用
excel remove all carriage return from a cell
Prufer序列
从0到1:图文投票小程序设计与研发笔记
ping no reply
联邦学习入门
How to prevent governance attacks in DAOs?
1391D. 505 状压dp
数据分析04
E - Integer Sequence Fair
leetcode刷题
解决 win10 下 ISE14.7的 iMPACT 崩溃问题 - FPGA 笔记
Prufer sequence
AQS