Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get date of last saturday - BusyBox 1.1.0

Tags:

date

sh

busybox

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"`

only works fine with gnu date.

I've found something like this to calculate from Epoch

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

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

Any suggestions?

like image 827
cuilster Avatar asked Sep 16 '25 14:09

cuilster


1 Answers

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.

EDIT: 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 */
like image 172
Eric Avatar answered Sep 19 '25 08:09

Eric