Solved

Finding if the expiry date is within 1 year

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PERL export multiple query results to a JSON file 1 173
Perl 101 11 81
How to search multiple patterms in a file with perl? 4 82
Awk Question 2 129
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

820 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