how to calculate the time 10 mins ago?? unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to calculate the time 10 mins ago?? unix
# 1  
Old 09-14-2010
how to calculate the time 10 mins ago?? unix

Hi guys,

Im trying to subtract time in ksh script. i.e. basically im querying a database and i want to get the time 10mins before hand..(from)

in ksh
Code:
CurrMin=$(date "+%M")
  
  from=`expr $CurrMin - 10`
  to=$CurrMin

however if i run this i say at 2 or 3 mins past the hour, i.e. 8:03 and trp and subtract 10 mins ie 07:53, the above will not work,

Is there any simple script or function to allow me to do this ?

There is alot of posts online, but i dont see any for what i am trying todo.

Any help would be appreciated

Thanks

Moderator's Comments:
Mod Comment [CODE][/CODE] tags are your friend

Last edited by pludi; 09-14-2010 at 05:22 AM..
# 2  
Old 09-14-2010
If you have GNU date then

try this,
Code:
date -d'10 min ago' '+%F %H:%M:%S'

# 3  
Old 09-14-2010
convert the time stamp to unix seconds, subtract 600 and reconvert it


from the wiki page of date:

Quote:
To show the time in seconds since 1970-01-01 (Unix epoch):

date +"%s" -d "Fri Apr 24 13:14:39 CDT 2009"
1240596879

To convert Unix epoch time (seconds since 1970-01-01) to a human readable format:

date -d "UTC 1970-01-01 1240596879 secs"
Fri Apr 24 13:14:39 CDT 2009

Or:

date -ud @1000000000
Sun Sep 9 01:46:40 UTC 2001
# 4  
Old 09-14-2010
Hi ,

Ive tried all the above however, i am getting the following. i have Solaris 10 installed. Is there any function that i could use to get the time 10 mins ago. or any suggestions:

Code:
> date -d'10 min ago' '+%F %H:%M:%S'
date: illegal option -- d
date: illegal option -- 1
date: illegal option -- 0
date: illegal option -- 
date: illegal option -- m
date: illegal option -- i
date: illegal option -- n
date: illegal option -- 
date: invalid argument -- go
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
 
> date +"%s" -d "Fri Apr 24 13:14:39 CDT 2009"
%s
> date -ud @1000000000
date: illegal option -- d
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
> 
>

thanks

Last edited by Scott; 09-14-2010 at 06:39 PM.. Reason: Code tags, please...
# 5  
Old 09-14-2010
You'll either have to install GNU date or you'll need to write a program. You could use perl (if that helps). Just make sure you have the Date:Calc module.... you might be able to do something reasonable without that perl module.
# 6  
Old 09-15-2010
You can probably build a query just using SQL, e.g. in Oracle...
Code:
SELECT *
  FROM some_view
 WHERE last_active_time > SYSDATE - (10 / 60 / 24)

Or use perl, e.g.
Code:
STAMP=$(perl -e '($ss, $mm, $hh, $DD, $MM, $YY) = localtime(time() - 600);
       printf "%04d%02d%02d%02d%02d", $YY + 1900, $MM + 1, $DD, $hh, $mm')
echo $STAMP

# 7  
Old 11-17-2010
How to calculate unix epoch time with DC(Caulator)
--------------------------------------------------------------------------------
Hi All,
I have a similar problem:
In our code we are using below code to calculate the UNIX Epoch Time from the time stamp present in the file.
Can any one explain how the DC operates in below command and how we calculate the UNIX EPOCH time from this.
Date incide the file is 20:26:51
Output we get is 1289943554

File content:
0|0|4|472|2010-11-15 23:22:14|2010-11-15 23:30:06
0|0|3|200|2010-11-15 20:26:51|2010-11-15 20:30:11
Can you please explain.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX and Linux Applications

Sendmail delay: 3 mins 11 secs... Every time.

Hi all, I would like some help with a sendmail problem: We have a new system comprising of 4 T7-1 servers, each hosting 5 LDOMs, all domains running Solaris 11.3 All emails sent from every one of these domains (including the control domains) sit in the queue for 3 mins 11 secs (sometime 3m 12s,... (11 Replies)
Discussion started by: Mysturji
11 Replies

2. Shell Programming and Scripting

How to get past 30 mins time in Solaris?

Hi guys, could you help to find a way to get the past 30 mins time in solaris. version: bash-3.00# uname -a SunOS solaris 5.10 Generic_142910-17 i86pc i386 i86pc I had tried the following ways, it works fine in GNU Linux, but doesn't work in Solaris. # date Tue Apr 2 01:01:49 CST... (4 Replies)
Discussion started by: ambious
4 Replies

3. Shell Programming and Scripting

How long ago since time - Using perl

echo "1337124526" | perl -pe 's/(\d+)/easttime($1)/e' the above gives a date and time. how can i subtract the date and time given by this command, from the current present date? can this be a one liner or as close to a one-liner as possible? (1 Reply)
Discussion started by: SkySmart
1 Replies

4. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies

5. UNIX for Advanced & Expert Users

getting time mins ago

Hi I trying to get 5 mins ago time using below command echo `date +%R -d "1 min ago"` but this is giving only current time. Please help (6 Replies)
Discussion started by: cka
6 Replies

6. Shell Programming and Scripting

How to calculate unix epoch time with DC(Calculator)

Hi All, In our code we are using below code to calculate the UNIX Epoch Time from the time stamp present in the file. Can any one explain how the DC operates in below command and how we calculate the UNIX EPOCH time from this. Date incide the file is 20:26:51 Output we get is 1289943554... (3 Replies)
Discussion started by: mithunpatel
3 Replies

7. Shell Programming and Scripting

calculate 13 months ago

hi, I have a big file that contains datas since 4 years ago. I need re-create this file but just lines that are 13 months ago from today. see what I have: ( I have a file.ksh that calls this file.scl ok !!) ======== file.scl ================ /STATISTICS=stderr /STABLE /NODUPLICATES... (4 Replies)
Discussion started by: andrea_mussap
4 Replies

8. Shell Programming and Scripting

calculate 13 months ago

hi, I have a big file that contains datas since 4 years ago. I need re-create this file but just lines that are 13 months ago from today. see what I have: ( I have a file.ksh that calls this file.scl ok !!) ======== file.scl ================ /STATISTICS=stderr /STABLE /NODUPLICATES... (3 Replies)
Discussion started by: andrea_mussap
3 Replies

9. Shell Programming and Scripting

Finding files which are modified few mins ago

Hi All, I have a requirement to find out the files which are modified in the last 10 minutes. I tried the find command with -amin and -mmin options, but its not working on my AIX server. Can anyone of you could help me. Thanks in advance for your help. Raju (3 Replies)
Discussion started by: rajus19
3 Replies

10. Shell Programming and Scripting

Cron to run first day of month to calculate date 3 months ago

Hi, I would like to find out how can i calculate a date which is 3 months ago. I intend to run a cron job on the 1st of every month, and calculate the month 4 months earlier from the date. For example, if today's date is 1st May 2007, i would like to return 012007( January 2007). i can get... (1 Reply)
Discussion started by: new2ss
1 Replies
Login or Register to Ask a Question