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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
04-05-2017, 09:33 AM
|
#1
|
Member
Registered: Feb 2017
Posts: 38
Rep: 
|
Difference between timestamps
Hi guys
Tried executing this code but didn't seem to like it not 100% sure why
Code:
current_time=`date`
last_defect_file_recieved=`date -d @1489662376`
(stat --printf '%Y' $current_time; printf ' - '; stat --printf '%Y\n' $last_defect_file_recieved) | bc -lq
When I go to execute the script I get:
Quote:
[rp1cem@wycvlapph036 self_monitoring]$ ./test.sh
stat: cannot stat `Wed': No such file or directory
stat: cannot stat `Apr': No such file or directory
stat: cannot stat `5': No such file or directory
stat: cannot stat `15:33:15': No such file or directory
stat: cannot stat `BST': No such file or directory
stat: cannot stat `2017': No such file or directory
stat: cannot stat `Thu': No such file or directory
stat: cannot stat `Mar': No such file or directory
stat: cannot stat `16': No such file or directory
stat: cannot stat `11:06:16': No such file or directory
stat: cannot stat `GMT': No such file or directory
stat: cannot stat `2017': No such file or directory
(standard_in) 1: syntax error
|
Any particular reason for this?
|
|
|
04-05-2017, 09:35 AM
|
#2
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,644
|
try to use ": "$current_time"
|
|
|
04-05-2017, 11:05 AM
|
#3
|
LQ Addict
Registered: Dec 2013
Posts: 19,872
|
not sure which shell you are using, but on my bash 4.4.12:
Code:
$> printf '%Y' "$(date)"
bash: printf: `Y': invalid format character
also why don't you use - much easier to do calculations with, and presumably 1489662376 already is in that format.
Last edited by ondoho; 04-05-2017 at 11:07 AM.
|
|
|
04-07-2017, 03:08 AM
|
#4
|
Member
Registered: Feb 2017
Posts: 38
Original Poster
Rep: 
|
Hi ondoho
I understand what you are trying to say but 1489662376 is also the name of the file, so if the file executes I presume this number will change again?
As you can see below now the file has changed its name as the execution date changed:
Quote:
4/07/17 09:05:45.864 AM BST [INFO] [TimPollThreadPool.Thread6] [Manager.com.timestock.tess.services.tim.TimIo] File 'Wynyard_MTP_Primary-defect-14915523440000159519.xml' read, length=37791
|
And if I execute this on the timeline:
Quote:
[rp1cem@wycvlapph036 logs]$ date -d @1491552344
Fri Apr 7 09:05:44 BST 2017
|
So is there anyway of doing this?
Cheers
|
|
|
04-07-2017, 05:55 AM
|
#5
|
Moderator
Registered: Aug 2002
Posts: 26,911
|
In this case printf is an option of the stat command and not bash.
If you want to find the difference between timestamps they need to be in seconds.
The difference between now and Thu Mar 16 06:06:16 2017 in seconds.
Code:
echo "$(date '+%s')-1489662376" | bc -lq
1900076
The difference between now and Thu Mar 16 06:06:16 2017 in days.
Code:
echo "scale=0; ($(date '+%s')-1489662376)/3600/24" | bc -lq
21
Last edited by michaelk; 04-07-2017 at 05:57 AM.
|
|
|
04-07-2017, 07:24 AM
|
#6
|
Senior Member
Registered: Feb 2003
Distribution: debian
Posts: 4,137
|
Code:
#!/bin/bash
CURRENTDATE=$(date +%s)
OTHERDATE=$(date -d @1489662376 +%s); # redundant to use +%s on the epoch date
DIFFDATE=$(($CURRENTDATE-$OTHERDATE))
echo -n "Seconds: "$DIFFDATE" - "
date -d @$DIFFDATE +%Y%m%d-%H:%M:%S
exit 0
No need for printf or bc. Bash math $(( )) and date can replace most of that. Although you'd probably want to -1970 from the year (%Y) for the "difference". And $( ) replaces ` ` with an advantage of being more readable and nest-able. The default output of date is what makes up that list of cannot stat errors. Although your (stat ; stat) part probably needs a leading $ for $(stat ; stat), not that that would give desired results.
$ date +"%a %b %e %R:%S %Z %Y"
$ date
Last edited by Shadow_7; 04-07-2017 at 07:31 AM.
|
|
1 members found this post helpful.
|
04-07-2017, 08:12 AM
|
#7
|
Moderator
Registered: Aug 2002
Posts: 26,911
|
Code:
date -d @$DIFFDATE +%Y%m%d-%H:%M:%S
The output is the absolute date for the number of seconds since the epoch and not the relative difference between timestamps although the number of days should be correct if using %j.
It depends on what the OP is trying to accomplish.
Last edited by michaelk; 04-07-2017 at 08:16 AM.
|
|
1 members found this post helpful.
|
04-12-2017, 05:53 AM
|
#8
|
Member
Registered: Feb 2017
Posts: 38
Original Poster
Rep: 
|
Hi Guys
Tried using the above command & it helped a lot.
I used man date to get a better understanding and saw their were other options
I used "+%F" so a full date for example and my return was the following:
[rp1cem@wycvlapph036 self_monitoring]$ ./test.sh
Seconds: 2331840 - 1970-01-28
Any particular reason why it says 1970 when the seconds is 26 days?
Quote:
CURRENTDATE=$(date +%s)
OTHERDATE=$(date -d @1489662376 +%s); # redundant to use +%s on the epoch date
DIFFDATE=$(($CURRENTDATE-$OTHERDATE))
echo -n "Seconds: "$DIFFDATE" - "
date -d @$DIFFDATE +%F
exit 0
~
|
Cheers
Alex
|
|
|
04-12-2017, 06:11 AM
|
#9
|
Moderator
Registered: Aug 2002
Posts: 26,911
|
In a nutshell time is basically a counter with 0 being 00:00 1 January 1970 UTC. This is known as epoch time which can differ between operating systems and applications that use date functions.
date -d @$DIFFDATE +%F converts the difference in seconds to an absolute date/time with reference to the epoch time which is why the year is 1970.
Last edited by michaelk; 04-12-2017 at 06:46 AM.
|
|
|
04-12-2017, 06:12 AM
|
#10
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,644
|
you can put set -xv at the beginning of your script to see what's happening (that will not solve anything, just will print a lot of debug information)
DIFFDATE=$(($CURRENTDATE-$OTHERDATE))
you can remove those $
|
|
|
04-12-2017, 07:42 AM
|
#11
|
Senior Member
Registered: Feb 2003
Distribution: debian
Posts: 4,137
|
Quote:
Originally Posted by Shadow_7
...Although you'd probably want to -1970 from the year (%Y) for the "difference"...
|
The epoch date is 1970/01/01 at 00:00:00 when it's value is 0. Or 1969/12/31 at 23:59:59 for a lot of databases that store unset dates as -1. Basically using the epoch value for date math returns the deference relative to the epoch date for value 0.
Last edited by Shadow_7; 04-12-2017 at 07:52 AM.
|
|
|
04-12-2017, 10:34 AM
|
#12
|
Moderator
Registered: Aug 2002
Posts: 26,911
|
Quote:
...Although you'd probably want to -1970 from the year (%Y) for the "difference"...
|
By using %j option the date command outputs the day of the year (i.e 1-365) which is easier then trying to subtract 1970 or when the difference exceeds 31 days
|
|
|
All times are GMT -5. The time now is 09:21 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|