Converting LongTimeFormat to another LongTimeFormat.


    I need a function that would convert any LongTimeFormat time to the current LongTimeFormat time.

The problem is i am reading values from some files, and when i read the time(it's written in the file ), it was never saved in the same time format, ( i know bad programming, not my fault just fixing it ), so now i need to save this elsewhere, but how can i convert all those format. I've checked the file i know that i have at least 4 different time format, and the best would be to handle all case because i don't know what where all the format used.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

if you use d7,
then there is an overloaded version of the strtodate or strtotime or strtodatetime-functions,
you could use

maybe you should show your time-versions

meikl ;-)
ChadakAuthor Commented:
This is the sample string that i have, maybe there is other format but i don't know.

1:31:35 PM
01:32:34 p.m.
01:33:17 nm (did a bit of research on that one, seems to be the afrikaans regional setting)

one of my concerns is, i know of those format, is there other format that could exist... is there a way to do some kind of universal function that could convert them all??

i use D6

Pierre CorneliusCommented:
This works on the 4 examples you gave above. You should easily be able to modify it to suite any other formats you might come across.

function GetAsTime(s: string): TDateTime;
const pmStrings : array[0..3] of string = ('PM','p.m.','nm','n.m');
      amStrings : array[0..3] of string = ('am','a.m.','vm','v.m');
  function InAfternoon(s: string):boolean;
  var i: integer;
    for i:= low(pmStrings) to High(pmStrings) do
      if s = pmStrings[i] then
        result:= true;
    //if still not found, check for amStrings
    for i:= low(amStrings) to High(amStrings) do
      if s = amStrings[i] then
        result:= false;

    //if still not found report error
    Raise exception.Create('An unknown am/pm qualifier was detected: '+s);
var Hr, Min, Sec, p1,p2: word;
  p1:= Pos(':',s);
  Hr := StrToInt(LeftStr(s, p1-1));
  p2:= PosEx(':',s,p1+1);
  Min:= StrToInt(Copy(s,p1+1,p2-p1-1));
  Sec:= StrToInt(copy(s,p2+1,2));
  p2:= p2+2;

  //check for am/pm qualifiers
  if Hr <= 12 then //only if HR is less than 12 else we know it is in afternoon
    if Length(s) > p2 then //is there a qualifier, otherwise assume morning
      if InAfternoon(trim(RightStr(s, Length(s)-p2)))
      then Hr:= Hr+12;

  result:= EncodeTime(Hr, Min, Sec, 0);


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Pierre CorneliusCommented:
You're right about the Afrikaans.

nm stands for "na middag" which directly translates to "after midday"
vm stands for "voor middag" which directly translates to "before midday"
ChadakAuthor Commented:
Thx for your input Pierre!

   I had come to a solution close to yours. The only exception was that my constant that define the AM/PM qualifier where all un uppercase, and after the string compare would be "if uppercase(s) = amStrings[i] then"

ChadakAuthor Commented:
Also to determine if a time qualifier was used, i was looking if a 'M' existed in the string. iIt seems to be a valid rule for that check.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.