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
329 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

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 …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…

809 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