date arthemetic - really urgent


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting date arthemetic - really urgent
# 1  
Old 11-06-2006
date arithmetic - really urgent

I get the timestamp of a file. the output of the timestamp is like this:

Nov 3 11:04

I need to get the current date and subtract it from the timestamp.

the output of the difference should be in minutes..

eg) (current_date-timestamp = x minutes)

pls help guys.. thanks in advance

Last edited by ragha81; 11-06-2006 at 11:07 AM..
# 2  
Old 11-06-2006
This uses my datecalc script which is available on this site.

Code:
#! /usr/bin/ksh

#
# Initialize a few things

alias datecalc=./datecalc
MJan=1 MFeb=2 MMar=3 MApr=4 MMay=5 MJun=6
MJul=7 MAug=8 MSep=9 MOct=10 MNov=11 MDec=12

#
# Get current date and time and load into Now array removing leading zeros

set +A Now $(date '+%Y %m %e %k %M')
Now[1]=${Now[1]#0}
Now[4]=${Now[4]#0}
echo "Now = ${Now[0]} and ${Now[1]} and ${Now[2]} and ${Now[3]} and ${Now[4]}"

#
# Here is the timestamp
timestamp="Nov 3 11:04"

#
# Parse timestamp into the input array removing leading zeros from the numbers

IFS=": "
set +A input $(echo $timestamp)
input[2]=${input[2]#0}
input[3]=${input[3]#0}
echo "input = ${input[0]} and ${input[1]} and ${input[2]} and ${input[3]}"

#
# Load TS array with month, day, hour, and min of timestamp and remove leading zeros
# Guess the timestamp year

eval TS[1]=\$M${input[0]}
TS[2]=${input[1]}
TS[3]=${input[2]}
TS[4]=${input[3]}
(( ${TS[1]} > ${Now[1]} )) && ((TS[0]=${Now[0]}-1)) || TS[0]=${Now[0]}
echo "TS = ${TS[0]} and ${TS[1]} and ${TS[2]} and ${TS[3]} and ${TS[4]}"

#
#  Get number of days expressed as minutes
days=$(datecalc -a ${Now[0]} ${Now[1]} ${Now[2]} - ${TS[0]} ${TS[1]} ${TS[2]})
echo days = $days
((minutes = days * 60 * 24))
echo minutes = $minutes

#
#  Get the minutes past midnight for each date and then calc the delta
((MPM_Now=${Now[3]}*60+${Now[4]} ))
echo Now $MPM_Now ${Now[3]} ${Now[4]}
((MPM_TS=${TS[3]}*60+${TS[4]} ))
echo TS $MPM_TS ${TS[3]} ${TS[4]}
((MPM_delta=MPM_Now-MPM_TS))
echo delta = $MPM_delta

#
# Calc final answer
((minutes=minutes+MPM_delta))
echo And the answer is  that $timestamp was $minutes minutes ago
exit 0

And a sample run:
Code:
$ ./tsdate
Now = 2006 and 11 and 6 and 13 and 13
input = Nov and 3 and 11 and 4
TS = 2006 and 11 and 3 and 11 and 4
days = 3
minutes = 4320
Now 793 13 13
TS 664 11 4
delta = 129
And the answer is that Nov 3 11 04 was 4449 minutes ago

This is a little tricky, so I left the debug statements in so you can see what's going on.
# 3  
Old 11-06-2006
hi perdrabo

thanks a lot for your reply..

i have a problem.. the output of the script is as follows

Code:
Nov 3 11:04
Now = 2006 and 11 and 6 and 11 and 43
input = Nov and 3 and 11 and 4
TS = 2006 and 11 and 3 and 11 and 4
days = 3
minutes = 259200
Now 703 11 43
TS 664 11 4
delta = 39
And the answer is that Nov 3 11 04 was 259239 minutes ago

actually Nov 3 11 04 was like 5000 minutes ago, because there are only 1440 min in a day. i dont understand this part. can you plz explain..

also will it check for files with timestamp like this Nov 3 23 54

thanks a lot again!!
# 4  
Old 11-06-2006
oppps....
((minutes = days * 60 * 60 * 24))
is wrong. It should be

((minutes = days * 60 * 24))

There are 24*60 minutes in a day. 60*60*24 would be seconds. I will fix my original post.
# 5  
Old 11-06-2006
thanks perdrabo.. u really saved me.. have a couple of questions as well

will your script pickup timestamp values like this 11 04 23 09

what should i do to export minutes variable to other scripts..

i did export min=$minutes and did a echo $min in another script that references this script, but it didnt work..

thanks a lot for ur help again
# 6  
Old 11-06-2006
Yeah...it should work with a 24 hour clock. Also should work if you use a space instead of a colon to separate hours and minutes. Not sure which question you were asking.

This was just a sample script to show how to do the arithmetic. But if want to use it as a tool, remove all of the echo statements and at the end just do "echo $minutes". Then from another script do:
minutes=$(./tsdate)
You will need to to change it to send in a timestamp as a parameter. (Unless you really want that Nov 3 11:04 hard coded in.) To do that in my script do:
timestamp="$1"
And in another script do:
sometimestamp="Nov 3 11:04"
minutes=$(./tsdate "$sometimestamp")
# 7  
Old 11-06-2006
great perdrabo.. I am really grateful for your help. u made it happen in a matter of hours.. something that i thought is not possible at all..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Urgent for date increment

hi all, i would like to increment the date variable i am using for((i=20190731;i<=20190801;i++)) do done after 20190731 it should be 20190801 but this taking as 20190732,20190733.... kindly help me to solve this (3 Replies)
Discussion started by: prathaban
3 Replies

2. Shell Programming and Scripting

Date: invalid date trying to set Linux date in specific format

i try to set linux date & time in specific format but it keep giving me error Example : date "+%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" or date +"%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" keep giving me this error : date: invalid date ‘19-01-2017 00:05:01' Please use CODE tags... (7 Replies)
Discussion started by: umen
7 Replies

3. Shell Programming and Scripting

URGENT Reading a file and assessing the syntax shell script URGENT

I am trying to write a shell script which takes an input file as an arguement in the terminal e.g. bash shellscriptname.sh input.txt. I would like for the file to be read line by line each time checking if the .txt file contains certain words or letters(validating the syntax). If the line being... (1 Reply)
Discussion started by: Gurdza32
1 Replies

4. Shell Programming and Scripting

Converting a date to friday date and finding Min/Max date

Dear all, I have 2 questions. I have a file with many rows which has date of the format YYYYMMDD. 1. I need to change the date to that weeks friday date(Ex: 20120716(monday) to 20120720). Satuday/Sunday has to be changed to next week friday date too. 2. After converting the date to... (10 Replies)
Discussion started by: 2001.arun
10 Replies

5. Shell Programming and Scripting

Need to zip the files date wise --urgent Help needed

Hi all, this is my first post, i need to write a script to zip the files with datewise below are the log files. -rw------- 1 root sso 85316156 May 24 22:11 core_test_smservaz_104_104_1243217459_8896 -rw------- 1 root sso 90413304 May 25 22:12 core_test_smservaz_104_104_1243303895_20912... (4 Replies)
Discussion started by: lcschandu
4 Replies

6. Linux

date subtraction(URGENT)

Hi all, I need the date subtraction fuctionality using shell commands. For example: date1:Wed Apr 5 08:35:21 IST 2006 date2:Tue Apr 4 10:35:44 IST 2006 I need the date subtraction result like " 22 hours 23 seconds". Please guide me to complete this task. Can you please help me ASAP. ... (3 Replies)
Discussion started by: uday123
3 Replies

7. Shell Programming and Scripting

time arthemetic - really urgent

guys.. i currently use this logic to calculate date and time. export day=`date +%d` export month=`date +%m` export day_len=${#day} export mon_len=${#month} export hr_min=0000 if ] || ] then month=0$month day=0$day export time_search=$month$day$hr_min else export... (5 Replies)
Discussion started by: ragha81
5 Replies

8. UNIX for Advanced & Expert Users

URGENT,URGENT- Need help tape drive installation

Hi, I am trying to attach tape drive to sun V890 running Solaris 9 on it. I have installed HBA(qlogic) in slot 1 of 0-8 slots and booted the system. I do not see HBAin prtdiag output. The tape drive is not attached to HBA. The tape drive I am going to attach is Sony AIT3. 1.How can I make... (3 Replies)
Discussion started by: sriny
3 Replies

9. Shell Programming and Scripting

date problem -- URGENT

Hi All, When i run date command at unix prompt, i get the output in format Tue Aug 29 12:36:18 GMT 2006 when i write the shell script i should be able to export the date output with format as YYYY/MM/DD-HH:MM:SS into some variable(ex : dateop). and when i say echo ${dateop} i should... (9 Replies)
Discussion started by: rkrgarlapati
9 Replies
Login or Register to Ask a Question