Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Comparing two dates without installing any modules

by G Nagasri Varma (Novice)
on Dec 11, 2015 at 08:25 UTC ( [id://1149987] : perlquestion . print w/replies, xml ) Need Help??

G Nagasri Varma has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Comparing two dates without installing any modules
by choroba (Cardinal) on Dec 11, 2015 at 09:48 UTC
    Time::Piece is core from Perl 5.10.

    Moreover, you can use the standard string comparison for dates in the YYYYMMDD format.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Comparing two dates without installing any modules
by Ratazong (Monsignor) on Dec 11, 2015 at 09:10 UTC

    Hi

    An easy way would be to transform the dates into numbers, e.g. year*10000 + month*100 + day. Then you could just compare them numericaly (with >, <) to check if a date is in a range.

    HTH, Rata

Re: Comparing two dates without installing any modules
by ctilmes (Vicar) on Dec 11, 2015 at 13:58 UTC
    BTW, date/time comparisons are yet another great reason to standardize on ISO 8601.
Re: Comparing two dates without installing any modules
by BillKSmith (Monsignor) on Dec 11, 2015 at 14:47 UTC
    I consider these comments slightly off-topic, but highly relevant. Three types of error are common in dates input by users. ('.' or '-' rather than '/'. 2-digit rather than 4-digit years. and fields out of order) Use of such dates in a simple compare can lead to wrong results with no indication of error. Testing for the first two is so easy that there is simply no reason not to do it. It is not always possible to detect the third. The best that you can do is to verify that the date is valid and in a range that is "reasonable" for your application. Without modules, this may be more trouble than it is worth. A possible compromise is to verify that the month is less than 13 and the day is less than 32. This would detect about half of the errors and never reject a valid date.
    Bill

      One could also let the user specify which date format they are using. It may even have field escapes to build a format like the date(1) command. Yet another option is to allow for year, month, and day arguments (-fromday -frommonth -fromyear -tomonth -today -toyear). One could also document ISO 8601 dates are taken by the program and reject anything that isn't one. There might be a Unix epoch timestamp option.

      I find it odd that you would call using '-' as a separator an error. ISO 8601 demands it. The standard also works with week numbers and day-of-year numbers which can be quite handy sometimes. When writing new software which doesn't have to deal with legacy formats, I generally try to use a standard. If you're going to force a particular format, don't use one that encourages localization nightmares.

        Good point! Who can argue with standardization? (Except possibly the customer) Validation is still required to prevent common errors from propagating into the application.
        Bill
Re: Comparing two dates without installing any modules
by ambrus (Abbot) on Dec 12, 2015 at 13:51 UTC

    You could at least tell us whether these dates are in day/month/year or month/day/year format.

    Let me guess. You're trying to do `dir` on a windows system and it depends on the locale.

Re: Comparing two dates without installing any modules
by Anonymous Monk on Dec 11, 2015 at 08:28 UTC

    I cannot install any modules.

    Then you must become smarter :)(learn to search)

      I have to run the script on a server, and i donot have admin access to install modules.