Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
346 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 1000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Six Sigma Control Plans

722 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