Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a date in milliseconds since Unix epoch format, how do I substract 5 minutes from it?

share|improve this question

Supposing your timestamp is in a variable timestamp, and in milliseconds since the epoch:

fiveminutesbefore=$((timestamp - 5 * 60 * 1000))

This uses arithmetic expansion to subtract 5 lots of 60 (seconds in a minute) lots of 1000 (milliseconds in a second) from your value timestamp, giving a time five minutes earlier as you would expect.

share|improve this answer
    
My timestamp is actually passed as an argument into my bash script, and it won't do ss=$(($1-5*60*1000))...How can I convert it before substracting? – Alexiel Sep 22 '14 at 5:19
    
Are you sure this is Bash? ss=$(($1-5*60*1000)) is valid and working Bash. You can save it in a named variable first if you want: timestamp=$1, but it shouldn't make any difference. Do you get an error message? If so, what is it? – Michael Homer Sep 22 '14 at 5:26
    
There was an error, the argument had an incorrect format! Thanks for your anser) – Alexiel Sep 22 '14 at 8:58

5 minutes of 60 seconds of 1000 milliseconds each gives 300000.

You can subtract this from a variable that containts current date in milliseconds using $(( )):

dd=$(($(date +'%s * 1000 + %-N / 1000000')))
ddmin5=$(($dd - 300000))
echo $ddmin5

The milliseconds calculation comes from this answer

share|improve this answer
2  
The nice thing about the "seconds since epoch" format is that for a computation like this, there is no need to consider time zones or corner cases for daylight savings or leap seconds. – Nate Eldredge Sep 22 '14 at 5:12

There are too many ways:

fiveminutesbefore=$[$timestamp - 5 * 60 * 1000]

or

fiveminutesbefore=`echo "$timestamp - 5 * 60 * 1000" | bc -l`

or

fiveminutesbefore=`echo "$timestamp" | python -c 'import sys; t=sys.stdin.read(); print int(t) - 5 * 60 * 1000'`

etc...

share|improve this answer
    
Hello and welcome to ul.sx! Please be specific and ensure your answers are always complete, including stating you answer's idea before providing code snippets. – Bananguin Sep 22 '14 at 10:36

print solaris sun os minus 10min

dt="$(date +%H:"$(( `date +%M`-10))":%S)"
share|improve this answer
    
Mind adding some description? – phk Oct 7 at 22:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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