LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 04-05-2017, 09:33 AM   #1
slayer_1994
Member
 
Registered: Feb 2017
Posts: 38

Rep: Reputation: Disabled
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?
 
Old 04-05-2017, 09:35 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,622

Rep: Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610
try to use ": "$current_time"
 
Old 04-05-2017, 11:05 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 17,426
Blog Entries: 10

Rep: Reputation: 5248Reputation: 5248Reputation: 5248Reputation: 5248Reputation: 5248Reputation: 5248Reputation: 5248Reputation: 5248Reputation: 5248Reputation: 5248Reputation: 5248
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
Code:
date '+%s'
- 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.
 
Old 04-07-2017, 03:08 AM   #4
slayer_1994
Member
 
Registered: Feb 2017
Posts: 38

Original Poster
Rep: Reputation: Disabled
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
 
Old 04-07-2017, 05:55 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,611

Rep: Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169
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.
 
Old 04-07-2017, 07:24 AM   #6
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873
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.
Old 04-07-2017, 08:12 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,611

Rep: Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169
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.
Old 04-12-2017, 05:53 AM   #8
slayer_1994
Member
 
Registered: Feb 2017
Posts: 38

Original Poster
Rep: Reputation: Disabled
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
 
Old 04-12-2017, 06:11 AM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,611

Rep: Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169
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.
 
Old 04-12-2017, 06:12 AM   #10
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,622

Rep: Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610Reputation: 5610
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 $
 
Old 04-12-2017, 07:42 AM   #11
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873
Quote:
Originally Posted by Shadow_7 View Post
...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.
 
Old 04-12-2017, 10:34 AM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,611

Rep: Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169Reputation: 4169
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
 
  


Reply

Tags
difference, newbie, shell script, time, timestamp


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
Timestamps kingsfischer Linux Deepin 1 07-25-2015 04:18 PM
Backups and Timestamps shane25119 Linux - General 9 02-22-2009 09:49 PM
Grep timestamps tekmann33 Linux - Newbie 3 08-26-2008 12:52 PM
calculate time difference between 2 formatted timestamps nickleus Linux - General 3 05-12-2006 10:08 PM
Timestamps Config Programming 2 04-22-2002 02:07 AM

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

All times are GMT -5. The time now is 07:54 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