Solved

Finding if the expiry date is within 1 year

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now