You're welcome.  Hope someone somewhere finds it useful.

A bit of back story ...

The reason I wanted to know how to work with date was because we have
a number of systems with RAID controllers that automatically discharge
and recharge their batteries.  This is called the battery relearn
cycle and, apparently, is a good thing for the battery. That relearn
cycle is not such a good thing for disk I/O performance.  And it is
really bad when it happens during peak hours on high volume production
servers.

Unfortunately, you can't turn the relearn cycle off.  Nor can you
reschedule it on the controller.

However, the controller does show the number of days before the next
relearn cycle.  And you can manually force a relearn.  If we could
calculate the date of the next automatic relearn cycle, then we could
schedule a manual relearn cycle for an earlier date/time that would be
more convenient for us.

So, the challenge was to calculate the date of the Saturday before the
next automatic relearn cycle.

Here's one solution:

dbnr=72                                  # days before next relearn
delta=$(( ${dbnr}*24*60*60 ))            # dbnr days in seconds
today=$(date +%s -d 0)                   # today at midnight in seconds
future=$(( ${today} + ${delta} ))        # future in seconds
dow=$(date +%w -d @${future})            # numeric day of week of future
dts=$(( 6 - 7 - ${dow} ))                # number of days to previous
Saturday(6) in future
rdt=$(( ${future} + (${dts}*24*60*60) )) # relearn date in seconds

Display the future date:

date -d @${rdt}

To schedule something in the future using the 'at' command:

atdt=$(date +%Y%m%d%H%M.%S -d @${rdt})   # date format suitable for at -t
at -f ~/bin/relearn.sh -t ${atdt}        # scheduling the relearn for
a Saturday morning

This isn't perfect as it doesn't handle a few edge cases.  But works
good enough for our use cases.

Regards,
- Robert

On Fri, Apr 5, 2013 at 11:12 AM, Scott Granneman <[email protected]> wrote:
> Thank you very much for this! It is now going into Linux Phrasebook, volume
> 2.
>
> Scott
> --
> R. Scott Granneman
> [email protected] ~ www.granneman.com ~ granneman.tel
> Full list of publications @ http://www.granneman.com/publications
>  My latest book: Mac OS X for Power Users @ http://www.granneman.com/books
>
> “One of the differences between people who do science and people who don’t
> is the people who do science realize that what you’re trying to do in
> science is falsify a hypothesis. And only after you examine all sorts of
> evidence and you can’t falsify a hypothesis do you establish that the
> hypothesis is true. The people trying to prove a hypothesis look at any
> piece of positive evidence and then stop.”
>      ---Peter Rosen, emergency room doctor
>
>
> Robert Citek wrote:
>>
>> On Sun, Mar 17, 2013 at 10:00 AM, Carl Fitch<[email protected]>  wrote:
>>>
>>> Very interesting! On my MAcBook there is not a --date parameter and -d
>>> is:
>>>
>>>>       -d dst  Set the kernel's value for daylight saving time.  If dst
>>>> is non-zero, future calls to gettimeofday(2)
>>>>               will return a non-zero for tz_dsttime.
>>>
>>>
>>> My experience with --date was as a way to convert formats in bash
>>> scripts:
>>>
>>>>   date +%F --date=05/12/1953
>>>> 1953-05-12
>>
>>
>> Exactly.  One use of the date command is to display various date/time
>> formats.  By default, date uses the current system time and timezone.
>> But the --date= option enables date to use an alternate date/time
>> specified by STRING.  What range of possible strings are valid is not
>> immediately obvious.  It's as though the STRING section of the manual
>> was omitted.
>>
>> Some examples:
>>
>> # default, today, now -- all equivalent
>> date
>> date --date=now
>> date --date=today
>>
>> # unix epoch
>> date --date=@0
>> date --date=@0 -u
>>
>> # past, future
>> date --date=yesterday
>> date --date=tomorrow
>> date --date="next week"
>> date --date="last week"
>>
>> # future day of week, Sunday-Saturday, abbreviations work.  Midnight
>> local timezone.
>> date --date=sat
>> date --date=sat -u
>>
>> # 1-2 digit number == today's hour (0-23), local timezone, unless
>> specified
>> date --date=10
>> date --date=10 -R
>> date --date=10 -R -u
>>
>> # 3-4 digit numbers == today's time
>> date --date=010
>> date --date=1010
>>
>> # 5-?? digit numbers == a date; 6-digit dates are assumed to be 1900
>> or 2000 years
>> date --date=71010 -u
>> date --date=871010 -u
>> date --date=0871010 -u
>> date --date=9871010 -u
>> date --date=19871010 -u
>>
>> What surprises me is that I have not found any of this information
>> described in the man pages.  Instead, it is documented in the info
>> pages, but again somewhat cryptically and with few examples.
>>
>> Regards,
>> - Robert
>>
>
> --
> --
> Central West End Linux Users Group (via Google Groups)
> Main page: http://www.cwelug.org
> To post: [email protected]
> To subscribe: [email protected]
> To unsubscribe: [email protected]
> More options: http://groups.google.com/group/cwelug
> --- You received this message because you are subscribed to the Google
> Groups "Central West End Linux Users Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

-- 
-- 
Central West End Linux Users Group (via Google Groups)
Main page: http://www.cwelug.org
To post: [email protected]
To subscribe: [email protected]
To unsubscribe: [email protected]
More options: http://groups.google.com/group/cwelug
--- 
You received this message because you are subscribed to the Google Groups 
"Central West End Linux Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to