Solved

Finding if the expiry date is within 1 year

Posted on 2013-06-24
5
229 Views
Last Modified: 2013-07-30
I have a script which will fetch values like this
Valid from: Fri Dec 28 06:22:27 UTC 2012 until: Mon Dec 26 06:22:27 UTC 2022

As we can see this is valid until Mon Dec 26 06:22:27 UTC 2022

So now my script will have to do do the following
Check if the untill date is within 1 year from the current date.

Example:

Valid from: Fri Dec 28 06:22:27 UTC 2012 until: Mon Dec 26 06:22:27 UTC 2022

If the current date extracted is
Mon Jun 24 14:29:54 UTC 2013

Now we see that Until has expiry date upto 2022. So it's fine.

Another Example:
Valid from: Fri Dec 28 06:22:27 UTC 2012 until: Mon Dec 26 06:22:27 UTC 2013

If the current date extracted is
Mon Jun 24 14:29:54 UTC 2013

Now we see that expiry date is less than one year from the current date and has to take an action.

If i could use standard modules within perl5.8.8 that will help.
0
Comment
Question by:gurudasha
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 26

Expert Comment

by:wilcoxon
ID: 39272349
You could write your own but by far the best thing to do is to use Data::Manip, Date::Calc, or DateTime.  DateTime is more-or-less becoming the defacto standard but is overkill for what you want to do (and I think has more dependencies).  Date::Manip can parse almost any format date correctly but is a little slower (but doesn't have the dependencies of DateTime).  Date::Calc is very fast but doesn't parse as many date string formats (but is generally what I use if I don't need other features of DateTime).  Looking at MetaCPAN, it looks like Date::Manip has the fewest non-core dependencies (one Test module that likely isn't needed to actually run the module - just to test it during build).
use Date::Manip;
my $cutoff = DateCalc("today", "+ 1 year");
my $str = 'Mon Dec 26 06:22:27 UTC 2013';
if (Date_Cmp($str, $cutoff) > 0) {
    # not ok - do something
}

Open in new window

0
 

Author Comment

by:gurudasha
ID: 39274226
Thank you wilcoxon

The condition if (Date_Cmp($str, $cutoff) > 0)  is not working.
It seems to be returning -1 for all values.
0
 
LVL 84

Expert Comment

by:ozo
ID: 39274310
what are $str and $cutoff when it returns -1?
You may be getting an error because Dec 26 06:22:27 UTC 2013 is not  Monday
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 39275384
Ozo is correct.  I just did some quick testing.  ParseDate (called from Date_Cmp) apparently silently fails on 'Mon Dec 26 06:22:27 UTC 2013' (returning undef).  It works fine if you remove 'Mon ' from the front of the string.

A simple way to test it is:
my $dt = DateParse($str);
print $dt, "\n";

Open in new window


And I reversed the comparison - it should be:
use Date::Manip;
my $cutoff = DateCalc("today", "+ 1 year");
my $str = 'Mon Dec 26 06:22:27 UTC 2013';
if (Date_Cmp($str, $cutoff) < 0) {
    # not ok - do something
}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 39277193
It works fine if you remove 'Mon ' from the front of the string.
Replacing  'Mon Dec 26 06:22:27 UTC 2013'  with 'Thu Dec 26 06:22:27 UTC 2013' should also work
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question