unit Rsc.Vcl.Utils;

interface

uses
    Winapi.Windows
  , Winapi.Messages

  , System.SysUtils
  , System.Variants
  , System.Classes
  , System.MaskUtils

  , Vcl.Graphics
  , Vcl.Controls
  , Vcl.Forms
  , Vcl.Dialogs
  , Vcl.Buttons
  , Vcl.StdCtrls
  , Vcl.ExtCtrls


  ;


type
  TCaractere = set of Char;


  function SomenteNumero(Num  : string) : string;
  function FormataTelefoneBR(nNum : string) : string;
  function FormatTelefoneRemoveNove(nNumCelular : string):  string;
  
  function DiferHora(Inicio,	Fim : String):String;
  
  function SaudacaoPorHora: string;
  
  function GetStrNumber(const S: string): string;

 /// <summary> Função para ser usada no KeyPress onde se permitir digitar somente numeros
 /// </summary>
 /// <param name="Key">char a ser verificada
 /// </param>
 /// <returns>Retorna um <c>Char</c> contendo o mesmo valor passado caso verdadeiro e  um
 /// <c>Char</c> contendo #0 caso não seja um char válio
 /// </returns>
  function SoNum(Key: Char; PermitirKey: TCaractere = [#0]): Char;


implementation

{---------Funções Auxiliares----------------}

function SaudacaoPorHora: string;
var
  HoraAtual:ttime;
begin
  HoraAtual:=StrToTIme(TimeToStr(Time));

  If TimeToStr(HoraAtual) >=  '00:00:00' Then
    Result  :=  '.      ..:: Bom Dia ::..          .';

  If TimeToStr(HoraAtual) >= '12:00:00' Then
    Result  :=  '.       ..:: Boa Tarde ::..       .';

  If TimeToStr(HoraAtual) >=  '18:00:00' Then
    Result  :=  '.       ..:: Boa Noite ::..       .';
end;
{--------==========================-----------------}


function DiferHora(Inicio,	Fim : String):String;
var
  FIni,FFim : TDateTime;
begin
  Fini := StrTotime(Inicio);
  FFim := StrToTime(Fim);
  If (Inicio > Fim) then
    begin
      Result := TimeToStr((StrTotime('23:59:59')-Fini)+FFim)
    end
  else
    begin
      Result := TimeToStr(FFim-Fini);
    end;
end;


function FormatTelefoneRemoveNove(nNumCelular : string):  string;
var
  nTel      : string;
  ndigitos  : Integer;
begin
  if nNumCelular = '' then
    exit;

  nTel  :=  SomenteNumero(nNumCelular);

  if nTel = '' then
    Exit;

  ndigitos  :=  Length(nTel);

  if ndigitos <> 13 then
    Exit;

  if nTel[5] = '9' then
    nTel  :=  Trim(Copy(nTel,  1,  4)) + Trim(Copy(nTel,  6,  Length(nTel)));

  Result  :=  nTel;
end;

function FormataTelefoneBR(nNum : string) : string;
var
  nTel      : string;
  nTelSNove : string;
  testeDDD  : string;
  ndigitos  : Integer;
  DDD       : Integer;
begin
  if nNum = '' then
    exit;

  nTel  :=  SomenteNumero(nNum);

  if nTel = '' then
    Exit;

  if nTel[1] = '0' then
    nTel  :=  Trim(Copy(nTel,  2,  Length(nTel)));

  ndigitos  :=  Length(nTel);

  case ndigitos of
    8 :
      begin
        testeDDD  :=  '55929'  +  nTel;//  FormatMaskText('9999 9999;0;_',  nTel;
      end;
    9 :
      begin
        testeDDD  :=  '5592'  + nTel;//  FormatMaskText('99999 9999;0;_',  nTel);
      end;
    10  :
      begin
        testeDDD  :=  '55'  +  nTel;//  FormatMaskText('99 99999 9999;0;_',  nTel);
      end;
    11  :
      begin
        testeDDD  :=  '55'  +  nTel;//  FormatMaskText('99 99999 9999;0;_',  nTel);
      end;
    12  :
      begin
        testeDDD  :=  nTel;//  FormatMaskText('99 99 99999 9999;0;_',  nTel);
      end;
    13  :
      begin
        testeDDD  :=  nTel;//  FormatMaskText('99 99 99999 9999;0;_',  nTel);
      end;
  else
      testeDDD  :=  nNum;
  end;

  DDD :=  StrToInt(Trim(Copy(testeDDD,  3,  2)));

  if DDD <= 28 then
    begin
      Result  :=  FormatMaskText('99 99 99999 9999;0;_',  testeDDD);
    end
  else
    begin
      if Length(testeDDD) > 12 then
        begin
          nTelSNove :=  Trim(Copy(testeDDD,  1,  4))  + Trim(Copy(testeDDD,  6,  Length(testeDDD)));
        end
      else
        begin
          nTelSNove :=  testeDDD;
        end;

      Result  :=  FormatMaskText('99 99 9999 9999;0;_',  nTelSNove);
    end;
end;

function SomenteNumero(Num  : string) : string;
var
  n2  : string;
  I: Integer;
begin
  n2  :=  '';

  for I := 1 to Length(Trim(Num)) do
    begin
      if Num[I] in ['0'..'9'] then
        begin
          n2  :=  n2  + Num[I];
        end;
    end;

  Result  :=  n2;

end;

function SoNum(Key: Char; PermitirKey: TCaractere = [#0]): Char;
begin
{
  #8  = BACKSPACE
  #9  = TAB
  #13 = ENTER
  #32 = ESPAÇO
  #42 = ASTERÍSCO '*'
  #44 = VÍRGULA   ','
  #45 = TRAÇO     '-'
}

  if (not (Key in ['0'..'9', #8, #9, #13])) then
    begin
      if not (Key in PermitirKey) then
        key :=  #0;
    end;
  Result  :=  Key;
end;

function GetStrNumber(const S: string): string;
var
  vText : PChar;
begin
  vText := PChar(S);
  Result := '';

  while (vText^ <> #0) do
  begin
    {$IFDEF UNICODE}
    if CharInSet(vText^, ['0'..'9']) then
    {$ELSE}
    if vText^ in ['0'..'9'] then
    {$ENDIF}
      Result := Result + vText^;

    Inc(vText);
  end;
end;


end.
