You are not logged in.

#1 2015-11-25 02:01:19

Horizon_Brave
Operating System: Linux-Nettrix
Registered: 2015-10-18
Posts: 1,473

Comparing Two Dates

Hey guys,

I'm working on a script that will work something akin to an RSS feed. I have a directory set up and I'm trying to get the bash script to look at the date of the last downloaded (modified) file. Then compare that with today's date. If today's date is "bigger" I want it to wget to the server that stores the podcast that I want to download and see if there's a newer episode available. Probably a very trivial matter, but how can I compare two different dates?

Right now I have the dates in the form  of

date +%m%d%y   

so basically it looks like 

112415
So I end up with two of these values. One for today's current date and one for the last modified file that I want to compare it with. But the problem is I can't do a straight compare. What if one date is..

[[ 112415 -ge  123012 ]]

Clearly this is true, (2015 compared to 2012) but if run with a straight compare it see's that the 11 is smaller than then 12, so it results to false.

What would you guys use if you wish to compare two dates like this? Is there a better format to use that would make this easier?


"I have not failed, I have found 10,000 ways that will not work" -Edison

Offline

#2 2015-11-25 02:12:28

damo
....moderator....
Registered: 2015-08-20
Posts: 6,701

Re: Comparing Two Dates

Look at `man date` (Examples), and maybe convert your date/times to "seconds since epoch". Then it would be easy to compare them.


Be Excellent to Each Other...
The Bunsenlabs Lithium Desktop » Here
FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#3 2015-11-25 06:56:14

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,370
Website

Re: Comparing Two Dates

right, perhaps a good starting point
http://mywiki.wooledge.org/BashFAQ/102
http://mywiki.wooledge.org/BashFAQ/070

... epoch timestamps. If the epoch is January 1, 1970 at midnight UTC, then it's also called a "Unix timestamp" ...

p.s. for simple comparison  y m d  (20151230) should also work.

Last edited by brontosaurusrex (2015-11-25 07:05:00)

Offline

#4 2015-11-25 07:17:19

Head_on_a_Stick
Member
From: London
Registered: 2015-09-29
Posts: 8,759
Website

Re: Comparing Two Dates

brontosaurusrex wrote:

y m d  (20151230) should also work.

^ This, for any date -- that's why the ISO standard was chosen wink


“Et ignotas animum dimittit in artes.” — Ovid, Metamorphoses, VIII., 18.

Forum Rules   •   How to report a problem   •   Software that rocks

Offline

#5 2015-11-25 17:22:12

pingu
Member
Registered: 2015-09-29
Posts: 128

Re: Comparing Two Dates

date +%s
is for the timestamp


"Chuck Norris can compile syntax errors."

Offline

#6 2015-11-25 21:44:28

Horizon_Brave
Operating System: Linux-Nettrix
Registered: 2015-10-18
Posts: 1,473

Re: Comparing Two Dates

Thanks everyone! Hmm so looks like I have some options. I may go for the date +%s Epoch time method and compare the two times that way. Thanks folks! You'll make a shell scripting guru out of me yet...


"I have not failed, I have found 10,000 ways that will not work" -Edison

Offline

#7 2015-11-28 11:05:05

Head_on_a_Stick
Member
From: London
Registered: 2015-09-29
Posts: 8,759
Website

Re: Comparing Two Dates

/Moved to Basic Help & Support


“Et ignotas animum dimittit in artes.” — Ovid, Metamorphoses, VIII., 18.

Forum Rules   •   How to report a problem   •   Software that rocks

Offline

Board footer

Powered by FluxBB