Solved

Finding if the expiry date is within 1 year

Posted on 2013-06-24
5
223 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
  • 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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