LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to find the time difference in min (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-find-the-time-difference-in-min-4175417783/)

pradeepdee6 07-20-2012 09:09 AM

How to find the time difference in min
 
Hi,

I want to get the difference of two time stamps in min.

Ex:
a= Jul 20 08:07:36
b= Jul 20 08:09:09

I want the difference in minutes.

Please help me to get through it.

Thanks in Advance.. :-)

TB0ne 07-20-2012 09:35 AM

Quote:

Originally Posted by pradeepdee6 (Post 4733675)
Hi,
I want to get the difference of two time stamps in min.

Ex:
a= Jul 20 08:07:36
b= Jul 20 08:09:09

I want the difference in minutes. Please help me to get through it.
Thanks in Advance.. :-)

You don't provide any details. Help you through it HOW? What are you trying to do this in (bash? perl? python?) What's your goal?? And what have you done/tried so far??

Based on your last thread, you didn't put any effort into your problem, but asked others to solve it for you...and this sounds very much like homework.

pixellany 07-20-2012 09:45 AM

Quote:

and this sounds very much like homework.
+1

pradeep*;
You have more replies in your other thread---it is good practice to follow thru on existing threads before starting new ones.
Quote:

I want the difference in minutes.
1.55 minutes

lithos 07-20-2012 01:14 PM

Quote:

Originally Posted by pixellany (Post 4733702)
+1

pradeep*;
You have more replies in your other thread---it is good practice to follow thru on existing threads before starting new ones.
1.55 minutes

Well, I don't know my math if it is good, but this seems wrong to me !?

Isn't it 1:33 minutes ?

so I wrote a script:
Code:

#!/bin/sh
a="Jul 20 08:07:36"
b="Jul 20 08:09:09"

# convert date/time to Epoch
epa=$(date -d"$a" +%s)
epb=$(date -d"$b" +%s)

#do the math
tdiff=$(($epb-$epa))
#convert to human date/time format
th=$(date -d "1970-01-01 $tdiff sec" +"%H:%M:%S")

echo "Time difference (Hours:Min:Sec): $th  / ($tdiff seconds)"


### OUTPUT
Time difference (Hours:Min:Sec): 00:01:33  / (93 seconds)

confirmed time difference is: 93 seconds
so that is 1 minute (60 sec) and 33 seconds to me.

I hope it helps.

good luck

pixellany 07-20-2012 01:30 PM

01:33 = 1 minute, 33 seconds = 1.55 minutes

seriously, as long as we are doing the OP's homework, I wonder if there is not a simpler solution...

lithos 07-20-2012 01:35 PM

Oh,
you mean 1.55 where a minute is in 100's not 60 seconds (.50 = 30 seconds, 100 = 60sec/1min).

Now I got it. I'm sorry...

TB0ne 07-20-2012 03:31 PM

Quote:

Originally Posted by pixellany (Post 4733896)
01:33 = 1 minute, 33 seconds = 1.55 minutes

seriously, as long as we are doing the OP's homework, I wonder if there is not a simpler solution...

I think the OP should post the simpler solution, since we've now done TWO of his homework assignments for him.

pradeepdee6 07-23-2012 04:14 AM

Hi Everyone. Thank u so much for your answers
 
Hi,

Im new here in the forum. So learning form you all seniors here.

Actual Issue is: I need to get the list of files in a ftp process and change the permissions of those files alone.
Problem i was facing was, sftp should perform "mget" method to a common directory. And search string was like "mget ABC*". So after ftping If i grep for the files with the string ABC* it may even select other preexixting files which would match.

I tried using "sed" command,(In other post) it was working but not accurate { I used ls -lrt before and after FTP process and was gathering the difference in lists}

So My Idea was to get the time difference and to list the files FTPed with in that time limit, As i had find command {find . -mmin -$min}
So thought of applying the time difference.

Im working in "bash"

Ex of My Code format:
DATE1=date

FTP process

DATE2=date

TIME_DIFF=(DATE1-DATE2) # which i wanted in min to use find command

find . -mmin -$TIME_DIFF # find . -newermt "2012-07-23 13:42" doesnt work in bash :-(

Really sorry if im not clear. I ll try to Improve myself.

Thank you so much in Advance.

pixellany 07-23-2012 06:11 AM

In BASH, I have not seen a solution simpler than what lithos posted.

In Python, there are some useful routines in the "datetime" module.

pradeepdee6 07-23-2012 07:01 AM

Im Close to the solution, Help me
 
Hi everyone.. Thanku very much for all the posts.

I just found one line code for my problem.

In previous post i was trying to get the difference of time and list the files.
Now that can be achieved by an ready made option of "find" command.

Ex:

find . -newer "File Name" # where File Name should contain the Time from which i should list.
# This can be achieved By creating a file with date command in it.

But My problem is: Should not create and extra file as we are most concerned about the performance and code optimization.

So I can not create a new file "File Name"

Can anyone help me to solution.

TB0ne 07-23-2012 09:43 AM

Quote:

Originally Posted by pradeepdee6 (Post 4735744)
Hi everyone.. Thanku very much for all the posts.
I just found one line code for my problem. In previous post i was trying to get the difference of time and list the files. Now that can be achieved by an ready made option of "find" command.

Ex:
find . -newer "File Name" # where File Name should contain the Time from which i should list.
# This can be achieved By creating a file with date command in it.

But My problem is: Should not create and extra file as we are most concerned about the performance and code optimization. So I can not create a new file "File Name"
Can anyone help me to solution.

Again, post what you've written/tried so far. Your posts thus far have all seemed like homework questions, and you've not provided any evidence that you've tried to do this, or shown what error(s) you've got. As with any programming task, you can either create a file, or put data into a variable/array for later use. There is abundant documentation on how to do this that you can find with a brief Google search.

pradeepdee6 07-23-2012 11:04 AM

Hi TB0ne

These are few things i have worked out and gives required answer for my local setup.

Quote:

vari=`ls -1t | head -1`
echo $vari # Here I ll be getting latest modified file name

FTP PART

MYFILES=`ls -lt | sed -n "1,/${vari}/p" | awk {'print $9'} | grep -v $vari `
But i need to work it out with production where large number of files get created and get deleted simultaniously. So it wont be efficient and dependable.

Then tried with capturing the log for FTP part. Faced an issue for creating an extra file.

Quote:

FTP Part > logfile # will capture the files FTPed

MYFILES=`cat logfile | grep -i "Fetching" | awk {'print $4'}`
Then worked with sed command as in other post.

And now im trying with the above find command.

Now Im trying to use the batch file which is already being used in the script. So that i can feed input to the find command.

Quote:

BATCHFILE=/tmp/mftget.$CONSUMER.$PRODUCER.$TIMESTAMP.$$.tmp
echo "cd /$PRODUCER/Inbox
$GETFILES
bye" > $BATCHFILE # modifying Batch file time stamp

FTP Part

find . -newer $BATCHFILE # Using the Batch file as latest modified file
Thanks for your help and guidance.

TB0ne 07-23-2012 01:01 PM

Quote:

Originally Posted by pradeepdee6 (Post 4735940)
Hi TB0ne
These are few things i have worked out and gives required answer for my local setup.

But i need to work it out with production where large number of files get created and get deleted simultaniously. So it wont be efficient and dependable. Then tried with capturing the log for FTP part. Faced an issue for creating an extra file.

Then worked with sed command as in other post. And now im trying with the above find command. Now Im trying to use the batch file which is already being used in the script. So that i can feed input to the find command.

Ok, so use the Unix epoch date in a variable in your script. That is VERY fine grained, and the chances of two files being created in the EXACT same thousandth of a second is very small. That'll give you a unique time stamp. Then convert that into a 'regular' date, and save your file. A simple check would tell you if that file already exists, and you can append something to the date, like a "(01)", so you can have a file called "2012072073258(01).txt", and "20120720073258(02).txt"...two files same date/time/second, with SLIGHTLY different names.


All times are GMT -5. The time now is 09:56 PM.