FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour List all days of the year
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
List all days of the year
Posted: Mon Nov 09, 2015 09:55 AM
I wish create an array with the days of the solar year
From a Date to another date
sample from 01/09/2015 to 20/06/2016

I made a small test but not run ok
there are problems
1. it run untill to 12 december and I not Know how make to arrive to 20.06 of another year

Code (fw): Select all Collapse
#include "fivewin.ch"
#include "constant.ch"

REQUEST HB_LANG_IT
REQUEST DBFCDX
REQUEST DBFFPT
EXTERNAL ORDKEYNO,ORDKEYCOUNT,ORDCREATE,ORDKEYGOTO


Function Main()
Local aGiorni:= {}
Local ddate:= CTOD("01"+"/"+"01"+"/"+StrZero(Year(date()),4))   // inizio anno in corso
Local K,n
Local nDays:= 0
Local pgiorno

   Local oDlg
   Local  nBottom   := 29
   Local  nRight    := 95
   Local nWidth :=  Max( nRight * DLG_CHARPIX_W, 180 )
   Local nHeight := nBottom * DLG_CHARPIX_H


Local ddateiniziale
Local ddatefinale


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
   SET DATE ITALIAN

   HB_LangSelect("IT")

    ddateiniziale  := CTOD("01"+"/"+"09"+"/"+StrZero(Year(date()),4))
    ddatefinale :=  ddateiniziale+ 365   // add 1 year to ddateINItial

   // array creation 

   For k=Month(ddateiniziale) to 12 //Month(ddateFINALE)  //mesi

              pgiorno:=  CTOD("01"+"/"+str(k,2)+"/"+StrZero(Year(date()),4))    // first day of the month initial

            nDays:= nDaysMonth( pgiorno ) // return n days of the month

                FOR n=1 to nDays
                    AADD(aGiorni,{ (pGiorno), Oemtoansi(cDow(PGiorno)) ,space(5),space(5),Space(5)} )//  add to array
                    pgiorno := pgiorno+1
                 NEXT n
                 n:= 0
                 nDays:= 0
       NEXT k


  DEFINE DIALOG oDlg        ;
   TITLE "Calendario"   ;
   SIZE nWidth, nHeight  PIXEL

@ 0,0 XBROWSE oBrw OF oDlg               ;
      COLUMNS 1, 2, 3, 4,5                    ;
      HEADERS "Data","Giorno","Assenti","Sostituti","Vacanza"   ;
      COLSIZES 75, 60, 50, 50, 50           ;
      ARRAY aGiorni LINES FASTEDIT CELL SIZE 375,200 PIXEL

 WITH OBJECT oBrw
          :lHScroll := .f.
          :lFooter:=.t.
          :MakeTotals()
          :CreateFromCode()
   END
 ACTIVATE DIALOG oDlg center
return nil


//-----------------------------------------------------------------------------------------//
 function nDaysMonth( dDate )
Local nMes, cYear
Local dDay
Local aDays := {31,28,31,30,31,30,31,31,30,31,30,31}
Local nReturn
Local dateformat
if empty( dDate )
   return 0
endif
nMes := Month( dDate )
cYear := str( year( dDate ),4 )
if nMes == 2
   dateformat := set( _SET_DATEFORMAT, "dd-mm-yyyy" )
   if day( ctod( "29-02-" + cYear ) ) != 0
      nReturn := 29
   else
      nReturn := 28
   endif
   set( _SET_DATEFORMAT, dateformat )
else
   nReturn := aDays[ nMes ]
endif
Return nReturn
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Posts: 2170
Joined: Fri Jul 18, 2008 01:24 AM
Re: List all days of the year
Posted: Mon Nov 09, 2015 03:25 PM
Is this what you want to do?

Francisco J. Alegría P.

Chinandega, Nicaragua.



Fwxh-MySql-TMySql
Posts: 989
Joined: Thu Nov 24, 2005 03:01 PM
Re: List all days of the year
Posted: Mon Nov 09, 2015 04:36 PM
Silvio,

I think I understood your problem, may be we must use simple date math.

Code (fw): Select all Collapse
    ddateiniziale  := CTOD("01"+"/"+"09"+"/"+StrZero(Year(date()),4))
    ddatefinale :=  ddateiniziale+ 365   // add 1 year to ddateINItial  -> wrong. Leap Years have 366 days, normal 365
//     Using StoD is a better way, Set Date agnostic, forget CtoD. Or better you can test HB_Date()
    dDateInitiale := StoD( StrZero(Year(date()),4)+'0901' )
    dDateFinale  := StoD( StrZero(Year(date())+1,4)+'0620' )

   // array creation

   pGiorno:= dDateInitiale
   WHILE pGiorno <= dDateFinale
         AADD(aGiorni,{ (pGiorno), Oemtoansi(cDow(PGiorno)) ,space(5),space(5),Space(5)} )//  add to array
         pGiorno++
   ENDDO


there is no need to do so complicated computing. Hope it helps.
Saludos
Carlos Mora
http://harbouradvisor.blogspot.com/
StackOverflow http://stackoverflow.com/users/549761/carlos-mora
“If you think education is expensive, try ignorance"
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
Re: List all days of the year
Posted: Tue Nov 10, 2015 09:13 AM
OK this is resolved

but now I have a problem to calc diff from a data to another

this test give me 3 days

I have a teacher absent from 2.11.2015 to 5.11.2015
the function give me 3 days

but I have 4 days because the 5.11 must be inserted as absent

2.11
3.11
4.11
5.11

why give me only 3 days ?

I cannot add +1 to this function is wrong for me



test to try

Code (fw): Select all Collapse
#include "fivewin.ch"

Function test()
Local dDaGiorno, dAGiorno

   SET DATE FORMAT "dd-mm-yyyy"
   SET DELETED     ON
   SET CENTURY     ON
   SET EPOCH TO    year( date() ) - 20
   SET MULTIPLE    OFF




   dDaGiorno:=ctod("02/11/2015")

    dAGiorno :=ctod("05/11/2015")

    calcolo_Giorni(dDaGiorno, dAGiorno)



return nil


//--------------------------------------------------------------//
Function calcolo_Giorni(dDaGiorno, dAGiorno)
Local nNumeroGiorni:= Date360(dDaGiorno, dAGiorno)
return msginfo(str(nNumeroGiorni))
//--------------------------------------------------------------//

Function Date360(FirstDate, SecondDate)
    LOCAL FirstDay, SecondDay, Date360


        DO Case
            Case Day(FirstDate)=31
                    FirstDay := 30

            Case Day(FirstDate)=28 .OR. Day(FirstDate)=29

                If Month(FirstDate) = 2 //febbraio
                    FirstDay := 30
                Else
                    FirstDay := Day(FirstDate)
                EndIf

            OTHER
                FirstDay := Day(FirstDate)
        EndCASE


        DO Case
            Case Day(SecondDate)=31
                SecondDay := 30

            Case Day(SecondDate)=28 .OR. Day(SecondDate)=29

                If Month(SecondDate) = 2    //febbraio
                    SecondDay := 30
                EndIf

            OTHER
                SecondDay := Day(SecondDate)
        EndCASE



        Date360 := ((DateDiff(FirstDate, SecondDate) - 1) * 30) + ;
                                (30 -FirstDay) + SecondDay

    Return Date360

//----------------------------------------------------------------------------------------------//

    Function DateDiff(FirstDate, SecondDate)
       Return ((year(SecondDate)*12)+Month(SecondDate))-((year(FirstDate)*12)+Month(FirstDate))
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Posts: 2170
Joined: Fri Jul 18, 2008 01:24 AM
Re: List all days of the year
Posted: Tue Nov 10, 2015 02:53 PM
Silvio.Falconi wrote:OK this is resolved

but now I have a problem to calc diff from a data to another

this test give me 3 days

I have a teacher absent from 2.11.2015 to 5.11.2015
the function give me 3 days

but I have 4 days because the 5.11 must be inserted as absent

2.11
3.11
4.11
5.11

why give me only 3 days ?

I cannot add +1 to this function is wrong for me



test to try

Code (fw): Select all Collapse
#include "fivewin.ch"

Function test()
Local dDaGiorno, dAGiorno

   SET DATE FORMAT "dd-mm-yyyy"
   SET DELETED     ON
   SET CENTURY     ON
   SET EPOCH TO    year( date() ) - 20
   SET MULTIPLE    OFF




   dDaGiorno:=ctod("02/11/2015")

    dAGiorno :=ctod("05/11/2015")

    calcolo_Giorni(dDaGiorno, dAGiorno)



return nil


//--------------------------------------------------------------//
Function calcolo_Giorni(dDaGiorno, dAGiorno)
Local nNumeroGiorni:= Date360(dDaGiorno, dAGiorno)
return msginfo(str(nNumeroGiorni))
//--------------------------------------------------------------//

Function Date360(FirstDate, SecondDate)
    LOCAL FirstDay, SecondDay, Date360


        DO Case
            Case Day(FirstDate)=31
                    FirstDay := 30

            Case Day(FirstDate)=28 .OR. Day(FirstDate)=29

                If Month(FirstDate) = 2 //febbraio
                    FirstDay := 30
                Else
                    FirstDay := Day(FirstDate)
                EndIf

            OTHER
                FirstDay := Day(FirstDate)
        EndCASE


        DO Case
            Case Day(SecondDate)=31
                SecondDay := 30

            Case Day(SecondDate)=28 .OR. Day(SecondDate)=29

                If Month(SecondDate) = 2    //febbraio
                    SecondDay := 30
                EndIf

            OTHER
                SecondDay := Day(SecondDate)
        EndCASE



        Date360 := ((DateDiff(FirstDate, SecondDate) - 1) * 30) + ;
                                (30 -FirstDay) + SecondDay

    Return Date360

//----------------------------------------------------------------------------------------------//

    Function DateDiff(FirstDate, SecondDate)
       Return ((year(SecondDate)*12)+Month(SecondDate))-((year(FirstDate)*12)+Month(FirstDate))



Silvio:
Code (fw): Select all Collapse
//------------------------------------------
Function DiasPeriodo(dFI,dFF)
local aDias:={}, dDate

//DEFAULT dFI := cTod("01/09/2015"), dFF := cTod("20/06/2016")
DEFAULT dFI := cTod("02/09/2015"), dFF := cTod("05/09/2015")

dDate := dFI

While dDate <= dFF
   aadd( aDias,{day(dDate), cDow(dDate), cMonth(dDate), year(dDate)} )
   dDate ++  
Enddo

MsgInfo("Dias = "+Str(Len(aDias),3))   //HERE...

Xbrowse( aDias,"Dias entre periodos" )

Return nil
Francisco J. Alegría P.

Chinandega, Nicaragua.



Fwxh-MySql-TMySql

Continue the discussion