Page 1 of 1

error in time format Resolved

Posted: Wed Feb 12, 2025 9:12 am
by Silvio.Falconi
I have a problem with the time format I have to calculate the access time in secs of a user by searching for it in the archive

I don't understand why the input format (attempt) is passed incorrectly

in archive I have
Tentrata 17.01.2025 15.59.26
tuscita 17.01.2025 16.00.00

so i must have only 34 secs

I tried to insert a debug with logfile function and I have saw

this is the test

if you need the archives I can send you the files dbf


Code: Select all | Expand

 
#include "FiveWin.ch"



request dbfcdx
request dbffpt

request hb_lang_it
request hb_codepage_itwin



 function Main()
    local nRow:= 10,nCol:= 2
    local nInterlinea:= 40
    local oWnd

       RddSetDefault( "DBFCDX" )
       HB_LANGSELECT( "IT" )
       HB_SETCODEPAGE( "ITWIN" )
       SetHandleCount( 100 )
       FWNumFormat( "E", .t. )

       SET DATE FORMAT "dd-mm-yyyy"
       SET DELETED     ON
       SET CENTURY     ON
       SET EPOCH TO    year( date() ) - 20
       SET MULTIPLE    OFF
       SetGetColorFocus(nRGB( 203, 225, 252 ))
       SetMGetColorFocus(nRGB( 203, 225, 252 ))

         test()

         return nil


Function test()
local cUser

cUser:="silvio"

? Calcola_TempoAccesso(cUser)

return nil


Function Calcola_TempoAccesso(cUser)
   Local oUtenti, oTabAct, cString := ""
   Local cStatus, dAccessDate, nTotalTime := 0
   Local tEntrata, tUscita
   Local nMinutes, nSeconds, nHours
   Local nArea := Select()

   tEntrata := ""
   tUscita  := ""
   nMinutes := 0
   nSeconds := 0
   nHours   := 0

   // Apri l'archivio oUtenti e controlla lo stato dell'utente
   oUtenti := TDatabase():Open( , "Utenti", "DBFCDX", .T. )
   oUtenti:SetOrder(1) // username
   oUtenti:GoTop()

   Do While !oUtenti:Eof()
      If AllTrim(oUtenti:UserName) == AllTrim(cUser)  
         cStatus := AllTrim(oUtenti:Status)
         dAccessDate := oUtenti:Fecha
         Exit
      EndIf
      oUtenti:Skip()
   EndDo

   // Se l'utente è ancora connesso, restituisci "Attualmente connesso"
   IF cStatus == "Ã"
      oUtenti:Close()
      Select (nArea)
      Return "Attualmente connesso"
   ELSE
      // Se l'utente non è connesso, calcola il tempo di accesso per quella data
      oTabAct := TDatabase():Open( , "tabact", "DBFCDX", .T. )
      oTabAct:SetOrder(2)
      oTabAct:GoTop()

      Do While !oTabAct:Eof()
         If AllTrim(oTabAct:Usuario) == AllTrim(cUser) .And. oTabAct:Fecha == dAccessDate
            If At("Entrata", AllTrim(oTabAct:Accion)) > 0
               tEntrata := AllTrim(oTabAct:Hora)  // Memorizza l'orario di entrata

               // Debug: Mostra l'orario di entrata appena letto
            *   ? "Orario di Entrata letto: ", tEntrata

               // Verifica il formato di tEntrata
               If Len(tEntrata) == 5
                  tEntrata := tEntrata + ":00"  // Aggiungi ":00" se non ci sono i secondi
               EndIf

               // Debug: Mostra l'orario di entrata dopo modifica
               * ? "Orario di Entrata dopo modifica: ", tEntrata
               logfile("silvio.txt", "Orario di Entrata dopo modifica: "+ tEntrata)

            ElseIf At("Uscita", AllTrim(oTabAct:Accion)) > 0
               tUscita := AllTrim(oTabAct:Hora)  // Memorizza l'orario di uscita
               // Debug: Mostra l'orario di uscita
              * ? "Orario di Uscita (tUscita): ", tUscita

               // Verifica il formato di tUscita
               If Len(tUscita) == 5
                  tUscita := tUscita + ":00"  // Aggiungi ":00" se non ci sono i secondi
               EndIf

               // Debug: Mostra l'orario di uscita dopo modifica

               logfile("silvio.txt", "Orario di Uscita dopo modifica: "+ tUscita)

               // Calcola la differenza in secondi
               nTotalTime += TimeDifferenceInSeconds(tEntrata, tUscita)
            EndIf
         EndIf
         oTabAct:Skip()
      EndDo

      If nTotalTime > 0
         // Converto la differenza in secondi nei formati giusti (ore, minuti, secondi)
         nHours   := Int(nTotalTime / 3600)            // Ore intere (3600 secondi in un'ora)
         nMinutes := Int((nTotalTime % 3600) / 60)    // Minuti rimanenti
         nSeconds := nTotalTime % 60                  // Secondi rimanenti

         If nHours > 0
            cString := Str(nHours, 6, 0) + " ore, " + Str(nMinutes, 2, 0) + " minuti e " + Str(nSeconds, 2, 0) + " secondi"
         ElseIf nMinutes > 0
            cString := Str(nMinutes, 6, 0) + " minuti e " + Str(nSeconds, 2, 0) + " secondi"
         Else
            cString := Str(nSeconds, 6, 0) + " secondi"
         EndIf
      Else
         cString := "Nessun dato di accesso trovato per la data " + dtoc(dAccessDate)
      EndIf

      oTabAct:Close()
   EndIf

   Select (nArea)
   oUtenti:Close()

   Return cString

   Function TimeDifferenceInSeconds(cStartTime, cEndTime)
   Local nStartHour, nStartMinute, nStartSecond
   Local nEndHour, nEndMinute, nEndSecond
   Local nStartSeconds, nEndSeconds
   Local nTimeDifference


    logfile("silvio.txt", "*Orario di Entrata : "+ cStartTime)
    logfile("silvio.txt", "*Orario di Uscita : "+ cEndTime)

   // Verifica se i parametri sono in formato stringa "hh:mm:ss"
   If Len(cStartTime) == 8 .And. Len(cEndTime) == 8
      // Estrarre ore, minuti e secondi
      nStartHour   := Val(SubStr(cStartTime, 1, 2)) // ore di inizio
      nStartMinute := Val(SubStr(cStartTime, 4, 2)) // minuti di inizio
      nStartSecond := Val(SubStr(cStartTime, 7, 2)) // secondi di inizio

      nEndHour   := Val(SubStr(cEndTime, 1, 2)) // ore di fine
      nEndMinute := Val(SubStr(cEndTime, 4, 2)) // minuti di fine
      nEndSecond := Val(SubStr(cEndTime, 7, 2)) // secondi di fine

      // Calcola i secondi totali dall'inizio della giornata
      nStartSeconds := (nStartHour * 3600) + (nStartMinute * 60) + nStartSecond
      nEndSeconds   := (nEndHour * 3600) + (nEndMinute * 60) + nEndSecond

      // Calcola la differenza in secondi
      nTimeDifference := nEndSeconds - nStartSeconds

      // Restituisci la differenza in secondi
      Return nTimeDifference
   Else
     * ? "Errore: Gli orari non sono nel formato corretto"
      Return 0
   EndIf


Re: error in time format Resolved

Posted: Wed Feb 12, 2025 9:20 am
by Silvio.Falconi
Resolved

The problem was that the calculation of the difference between the time of entry and exit was done inside the loop, and therefore it was added multiple times in the case in which multiple "Entrance" and "Exit" records were present for the same user.

Moving the calculation of nTotalTime out of the Do While loop is definitely the correct solution, because you only need to calculate the difference between the entry and exit once for each login session