当前位置:网站首页>busybox date 时间的加减
busybox date 时间的加减
2022-07-17 05:12:00 【xp5xp6】
1、下载安装busybox:
复制代码
wget http://busybox.net/downloads/busybox-1.29.3.tar.bz2
tar -jxvf busybox-1.29.3.tar.bz2
cd busybox-1.29.3
make defconfig //如果对根文件系统的大小不是很苛求,可以直接使用busybox的默认配置
make
make install
构建date链接
ln -sf ./busybox ./date
复制代码
busybox date参数详解
复制代码
[busybox-1.29.3]# ./date --help
BusyBox v1.29.3 (2019-11-25 11:00:35 CST) multi-call binary.
Usage: date [OPTIONS] [+FMT] [TIME]
Display time (using +FMT), or set time
[-s,--set] TIME Set time to TIME
-u,--utc Work in UTC (don't convert to local time)
-R,--rfc-2822 Output RFC-2822 compliant date string
-I[SPEC] Output ISO-8601 compliant date string
SPEC='date' (default) for date only,
'hours', 'minutes', or 'seconds' for date and
time to the indicated precision
-r,--reference FILE Display last modification time of FILE
-d,--date TIME Display TIME, not 'now'
-D FMT Use FMT for -d TIME conversion
Recognized TIME formats:
hh:mm[:ss]
[YYYY.]MM.DD-hh:mm[:ss]
YYYY-MM-DD hh:mm[:ss]
[[[[[YY]YY]MM]DD]hh]mm[.ss]
‘date TIME’ form accepts MMDDhhmm[[YY]YY][.ss] instead
复制代码
Linux系统中的date一般可以直接进行日期的相减,
例如: centos7系统
date 获取前一天的时间: date -d -1day 或者 date -d ‘1 day ago’
date 获取前一个月的时间: date -d ‘1 month ago’
date 获取前一年的时间: date -d ‘1 year ago’
而busybox date则不可以直接获取前一天的时间,,需要时间数字相减的方法来实现获取前一天的时间
例如: busybox date 获取前一天的时间:
考虑到当前时间是年度第一天1月1日的情况,代码如下:
复制代码
year=date +%Y // 获取当前时间的年份
month=date +%m // 获取当前时间的月份
day=date +%d // 获取当前时间的日期
if [[ “ d a y " = = " 01 " ] ] ; t h e n / / 如 果 当 前 时 间 是 1 号 , 则 考 虑 一 下 月 份 问 题 i f [ [ " day" == "01" ]];then // 如果当前时间是1号 ,则考虑一下月份问题 if [[ " day"=="01"]];then//如果当前时间是1号,则考虑一下月份问题if[["month” == “01” ]];then // 如果当前时间是1月1号,,获取的前一天则是去年的最后一天 ,所以年份需要减一,月份和日期则是12月31日
year=expr $year - 1
yesterday=“ y e a r − 12 − 31 " e l i f [ [ " {year}-12-31" elif [[ " year−12−31"elif[["month” == “03” ]];then // 如果当前时间是3月1号,获取的前一天则是2月的最后一天,2月又分28天和29天,,所以需要和4取余,
year_type=expr ${year} % 4
if [[ “ y e a r t y p e " = = " 0 " ] ] ; t h e n / / 与 4 取 余 为 0 则 为 闰 年 , 这 一 年 的 2 月 最 后 一 天 是 29 号 , , 取 余 不 为 0 则 为 平 年 , 2 月 的 最 后 一 天 是 28 号 d a y = " 29 " e l s e d a y = " 28 " f i y e s t e r d a y = " year_type" == "0" ]];then // 与4取余为0则为闰年,这一年的2月最后一天是29号,,取余不为0则为平年,2月的最后一天是28号 day="29" else day="28" fi yesterday=" yeartype"=="0"]];then//与4取余为0则为闰年,这一年的2月最后一天是29号,,取余不为0则为平年,2月的最后一天是28号day="29"elseday="28"fiyesterday="{year}-02- d a y " / / 1 , 3 , 5 , 7 , 8 , 10 , 12 月 均 是 31 天 e l i f [ [ " {day}" // 1,3,5,7,8,10,12月均是31天 elif [[ " day"//1,3,5,7,8,10,12月均是31天elif[["month” == “02” || “ m o n t h " = = " 04 " ∣ ∣ " month" == "04" || " month"=="04"∣∣"month” == “06” || “ m o n t h " = = " 08 " ∣ ∣ " month" == "08" || " month"=="08"∣∣"month” == “09” || “$month” == “11” ]];then
month=expr ${month} - 1
yesterday=" y e a r − {year}- year−{month}-31"
elif [[ “ m o n t h " = = " 05 " ∣ ∣ " month" == "05" ||" month"=="05"∣∣"month” == “07” || “ m o n t h " = = " 10 " ∣ ∣ " month" == "10" || " month"=="10"∣∣"month” == “12” ]];then
month=expr ${month} - 1
yesterday=" y e a r − {year}- year−{month}-30"
fi
else
yesterday= y e a r − {year}- year−{month}-expr ${day} - 1
fi
echo $yesterday
复制代码
努力成为一名优秀的工程师
边栏推荐
- Conversion, isolation and transmission of international standard signals 0-5v/0-10v/1-5v, 0-10ma/0-20ma/4-20ma, etc
- RS-485/232转4-20mA/0-10V隔离D/A转换器
- Review of software process and management (VIII)
- 【简单快速】启动后桌面正常下方任务栏无反应/鼠标一直转圈
- RestClient-多条件聚合
- HM8203线性两串充电管理控制器IC
- 自动补全 & (自定义)拼音分词器 & 搜索时注意事项
- 基于 VITA57.1 标准的单通道 6GSPS 12 位AD采集,单通道 6GSPS 16 位 DA(AD9176)输出 子卡
- Xilinx UltraScale+ MPSOC(ZU9EG/ZU15EG)高性能 PCIe 数据预处理板
- Solve cannot read properties of null (reading 'pickalgorithm')
猜你喜欢

3.7V锂电池升压到5V1A,FS2114升压转换芯片设计布局

EasyDarawin流媒体服务器介绍

Fs5383a lithium battery 3.7V input power supply solar lawn lamp drive IC

[antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique.....

CS品牌SDNAND在颜色检测仪行业中的应用案例

CS品牌SDNAND和STM32MCU成功合作资料

Xinlinx zynq7010国产替代 FMQL10S400 全国产化 ARM 核心板+扩展板

Loadng class `com.mysql.jdbc.Driver‘. This is deprecated. The new driver class is `com.mysql.cj.jdb

Minio installation, deployment and simple use

Review of software process and management (10)
随机推荐
FMC子卡:4 路 SFP+万兆光纤网络 FMC 子卡
处理中文分词 ik分词器以及拓展和停止字典
升压DC/DC转换器
HM8203线性两串充电管理控制器IC
获取当前年月日、时分秒、星期,并实时更新
为什么方案商“钟情”选择CS创世SD NAND
sd nand与nandflash的区别?
Chrome浏览器设置 【显示右上角 翻译语言图标】
NAND NOR FLASH闪存产品概述
2021-11-10 micropyton TB6600步进驱动类
什么是tSD/qSD?CS创世 SD NAND到底是什么?
Xinlinx zynq7010国产替代 FMQL10S400 全国产化 ARM 核心板+扩展板
宽电压输入高电压输出 电压控制型
5V boost charging 8.4v chip
Hm9922 switching buck LED constant current driver IC
Xilinx UltraScale+ MPSOC(ZU9EG/ZU15EG)高性能 PCIe 数据预处理板
Antd is not defined
CS品牌SD NAND在空气质量检测行业中的应用案例
2021 - 09 - 15
QTSS回调例程