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)
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