Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then 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.
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.
Generally, people here are not going to do your homework for you, srvan. But let me point you in the right direction.
First off, your question is a bit incomplete. "Compare" generally means that you're asking which of the two is larger. If that's all there is, the "logic" says, "look at the two strings, determine that they are dates, notice that the first is larger than the second and report your findings." You're done.
Oh? You wanted a program or a script to do all that? Interesting!
I would write a program or script to check the strings and first verify that they match the date format you require. Because dates can be formatted in a very large multitude of ways; such as mm/dd/yy or mm/dd/yyyy and things like the seconds could be the least significant value versus milli-seconds in your example. Also AM/PM versus 24-hour representation.
Therefore, first start by checking the strings to verify that they are valid date formats for what you expect. Likely part of that will be conversion of some of the numbers into numerical values versus strings as recognized by your program or script. But once you have validated that both are properly formatted dates, then write the next part which would be a function to compare: start with year, then month, then day; and verify also that the day is acceptable for instance there is no Feb 31st, ever. You can determine greater/lesser first via the date, but if the dates match, then you compare hours, minutes, seconds, and milli-seconds.
Write a program or script and start this effort. Once you've done something, and if you have problems accomplishing parts of it, post what you do have and then forum responders will likely offer suggestions.
The LQ policies are not that help won't be given for homework assignments, if that is what this is, but rather that you should make your own attempt first at solving your problem. And you should be truthful as to whether or not it is an assignment. You've been given suggestions how to approach the problem here. Start something and ask follow-up questions when you can post some more details.
Regarding being in a "hurry" to post. Slow down and review what you write before you hit submit. You can also edit things to be more clear about them.
Which language are you intending to write your script in? Many will have built-in date parsing that will likely work better than using the 'date' command.
Yes, and I was being a bit flipant too. Thanks for not taking offence.
You said:
Quote:
My question is: How to recognize the two strings as dates? Once it is done I can compare two of them.
And really, that depends entirely on the language you're using.
Were you trying to use the date command in a bash shell? That will only print or set the system time. You probably don't want to do that.
In C++, C, Perl, Javascript or that ancient, dead language I learned 40 years ago, Fortran, you'd either input the string (and then break the string up to get the separate parts) or assign the values to variables, validate them as being in the expected range, and then compare them largest to smallest (that is, compare the years first, then the months, then day, hr. min., sec.fractionsofseconds) and stop when you know when is larger.
Hope I'm not speaking down to you again. That's just a basic algorithm for this sort of thing. The rest of the details really do depend on the language.
Last edited by jbuckley2004; 09-03-2014 at 10:26 PM.
$ date --date="Feb 23 2015 12:00:00:000AM"
date: invalid date `Feb 23 2015 12:00:00:000AM'
Look at the message, then the manpage, then what format time should be in (hint - where's the decimal point ???).
Quote:
Were you trying to use the date command in a bash shell? That will only print or set the system time. You probably don't want to do that.
Why not pray tell ...
The date command will reformat datetimes quite nicely - even into seconds since epoch ... maybe the OP can take that as another hint.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.