Calculo em data e hora

Discussão em 'Progress 4GL' iniciado por asbahr, Agosto 24, 2011.

  1. jc01

    jc01 Membro Participativo

    Sei que o tópico é super antigo, mas criei uma função que retorna o tempo decorrido entre dois DATETIME. Quem sabe poderá servir pra alguém um dia.
    Segue abaixo a função e um exemplo de uso:

    Código:
    FUNCTION tempo-decorrido RETURN CHAR (INPUT dt-ini AS DATETIME, INPUT dt-fim AS DATETIME):
        DEF VAR i-tot-sgs AS INT64 NO-UNDO.
        DEF VAR i-dias AS INT INITIAL 0 NO-UNDO.
        DEF VAR i-hrs AS INT INITIAL 0 NO-UNDO.
        DEF VAR i-mts AS INT INITIAL 0 NO-UNDO.
        DEF VAR i-sgs AS INT INITIAL 0 NO-UNDO.
     
        ASSIGN i-tot-sgs = INTERVAL(dt-fim, dt-ini, "seconds").
       
        ASSIGN i-dias = TRUNC(i-tot-sgs / (24 * 60 * 60), 0).
        ASSIGN i-hrs = TRUNC((i-tot-sgs / (60 * 60)), 0) - (i-dias * 24).
        ASSIGN i-mts = TRUNC((i-tot-sgs / 60), 0) - (i-hrs * 60) - (i-dias * 24 * 60).
        ASSIGN i-sgs = i-tot-sgs - (i-mts * 60) - (i-hrs * 60 * 60) - (i-dias * 24 * 60 * 60).   
     
        RETURN STRING(i-dias) + " dia(s), " +
               STRING(i-hrs) +  " hora(s), " +
               STRING(i-mts) + " minuto(s) e " +
               STRING(i-sgs) + " segundo(s)".
    END. /* FUNCTION tempo-formatado */
     
    DEF VAR dt-ini AS DATETIME INIT "03-17-2015 16:01:23".
    DEF VAR dt-fim AS DATETIME INIT "03-18-2015 17:13:56".
     
    MESSAGE tempo-decorrido(INPUT dt-ini, INPUT dt-fim)
        VIEW-AS ALERT-BOX INFO BUTTONS OK. /* retorno: 1 dia(s), 1 hora(s), 12 minuto(s) e 33 segundo(s) */
    Alexandre curtiu isso.
  2. bootstrapmaster

    bootstrapmaster Moderator Moderador Equipe de Suporte

    eu fiz assim ....
    Código:
    define variable inicial   as datetime no-undo.
    define variable final     as datetime no-undo.
    define variable horas     as integer  no-undo.
    define variable dias      as integer  no-undo.
    define variable total     as integer  no-undo.
    
    assign inicial = datetime('01-01-2015 00:00:00').
    assign final   = datetime(today, time * 1000).
    assign total = (final - inicial) / 1000.
    assign dias = truncate((total / 86340),0).
    assign horas = total - (dias * 86340).
    
    
    message 'Data Inicial    : ' inicial skip
            'Data Final      : ' final skip
            'tempo decorrido : ' dias ' dias e ' string(horas,'HH:MM:SS') ' horas'
        view-as alert-box info buttons ok.
    
    
    
    Gustavo R. Hruschka e jc01 curtiram isso.
  3. bootstrapmaster

    bootstrapmaster Moderator Moderador Equipe de Suporte

    Corrigindo, no lugar do 86340 coloca 86400
    Gustavo R. Hruschka e Alexandre curtiram isso.
  4. jc01

    jc01 Membro Participativo

    Opa, legal! O bom da programação é isso: muitas formas de se chegar a um resultado.
    Alexandre curtiu isso.
  5. bootstrapmaster

    bootstrapmaster Moderator Moderador Equipe de Suporte

    se por um acaso não haver os campos do tipo datetime, faz data inicial - data final pra saber os dias.

    depois transforma as horas em segundos e faz um menos o outro ...
    Código:
    define variable dataInicial as date    no-undo.
    define variable dataFinal   as date    no-undo.
    define variable horaInicial as integer no-undo.
    define variable horaFinal   as integer no-undo.
    define variable horas       as integer no-undo.
    define variable dias        as integer no-undo.
    define variable total       as integer no-undo.
    
    assign dataInicial = 01/01/2015
           dataFinal   = today
           horaInicial = 0
           horaFinal   = time.
    
    message 'Data Inicial : ' string(dataInicial) ' ' string(horaInicial,'HH:MM:SS') skip
            'Data Final   : ' string(dataFinal)   ' ' string(HoraFinal,'HH:MM:SS') skip
            'Tempo decorrido : ' dataFinal - dataInicial ' ' string(horaFinal - horaInicial, 'HH:MM:SS')
        view-as alert-box info buttons ok.
    
  6. Alexandre

    Alexandre Membro Participativo

    Ajudou muito!!!!

Compartilhe esta Página