# Compute Sum Of Date

A third party script we use pulls information based on the number of minutes a certain job runs for. I need to enter this input in the script.

E.g
Here is an example

``````    export etool_start=`date +"%Y%m%d%H%M:"`
``````

My Main script will run in between the start and end time.

``````    export etool_end=`date +"%Y%m%d%H%M"`
``````

The output from the above results will look like this 201607050220:201607050230. The time is 0220 to 0230. My script wont accept this date type and will accept only minutes like this -0010 (10 mins). How can i create a math to help me get this result. The time i gave is just an example, the sample interval could be even between 2 mins to 120 minutes to a day. How can i make use of the date command to get this sort of an output.

Please help.

Thanks.

• always two answers to this sort of question: use a scripting language with a good date library (python leaps to mind) or contemplate some "seconds since epoch" math. "date +%s" returns seconds since 1970-01-01. subtract the start date in seconds since epoch from the end date and divide by 24*60*60 and you have the days betwixt. Jul 6, 2016 at 4:36
• I’m a little confused by the question.  You show code that sets two variables, but then you say, “the above results will look like this” and give a single string that is the concatenation of the values of the two variables.  Sure, it’s easy enough to split them up again, but what’s the deal?  Also, are these two values the only information you have about the start and end times, or can you capture your own? Please do not respond in comments; edit your question to make it clearer and more complete. Jul 6, 2016 at 5:51

## 2 Answers

It is much simpler to measure time in seconds since epoch (`+%s`) Then elapsed time in seconds in found by merely substracting and conversion from seconds to minute just requires dividing by 60:

``````export etool_start=\$(date +"%s")
do_stuff_with_etool
export etool_stop=\$(date +"%s")
echo "elapsed time=\$(( (etool_stop - etool_start)/60 )) minutes"
``````

If you do need the times in human form, you can convert them. Using GNU `date`:

``````\$ date -d "@\$etool_start"
Tue Jul  5 21:30:19 PDT 2016
``````

Or:

``````\$ date -d "@\$etool_start"  "+%Y%m%d%H%M"
201607052130
``````

### More on formatting

To express the elapsed time in minutes in different formats, we can use `printf`. For example, to produce the elapsed time with four digits, we can use the `%04i` format:

``````\$ printf '%04i\n' "\$(( (\$etool_stop - \$etool_start)/60 ))"
0001
``````

A format of `%4i` would give us an integer (i) in four characters. If we want leading zeros instead of leading spaces, then we use `%04i`.

If we want the opposite sign for the number:

``````\$ printf '%05i\n' "\$(( (\$etool_start - \$etool_stop)/60 ))"
-0035
``````

Because the minus sign requires an extra character, we have increased the allotted space from 4 to 5: `%05i`.

### More resolution

Because the above example used bash's arithmetic which is integer-only. If you want elapsed time in minutes with fractions of a minute, another tool, such as `bc`, can be used:

``````\$ echo "(\$etool_stop - \$etool_start)/60" | bc -l
1.38333333333333333333
``````

### What is seconds since epoch?

From `man date`, seconds since epoch is defined by:

%s
seconds since 1970-01-01 00:00:00 UTC

Since it is defined this way, local time zone is irrelevant. Consequently, computing it under the "local time zone" or under universal time, `-u`, makes no difference:

``````\$ date +%s; date -u +%s
1467780345
1467780345
``````
• Using UTC times would prevent DST and local time change caused glitches.
– user79743
Jul 6, 2016 at 4:44
• @BinaryZebra I have investigated that one before: seconds since epoch, `date +%s`, is in UTC. It turns out that the `-u` flag is superfluous. It is a subtle point, though. I have added some info on it to the answer. Jul 6, 2016 at 4:47
• Thanks i get the result but formatting is the main area i need. Jul 6, 2016 at 5:00
• export etool_start=\$(date +"%s") sleep 62s export etool_stop=\$(date +"%s") time=\$(( (etool_stop - etool_start)/60 )) echo \$time 1 I need the format like this -0010 for ten mins or -0120 for two hours or -1440 for 24 hours.. Jul 6, 2016 at 5:01
• @John1024 Thanks-works like a charm. best place where i could learn advanced scripting ? i truly suck, it seemed really simple. Jul 6, 2016 at 5:18

In bash:

``````\$ export etool_starting=`date -u +"%s"`
\$ export etool_ending=`date -u +"%s"`
``````

If only minutes are needed:

``````\$ printf '%05d\n' "\$(( (etool_starting - etool_ending) / 60 ))"
-0120       ### If 120 minutes have elapsed in between.
``````
• :- with your command i get this printf: `(': invalid format character Jul 6, 2016 at 5:11
• @Super That doesn't happen in bash. Are you using dash as the default shell instead?
– user79743
Jul 6, 2016 at 5:50