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
320 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

813 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

12 Experts available now in Live!

Get 1:1 Help Now