2009/2/12, Newton Teixeira do Nascimento Júnior
<[email protected]>:
> Olá comunidade, eis uma solução que fiz. O que acham? Att.
>
>
>
> CREATE OR REPLACE FUNCTION fs_ConvertStringHoraToSegundos(hora character
> varying(8)) RETURNS integer AS
>
> $BODY$
>
> DECLARE
>
>
>
> vetor character varying(8)[];
>
> total integer = 0;
>
> segundos integer;
>
> hora_nsegundos integer;
>
> minuto_nsegundos integer;
>
>
>
> BEGIN
>
>
>
> vetor = string_to_array(hora, ':');
>
> hora_nsegundos = CAST(vetor[1] AS INTEGER);
>
> minuto_nsegundos = CAST(vetor[2] AS INTEGER);
>
> segundos = CAST(vetor[3] AS INTEGER);
>
>
>
> total = (hora_nsegundos * 3600) + (minuto_nsegundos * 60) + segundos;--*/
>
>
>
> RETURN total;
>
>
>
> END; $BODY$
>
> LANGUAGE 'plpgsql' VOLATILE;
>
>
>
>
>
> CREATE OR REPLACE FUNCTION fs_ConvertSegundosToHora(nseg integer) RETURNS
> character(8) AS
>
> $BODY$
>
>
>
> DECLARE
>
>
>
> aux Integer;
>
> horas Integer;
>
> minutos Integer;
>
> segundos Integer;
>
>
>
> divisao1 real;
>
> divisao2 real;
>
> divisao3 real;
>
>
>
> hora_geral character varying(8);
>
> strhoras character varying(5);
>
> strminutos character varying(5);
>
> strsegundos character varying(5);
>
>
>
> BEGIN
>
>
>
> aux = mod(nseg, 3600);
>
> segundos = mod(aux,60);
>
> divisao1 = aux/60;
>
> minutos = trunc(divisao1);
>
> divisao2 = nseg/60;
>
> divisao3 = divisao2/60;
>
> horas = trunc(divisao3);
>
>
>
> If horas < 10 Then
>
>   strhoras = '0' || horas;
>
> Else
>
>    strhoras = horas;
>
> End If;
>
>
>
> If minutos < 10 Then
>
>    strminutos = '0' || minutos;
>
> Else
>
>    strminutos = minutos;
>
> End If;
>
>
>
> If segundos < 10 Then
>
>    strsegundos = '0' || segundos;
>
> Else
>
>    strsegundos = segundos;
>
> End If;
>
>
>
> hora_geral =  strhoras || ':' || strminutos || ':' || strsegundos;
>
>
>
> RETURN hora_geral;
>
>
>
> END; $BODY$
>
> LANGUAGE 'plpgsql' VOLATILE;
>
>
>
>
>
>
>
> SELECT fs_ConvertSegundosToHora(25564);
>
> SELECT fs_ConvertStringHoraToSegundos('07:06:04');
>
>
>
>
>
> ________________________________
>
> De: Newton Teixeira do Nascimento Júnior
> Enviada em: terça-feira, 10 de fevereiro de 2009 11:49
> Para: [email protected]
> Assunto: Função para converter n segundos para hora no formato HH:MM:SS
>
>
>
> Olá comunidade, alguém tem uma função em plpgsql que dado um inteiro nseg
> (um determinado número de segundos) retorne
>
> uma string que representa um horário no formato HH:MM:SS?
>
>
>
> E ao contrário? Dada uma string HH:MM:SS retorne o nseg? (essa eu acho fácil
> vou fazer a minha própria - se quiserem comparar com a minha passem ae)
>


Que tal a forma descrita no manual:
Nº seg. --> timestamp
SELECT TIMESTAMP WITH TIME ZONE 'epoch' + 982384720 * INTERVAL '1 second';

Timestamp --> Nº seg.
SELECT EXTRACT(EPOCH FROM TIME '20:38:40-08');

http://www.postgresql.org/docs/current/interactive/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT

Osvaldo
_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral

Responder a