?
Solved

Finding if the expiry date is within 1 year

Posted on 2013-06-24
5
Medium Priority
?
235 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 1500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Six Sigma Control Plans

718 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