LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-20-2012, 08:09 AM   #1
pradeepdee6
Member
 
Registered: Jul 2012
Posts: 30

Rep: Reputation: Disabled
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.. :-)
 
Old 07-20-2012, 08:35 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,602

Rep: Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139
Quote:
Originally Posted by pradeepdee6 View Post
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.
 
Old 07-20-2012, 08:45 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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
 
1 members found this post helpful.
Old 07-20-2012, 12:14 PM   #4
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Quote:
Originally Posted by pixellany View Post
+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

Last edited by lithos; 07-20-2012 at 12:17 PM.
 
Old 07-20-2012, 12:30 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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...
 
Old 07-20-2012, 12:35 PM   #6
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
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...
 
Old 07-20-2012, 02:31 PM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,602

Rep: Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139
Quote:
Originally Posted by pixellany View Post
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.
 
Old 07-23-2012, 03:14 AM   #8
pradeepdee6
Member
 
Registered: Jul 2012
Posts: 30

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-23-2012, 05:11 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
In BASH, I have not seen a solution simpler than what lithos posted.

In Python, there are some useful routines in the "datetime" module.
 
Old 07-23-2012, 06:01 AM   #10
pradeepdee6
Member
 
Registered: Jul 2012
Posts: 30

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-23-2012, 08:43 AM   #11
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,602

Rep: Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139
Quote:
Originally Posted by pradeepdee6 View Post
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.
 
Old 07-23-2012, 10:04 AM   #12
pradeepdee6
Member
 
Registered: Jul 2012
Posts: 30

Original Poster
Rep: Reputation: Disabled
Smile

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.

Last edited by pradeepdee6; 07-23-2012 at 10:09 AM.
 
Old 07-23-2012, 12:01 PM   #13
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,602

Rep: Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139Reputation: 8139
Quote:
Originally Posted by pradeepdee6 View Post
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
waiting for device sdb8 to appear (time out 1 min) Johng Mandriva 3 08-07-2010 11:49 PM
shell script to find the difference betwwn two file and place the difference to other kittunot4u Linux - General 3 07-19-2010 04:26 AM
Time skew of 580 min when I set my time zone in Gentoo scooter2 Linux - Newbie 1 03-05-2009 10:27 AM
2 min booting time with raid, WHY??? ekj Linux - General 3 04-17-2007 11:38 AM
write a command whose modification time is changed in min suchi_s Programming 1 09-08-2004 09:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration