cancel
Showing results for 
Search instead for 
Did you mean: 

Info on timestamp

Karthik_sg
Frequent Advisor

Info on timestamp

hi guys,
Can any body tell me how to subtract the current timestamp - 3minutes.
Thanks in advance.
10 REPLIES
Danny Petterson - DK
Trusted Contributor

Re: Info on timestamp

perl -e 'print scalar localtime(time-180)'

...where "180" is the seconds for three minutes.

Yours
Danny Petterson
Karthik_sg
Frequent Advisor

Re: Info on timestamp

Can u tel me in shell scripting pls
Karthik_sg
Frequent Advisor

Re: Info on timestamp

And the timestamp is in this format.
2007-07-19 14:29:33
Now How to subtract the 3 minutes tht i had askd earlier.
Oviwan
Honored Contributor

Re: Info on timestamp

Hey

I suggest you to use perl:
perl -e '@x=localtime(time-3*60);printf"%4d-%02d-%02d %02d:%02d:%02d\n",$x[5]+1900,$x[4]+1,$x[3],$x[2],$x[1],$x[0]'

Regards

p.s. I have assigned points to 4 of 36 responses --> don't forget to assign points, also in your other threads!
Stuart Browne
Honored Contributor

Re: Info on timestamp

In raw shell, not easily from a timestamp.

You can use 'date -d ""', but you can't do further modifications to it.

If you just wanted the current time 3 minutes ago, you could have used 'date -d "3 minutes ago"', but this doesn't work stacked onto another -d.

Unfortunately, 'date -d' doesn't understand the Epoch form of time, i.e. 'date -d 1184819193'. If it did, you could stack some date's, but it doesn't.

So, use the funky perl method. You can $() it in a subshell *shrug*.
One long-haired git at your service...
Karthik_sg
Frequent Advisor

Re: Info on timestamp

And can u pls explain this


perl -e '@x=localtime(time-3*60);printf"%4d-%02d-%02d %02d:%02d:%02d\n",$x[5]+1900,$x[4]+1,$x[3],$x[2],$x[1],$x[0]'

with respect to

2007-07-19 14:29:33

i have assigned points and thanks.
Stuart Browne
Honored Contributor

Re: Info on timestamp

Wait, I'm confused. I think I mis-read your original post.

Do you want to take 3 minutes away from the current time, or from a pre-defined time?

If from the current time, you can use either the perl method above, or:

date -d '3 minutes ago' +'%Y-%m-%d %H:%M:%S'

But this all depends on how and where you want to use it.

For instance, if you're wanting to do this calculation to use in an SQL query, it's easier to do it *IN* SQL. The MySQL syntax would be simply:

SELECT DATE_SUB(NOW(), INTERVAL 3 MINUTE);

In other SQL's, there can be slightly different syntax.

So, can you tell us where you want to use this? Perhaps then we can give you a better answer.
One long-haired git at your service...
Karthik_sg
Frequent Advisor

Re: Info on timestamp

I am using it in mysql.the difference is used in a script where we generate alerts.i.e, if the time is same for the last 3 minutes then we are suppose to notify via the script.thanks,i wil try try this and let u know
Karthik_sg
Frequent Advisor

Re: Info on timestamp

hi The query u had sent doesnt work in mysql .u have any idea how it goes in mysql
Stuart Browne
Honored Contributor

Re: Info on timestamp

.. How can you not get that to work?

SELECT data_field FROM table WHERE time_field > DATE_SUB(NOW(), INTERVAL 3 MINUTE);

This assumes that the 'time_field' is in a date/time/datetime/timestamp format.

If it's in epoch time (a flat integer), then use 'UNIX_TIMESTAMP() - 180' instead.
One long-haired git at your service...