See Also Using Patterns to Match Dates or Times

To rewrit e t im es in 12- hour form at wit h AM and PM suffixes int o 24- hour form at , you can do som et hing like t his: if val =~ \d{1,2}:\d{2}?::\d{2}?\sAM|PM?i { my hour, min, sec = 1, 2, 3; supply missing seconds sec = 00 unless defined sec; convert 0 .. 11 - 12 .. 23 for PM times hour += 12 if defined 4 uc 4 eq PM; val = hour:min:sec; } The t im e part s are placed int o 1 , 2 , and 3 , w it h 3 set t o undef if t he seconds part is m issing. The suffix goes int o 4 if it s present . I f t he suffix is AM or m issing undef , t he value is int erpret ed as an AM t im e. I f t he suffix is PM , t he value is int erpret ed as a PM t im e.

10.26.4 See Also

This sect ion is j ust t he beginning of what you can do when processing dat es for dat a t ransfer purposes. Dat e and t im e t est ing and conversion can be highly idiosyncrat ic, and t he sheer num ber of issues t o consider is m ind- boggling: • What is t he basic dat e form at ? Dat es com e in several com m on st yles, such as I SO CCYY-MM-DD , U.S. MM-DD-YY , and Brit ish DD-MM-YY form at s. And t hese are j ust som e of t he m ore st andard form at s. Many m ore are possible. For exam ple, a dat afile m ay cont ain dat es writ t en as June 17, 1959 or as 17 Jun 59 . • Are t railing t im es allowed on dat es, or perhaps required? When t im es are expect ed, is t he full t im e required, or j ust t he hour and m inut e? • Do you allow values like now or today ? • Are dat e part s required t o be delim it ed by a cert ain charact er, such as - or , or are ot her delim it ers allowed? • Are dat e part s required t o have a specific num ber of digit s? Or are leading zeros on m ont h and year values allowed t o be m issing? • Are m ont hs writ t en num erically, or are t hey represent ed as m ont h nam es like January or Jan ? • Are t wo- digit year values allowed? Should t hey be convert ed t o have four digit s? I f so, what is t he conversion rule? What is t he t ransit ion point wit hin t he range 00 t o 99 at which values change from one cent ury t o anot her? • Should dat e part s be checked t o ensure t heir validit y? Pat t erns can recognize st rings t hat look like dat es or t im es, but while t heyre ext rem ely useful for det ect ing m alform ed values, t hey m ay not be sufficient . A value like 1947-15-99 m ay m at ch a pat t ern but isnt a legal dat e. Pat t ern t est ing is t hus m ost useful in conj unct ion w it h range checks on t he individual part s of t he dat e. The prevalence of t hese issues in dat a t ransfer problem s m eans t hat youll probably end up writ ing som e of your own validat ors on occasion t o handle very specific dat e form at s. Lat er sect ions of t his chapt er can provide addit ional assist ance. For exam ple, Recipe 10.30 covers conversion of t wo- digit year values t o four-digit form , and Recipe 10.31 discusses how t o perform validit y checking on com ponent s of dat e or t im e values.

10.27 Using Patterns to Match Email Addresses and URLs