Tuesday, July 30, 2013

Kick Off, StringToCaseSelect & FindTimeDifference

Hi,

My first blog for Delphi Firemonkey Programming. It has been an year for me while producing FMX based apps so thought why not share the shareable part of my code for free and improve some snippets with your valuable input.

One reason being to piss the proprietary IP centric devs around and the other to help devs like me struggling with the framework and getting yelled at by our bosses in the process. :)

I am kinda low level programming freak so my methods may have seemingly unnecessary differences with anybody else code because I try to save as much CPU time as possible. I made a proprietary OS for Atmel Microcontrolelrs back in my university days, so the habits of nit picking tiniest bits of CPU cycle wastage kinda stuck. So if you all can suggest some tweaks, I will really appreciate it!




So to kick things off a function for new delphi devs from my utilities library;

Its a simple function to get time difference in any given unit of time (hours,minutes,seconds,milli-seconds):-

uses System, System.SysUtils, Windows; 

function  FindTimeDifference ( ANewDate,  AOldDate : TDateTime; ATimeUnit : String
               : DWORD;
var
 
TimeUnitInInt : Byte;
  Hours, Minutes, Seconds, MilliSeconds : WORD;

begin

  Result := 0;
  TimeUnitInInt := StringToCaseSelect ( ATimeUnit, ['h','m','s','ms'] );

  DecodeTime ( AOldDate, Hours, Minutes, Seconds, MilliSeconds );

  case
TimeUnitInInt of

    0 : Result := Hours;
    1 : Result := Minutes + ( Hours * 60 );
    2 : Result := Seconds + (( Minutes + ( Hours * 60 )) * 60 );
    3 : Result := MilliSeconds + (( Seconds + (( Minutes + ( Hours * 60 )) * 60 )) * 1000 );

  end;

  DecodeTime ( ANewDate, Hours, Minutes, Seconds, MilliSeconds );

  case
TimeUnitInInt of

    0 : Result := Hours - Result;
    1 : Result := Minutes + ( Hours * 60 ) - Result;
    2 : Result := Seconds + (( Minutes + ( Hours * 60 )) * 60 ) - Result;
    3 : Result := MilliSeconds + (( Seconds + (( Minutes + ( Hours * 60 )) * 60 )) * 1000 ) - 

         Result;

  end;

end;



Oh there is another custom function in there "StringToCaseSelect"; Here you go:- :)


function  StringToCaseSelect ( ASelector : String; ACaseList: array of String ): Integer;
var
  Counter : integer;

begin

  Result := -1;

  for Counter:= 0 to Length ( ACaseList ) - 1 do
  begin

    if CompareText ( ASelector, ACaseList [Counter] ) = 0 then
    begin

      Result := Counter;
      Break;

    end;

  end;

end;

No comments:

Post a Comment