0

I wanted to pass parameters in a shell script which calls a java program...

For example the current time is 2011:02:23 01:05 then I need to floor to nearest whole time like 1:00 (starttime parameter to java program) if its 1:05 and 1:15 (end time parameter to java program) the below timestamps I wanted to dynamically pass to a java program based on current time.

Example param(15min interval) to pass to java program via shell script.

java 2011:02:23 01:00 (start time) 2011.02.23 01:15 (end time)
java 2011.02.23 01:15 (starttime) 2011.02.23 01:30 (end time)

Can I ? or is there any way to generate the date with time in above specified format in unix ?

Note : This date: get current 15 minute interval doesnt serve my purpose.

  • "ceil" (ceiling) rounding would take 1:05 to something higher, not something lower such as 1:00. Can you spell out the translation in more detail? What are the possible inputs and resulting outputs? – Jeff Schaller Aug 31 '18 at 0:41
  • @jeff-schaller : updated. I need 2011:02:23 01:00(starttime param to java) if current time is 2011:02:23 01:08 . end time should look like 2011:02:23 01:15 – RamPrasad G Aug 31 '18 at 0:48
  • @JeffSchaller any pointers you can help with ? – RamPrasad G Aug 31 '18 at 17:12
0

You can get the number of minutes multiple of 15 lower than current minutes (or minutes given in the first parameter) with:

a=$(date date -d "2011-02-23 01:05" +%-m)        # need only minutes.
a=${1:-"$a"}                   # or use the value from the first parameter.
a=${a#0}                       # remove one optional leading zero to
                               # avoid issues with 08 and 09.
b=$((a-a%15))                  # Round down to a 15 minutes multiple.
c=$((b+15))                    # end time

echo "$b $c"

If you need a full date string use:

$ printf 'java %s%02d\n' "$(date -d "2011-02-23 01:05" +"%Y:%m:%d %H:")" "$b"
java 2011:02:23 01:00

Or, in bash:

$ printf -v a '%(%s)T\n' -1
$ printf '%(java %Y-%m-%d %H:%M:%S)T\n' "$(( a - a % (60*15) ))"
java 2018-08-30 22:45:00
  • Hi Isaac not working as expected in the post – RamPrasad G Aug 31 '18 at 1:29
  • @RamPrasadG Maybe a description of which part doesn't work could be useful. :-( – Isaac Aug 31 '18 at 2:21
  • Thanks for the solution! some how its not working for me. – RamPrasad G Aug 31 '18 at 20:21
  • @RamPrasadG I am sorry to hear that. Good luck. – Isaac Aug 31 '18 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.