Solved

Perl, a subroutine that takes in a date string of various format and return date string in format yyyy-mm-dd

Posted on 2010-09-12
6
303 Views
Last Modified: 2012-06-22
Need a subroutine that takes in a date string of various format and return date string in format yyyy-mm-dd.  If date is not a valid date then rerun 0;  I would prefer if you use any of the Perl date library.
0
Comment
Question by:rgbcof
  • 3
  • 3
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 33656015
use Date::Parse;
use POSIX;
sub yyyymmdd{
 my @F;
 return strftime "%Y-%m-%d",@F if @F=strptime shift;
 return 0;
}
0
 

Author Comment

by:rgbcof
ID: 33659520
Is there another way?  My system does not have Date::Parse, and I am not allowed to include the library in the system.
0
 
LVL 84

Expert Comment

by:ozo
ID: 33659535
If you want to write your own date parser, can you list all the various date formats that you want to convert?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:rgbcof
ID: 33659545
Acceptable input format:
01/02/2010
1/2/2010
01/02/10
1/2/10

Output format:
yyyy-mm-dd

Also, the date has to be a valid date (13/13/2010 would be invalid)
0
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 33659632
use POSIX;
sub yyyymmdd{
  return 0 unless my($m,$d,$y) = shift =~ m((\d+)/(\d+)/(\d+));
  $y += 2000 if $y < 100;
  return 0 unless (localtime mktime 0,0,12,$d,$m-1,$y-1900)[4] == $m-1;
  return sprintf"%04d-%02d-%02d",$y,$m,$d;
}
0
 

Author Closing Comment

by:rgbcof
ID: 33659693
Ozo, you are such a genius.  

Thank you so much.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 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

15 Experts available now in Live!

Get 1:1 Help Now