获取上一个星期六的日期-BusyBox 1.1.0 [英] Get date of last saturday - BusyBox 1.1.0

查看:110
本文介绍了获取上一个星期六的日期-BusyBox 1.1.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于BusyBox中的日期不如 gnu date 强大,因此我在计算上个星期六的日期时遇到了问题。

Since the date in BusyBox is not as powerful as gnu date, I have problems to calculate the date of last saturday.

last_sat=`date +"%Y-%m-%d" -d "last saturday"`

仅适用于gnu日期。

我发现类似这样的东西可以从Epoch中计算

I've found something like this to calculate from Epoch

busybox date -D '%s' -d "$(( `busybox date +%s`+3*60 ))"

但是我的BusyBox(v1.1.0)无法识别 -D

but my BusyBox (v1.1.0) doesn't recognize the -D argument.

有什么建议吗?

推荐答案

最后一个星期六今天之前,在busybox下1.16:

For the last Saturday before today, under busybox 1.16:

date -d "UTC 1970-01-01 $(date +"%s - 86400 - %w * 86400"|xargs expr) secs"

工作原理:将当前日期取为秒,减去一天,减去一天乘以当前工作日的天数,然后将这些秒转换回日期。

How it works: take the current date in seconds, subtract one day, subtract one day times the number of the current weekday, then convert those seconds back to a date.

编辑:hac之后国王一起构建了1.1版本,

after hacking together a build of 1.1, this works:

date -d "1970.01.01-00:00:$(date +"%s - 86400 - %w * 86400"|xargs expr)"

此工作版本基于在代码读取时:

This working version is based on code-reading:

} else if (t = *tm_time, sscanf(t_string, "%d.%d.%d-%d:%d:%d", &t.tm_year,
                    &t.tm_mon, &t.tm_mday, 
                    &t.tm_hour, &t.tm_min,
                        &t.tm_sec) == 6) {
    t.tm_year -= 1900;  /* Adjust years */
    t.tm_mon -= 1;  /* Adjust dates from 1-12 to 0-11 */

这篇关于获取上一个星期六的日期-BusyBox 1.1.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆