strange error for calculating days !!Resolved!!

Post Reply
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

strange error for calculating days !!Resolved!!

Post by Silvio.Falconi »

I have 4 date ranges (periods)
I have two dates and I want to calculate the number of days for each period.

local dcheck_in: = ctod ("17/05/2020")
local dCheck_out: = ctod ("17/06/2020")

if I do the operation

dCheck_out-dcheck_in

i get 31 days

in the calculation function for the periods they inexplicably become 32 and I have not yet understood why ( see column number 5)


Image


I have tried with other dates and sometimes the calculation is right and sometimes it is wrong

to calculate the days I use this function

Code: Select all

Function GetDayofPeriod(dDateFrom,dDateTo,aListini)
     Local d, n

 //reset days
 For n=1 to len(aListini)
  aListIni[n][5]:=0
 next

 For d = dDateFrom to dDateTo
    For n=1 to len(aListini)
       IF d >= aListini[n][1] .and. d <= aListini[n][2]
          alistini[n][5] += 1
       endif
    Next
 Next

 return alistini

if you want to try it, I have prepared a small test
Thanks in advance for the help I hope I can get from U

Code: Select all


#include "Fivewin.ch"

request dbfcdx
request dbffpt


request hb_lang_it
request hb_codepage_itwin



function Main()

   rddSetDefault( "DBFCDX" )
   setHandleCount( 100 )

   set date format "dd-mm-yyyy"
   set deleted on
   set century on
   set epoch to Year( Date() ) - 20
   set multiple off

    SetBalloon( .T. )

   HB_LANGSELECT( "IT" )
   HB_SETCODEPAGE( "ITWIN" )


   FWNumFormat( "E", .t. )

   Create_Dbf()   //periodi.dbf
   PopulatePeriodi()


   test()

return nil

//-------------------------------------------------------------------------//
// I want calculate how many days are on each  period

Function test()
 local dcheck_in   :=ctod("17/05/2020") //date()
 local dCheck_out  :=ctod("17/06/2020") //date()
 Local aPeriodi:= Crea_Periodi()  //create array
 Local aData:=GetDayofPeriod(dcheck_in,dCheck_out,aPeriodi)

 xbrowser aData TITLE "Days for period"

         ?   dCheck_out-dcheck_in
return nil



Function PopulatePeriodi()
Local aTemp  := {}
Local cYear :="2020"
 Local aDataPeriodi := { ;
    {"01/01/"+cYear,"01","26/07/"+cYear,"29/08/"+cYear,"1","Alta Stagione"},;
    {"01/01/"+cYear,"02","28/06/"+cYear,"25/07/"+cYear,"3","Media Stagione"},;
    {"01/01/"+cYear,"03","17/05/"+cYear,"27/06/"+cYear,"2","Bassa Stagione"},;
    {"01/01/"+cYear,"03","30/08/"+cYear,"20/09/"+cYear,"2","Bassa Stagione"} }


   USE PERIODI ALIAS PE
   If PE->(eof())
      PE->( FW_ArrayToDBF(aDataPeriodi))
   Endif
   DbCloseAll()


return NIL




Function GetDayofPeriod(dDateFrom,dDateTo,aListini)
     Local d, n

 //reset days
 For n=1 to len(aListini)
  aListIni[n][5]:=0
 next

 For d = dDateFrom to dDateTo
    For n=1 to len(aListini)
       IF d >= aListini[n][1] .and. d <= aListini[n][2]
          alistini[n][5] += 1
       endif
    Next
 Next

 return alistini





Function Create_Dbf()

   If .not. file("Periodi.dbf")

           DbCreate('PE',{ { "date"     , "D",   8, 0 },;
                           { "rooms_id" , "C",   4, 0 },;
                           { "check_in" , "D",   8, 0 },;
                           { "check_out", "D",   8, 0 },;
                           { "status"   , "C",   2, 0 },;
                           { "guest"    , "C",  30, 0 } }, 'DBFCDX')


      close all
      use &('PE') new
      select PE
      if FILE('Periodi.DBF')
         delete file &('Periodi.cdx')
         append from &('Periodi')
         dbcommitall()
         close all
         delete file &('Periodi.dbf')
      endif
      close all
      rename &('PE.dbf') to &('Periodi.dbf')

   ENDIF

   RETURN NIL


  function Crea_Periodi()
   Local oPeriodi
   Local aTemp  := {}

   Local nArea := Select()

    oPeriodi:=TDatabase():Open( ,"Periodi", "DBFCDX", .T. )
    oPeriodi:setorder(0)
    oPeriodi:gotop()


         DO While !oPeriodi:eof()
            AaDd(aTemp,{ oPeriodi:check_in,;
                            oPeriodi:check_out,;
                            oPeriodi:Guest,;
                            oPeriodi:rooms_id,;
                            0,; //for the days
                            0}) //for the rates

            oPeriodi:skip()
         ENDDO
         oPeriodi:close()
          Select (nArea)
 return aTemp



 
Last edited by Silvio.Falconi on Sat Apr 25, 2020 4:01 pm, edited 1 time in total.
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
ADutheil
Posts: 352
Joined: Sun May 31, 2009 6:25 pm
Location: Salvador - Bahia - Brazil

Re: strange error for calculating days

Post by ADutheil »

What happens if you change the function to:

Code: Select all

Function GetDayofPeriod(dDateFrom,dDateTo,aListini)
     Local n

 //reset days
 For n=1 to len(aListini)
  aListIni[n][5]:=0
 next

For n=1 to len(aListini)
    IF d >= aListini[n][1] .and. d <= aListini[n][2]
       alistini[n][5] := ( aListini[n][2] - aListini[n][1] )
    endif
Next

return alistini
Regards,

André Dutheil
FWH 13.04 HB 3.2 BCC 5.82 MinGW 4.5.2 MSVS 10
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

Re: strange error for calculating days

Post by Silvio.Falconi »

Perhaps the error not is there ...

This morning I saw the dateTo is wrong
on a my function to update the dates after a modify I have these lines

local dStart:= uCharToVal(dDataIniziale, "D" )
local dEnd:= uCharToVal(dDataFinale, "D" )

If I select the from 10 July to 14 July

and the dEnd return me 14 March

I not Know why and the the calculation days was wrong

I use a release of Nages for uCharToVal seems not correct


now I correct these lines with oldest my functios and now it seems correct as you can see on this picture

Image
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

Re: strange error for calculating days

Post by Silvio.Falconi »

ADutheil wrote:What happens if you change the function to:

Code: Select all

Function GetDayofPeriod(dDateFrom,dDateTo,aListini)
     Local n

 //reset days
 For n=1 to len(aListini)
  aListIni[n][5]:=0
 next

For n=1 to len(aListini)
    IF d >= aListini[n][1] .and. d <= aListini[n][2]
       alistini[n][5] := ( aListini[n][2] - aListini[n][1] )
    endif
Next

return alistini


happen...

Application
===========
Path and name: C:\Work\Prg\WinBeach\WinBeach.Exe (32 bits)
Size: 7,526,912 bytes
Compiler version: Harbour 3.2.0dev (r1904111533)
FiveWin version: FWH 20.02
C compiler version: Borland/Embarcadero C++ 7.0 (32-bit)
Windows version: 6.1, Build 7600

Time from start: 0 hours 1 mins 32 secs
Error occurred at: 25-04-2020, 18:04:48
Error description: Error BASE/1076 Parametro errato: >=
Args:
[ 1] = U
[ 2] = D 01-07-2020

Stack Calls
===========
Called from: Source\spiaggia\PInvoice.prg => GETDAYOFPERIOD( 1359 )
Called from: Source\spiaggia\PInvoice.prg => AGGIUNGIPRIMORECORD( 943 )
Called from: Source\spiaggia\PInvoice.prg => (b)INVOICE( 474 )
Called from: .\source\classes\DIALOG.PRG => TDIALOG:PAINT
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
Post Reply