• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

Converting LongTimeFormat to another LongTimeFormat.

Hi!

    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.

Chad
0
Chadak
Asked:
Chadak
  • 3
  • 2
1 Solution
 
kretzschmarCommented:
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 ;-)
0
 
ChadakAuthor Commented:
This is the sample string that i have, maybe there is other format but i don't know.

1:31:35 PM
13:32:22
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

Chad
0
 
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;
  begin
    for i:= low(pmStrings) to High(pmStrings) do
      if s = pmStrings[i] then
      begin
        result:= true;
        Exit;
      end;
    //if still not found, check for amStrings
    for i:= low(amStrings) to High(amStrings) do
      if s = amStrings[i] then
      begin
        result:= false;
        Exit;
      end;

    //if still not found report error
    Raise exception.Create('An unknown am/pm qualifier was detected: '+s);
  end;
var Hr, Min, Sec, p1,p2: word;
begin
  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);
end;

Regards
Pierre
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Pierre CorneliusCommented:
BTW:
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"
0
 
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"

Chad
0
 
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.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now