SOLVED: Subtructing Time

SOLVED: Subtructing Time

Postby fraxzi » Thu Feb 16, 2012 9:01 am

Dear All,

How can you subtract time with this..

'08:01' - '08:00' = 00:01 or 1

Cant find the right Time conversion..

any help..
Last edited by fraxzi on Fri Feb 17, 2012 12:48 am, edited 1 time in total.
Kind Regards,
Frances

Fivewin for xHarbour v18.07
xHarbour v1.2.3.x
BCC 7.3 + PellesC8 ( Resource Compiler only)
ADS 10.1 / MariaDB
Crystal Reports 8.5/9.23 DE
xMate v1.15
User avatar
fraxzi
 
Posts: 811
Joined: Tue May 06, 2008 4:28 am
Location: Philippines

Re: Subtructing Time

Postby MarcoBoschi » Thu Feb 16, 2012 9:21 am

#include "fivewin.ch"
PROCEDURE Main
LOCAL cStartTime := TIME()
LOCAL cStopTime := "20:00:00"

? ElapTime( cStartTime, cStopTime )
RETURN
User avatar
MarcoBoschi
 
Posts: 1065
Joined: Thu Nov 17, 2005 11:08 am
Location: Padova - Italy

Re: Subtructing Time

Postby StefanHaupt » Thu Feb 16, 2012 10:04 am

Frances,

here you have a small sample

Code: Select all  Expand view
#include "Fivewin.ch"

function main ()

  local cTime := "08:00", cTime2 := "08:01"
 
  // TimeToSec (cTime) -> secons since midnight
  ? "Difference (in sec): ", TimeToSec (cTime2) - TimeToSec (cTime)

  RETURN (0)
kind regards
Stefan
StefanHaupt
 
Posts: 824
Joined: Thu Oct 13, 2005 7:39 am
Location: Germany

Re: Subtructing Time

Postby codemaker » Thu Feb 16, 2012 5:51 pm

Of course, take care if the second time is LESS than the first.
In such case first calculate the time difference from TIME1 to midnight, then the difference from midnight to TIME2
Then add both differences
User avatar
codemaker
 
Posts: 208
Joined: Wed Dec 03, 2008 4:48 pm
Location: Belgrade, Serbia

Re: Subtructing Time

Postby fraxzi » Fri Feb 17, 2012 12:34 am

MarcoBoschi wrote:#include "fivewin.ch"
PROCEDURE Main
LOCAL cStartTime := TIME()
LOCAL cStopTime := "20:00:00"

? ElapTime( cStartTime, cStopTime )
RETURN



Dear Marco,

Thanks so much.. I didn't know there is a function ElapTime()...
I passed this ElapTime( '08:00', '08:01' ) => '00:01:00' result is good and almost what I need.
If I use Val() to convert the result to numeric, the output is '0'..
Kind Regards,
Frances

Fivewin for xHarbour v18.07
xHarbour v1.2.3.x
BCC 7.3 + PellesC8 ( Resource Compiler only)
ADS 10.1 / MariaDB
Crystal Reports 8.5/9.23 DE
xMate v1.15
User avatar
fraxzi
 
Posts: 811
Joined: Tue May 06, 2008 4:28 am
Location: Philippines

Re: Subtructing Time

Postby fraxzi » Fri Feb 17, 2012 12:37 am

StefanHaupt wrote:Frances,

here you have a small sample

Code: Select all  Expand view
#include "Fivewin.ch"

function main ()

  local cTime := "08:00", cTime2 := "08:01"
 
  // TimeToSec (cTime) -> secons since midnight
  ? "Difference (in sec): ", TimeToSec (cTime2) - TimeToSec (cTime)

  RETURN (0)


Dear Stefan,

Thanks so much for the reply.

I test with (TimeToSec('08:01') - TimeToSec('08:00')) / 60 = 1.00

The result is good.. able to output the diff in minutes which is what I need..
Kind Regards,
Frances

Fivewin for xHarbour v18.07
xHarbour v1.2.3.x
BCC 7.3 + PellesC8 ( Resource Compiler only)
ADS 10.1 / MariaDB
Crystal Reports 8.5/9.23 DE
xMate v1.15
User avatar
fraxzi
 
Posts: 811
Joined: Tue May 06, 2008 4:28 am
Location: Philippines

Re: Subtructing Time

Postby fraxzi » Fri Feb 17, 2012 12:40 am

codemaker wrote:Of course, take care if the second time is LESS than the first.
In such case first calculate the time difference from TIME1 to midnight, then the difference from midnight to TIME2
Then add both differences


Dear Codemaster,

Thanks for the insight!

Before I read this thread this morning. I did this:

((CToT(DToC(FIELD->date)+' '+cIn) - CToT(DToC(FIELD->date)+' 08:00')) / 0.000694 => 1.00

which I think Stefan has better solution..
Kind Regards,
Frances

Fivewin for xHarbour v18.07
xHarbour v1.2.3.x
BCC 7.3 + PellesC8 ( Resource Compiler only)
ADS 10.1 / MariaDB
Crystal Reports 8.5/9.23 DE
xMate v1.15
User avatar
fraxzi
 
Posts: 811
Joined: Tue May 06, 2008 4:28 am
Location: Philippines

SOLVED: Subtructing Time

Postby fraxzi » Fri Feb 17, 2012 12:40 am

To All,


Thanks for the WOW efforts..
Kind Regards,
Frances

Fivewin for xHarbour v18.07
xHarbour v1.2.3.x
BCC 7.3 + PellesC8 ( Resource Compiler only)
ADS 10.1 / MariaDB
Crystal Reports 8.5/9.23 DE
xMate v1.15
User avatar
fraxzi
 
Posts: 811
Joined: Tue May 06, 2008 4:28 am
Location: Philippines

Re: SOLVED: Subtructing Time

Postby sambomb » Fri Feb 17, 2012 11:34 am

Code: Select all  Expand view

/***************************************************************************
* Programa ....: DateTime.PRG
* Autor .......: Samir
* Date ........: 2/3/2010 às 10:30:13
* Revisado em .: 2/3/2010 às 10:30:13
*
* Classe para tratar uma Date e Time em conjunto
*
***************************************************************************/


#include 'FiveWin.ch'

CLASS TDateTime

   //-- Propriedades -----------------------------------------------------//

   //-- Atributos a serem acessados
   DATA Date                AS Date       INIT Date()       READONLY
   DATA Time                AS Character  INIT Time()       READONLY
   DATA Secs                AS Numeric    Init 0            READONLY

   //-- Uso interno da classe
   DATA nSecsMinute         AS Numeric    Init 0            HIDDEN
   DATA nSecsHour           AS Numeric    Init 0            HIDDEN
   DATA nSecsDay            AS Numeric    Init 0            HIDDEN
   DATA nSecsMonth          AS Numeric    Init 0            HIDDEN
   DATA nSecsYear           AS Numeric    Init 0            HIDDEN

   //-- Métodos ----------------------------------------------------------//

   //-- Construção
   METHOD New(cDateIni,cTimeIni)                            CONSTRUCTOR
   METHOD End()


   METHOD Absolute(dDateReference)

   //-- Uso Interno
   METHOD Verify()                                          HIDDEN
   METHOD UpdateVar()                                       HIDDEN

   //-- Modificar atributos da classe
   METHOD SetDate(dDate) INLINE ( ::Date := dDate )
   METHOD SetTime(cTime) INLINE ( ::Time := cTime, ::Secs := Secs(cTime) )

   //-- Add Tempo
   METHOD AddTime(nTime)                                    //PUBLIC
   METHOD AddMinute(nMinute)                                //PUBLIC
   METHOD AddSecond(nSecond)                                //PUBLIC

   //-- Add Date
   METHOD AddDay(nDay)                                      //PUBLIC
   METHOD AddMonth(nMonth)                                  //PUBLIC
   METHOD AddYear(nYear)                                    //PUBLIC

   //-- Remove Tempo
   METHOD RemTime(nTime)                                    //PUBLIC
   METHOD RemMinute(nMinute)                                //PUBLIC
   METHOD RemSecond(nSecond)                                //PUBLIC

   //-- Remove Date
   METHOD RemDay(nDay)                                      //PUBLIC
   METHOD RemMonth(nMonth)                                  //PUBLIC
   METHOD RemYear(nYear)                                    //PUBLIC

   //-- Compatibilidade ( Aceitar comandos no plural ) -------------------//

   //-- Add Tempo
   METHOD AddHours(nHour)        INLINE   ::AddHour(nHour)
   METHOD AddMinutes(nMinute)    INLINE   ::AddMinute(nMinute)
   METHOD AddSeconds(nSecond)    INLINE   ::AddSecond(nSecond)

   //-- Add Date
   METHOD AddDays(nDay)          INLINE   ::AddDays(nDay)
   METHOD AddMonths(nMonth)      INLINE   ::AddMonth(nMonth)
   METHOD AddYears(nYear)        INLINE   ::AddYear(nYear)

   //-- Remove Tempo
   METHOD RemHour(nHour)         INLINE   ::RemHour(nHour)
   METHOD RemMinutes(nMinute)    INLINE   ::RemMinute(nMinute)
   METHOD RemSeconds(nSecond)    INLINE   ::RemSecond(nSecond)

   //-- Remove Date
   METHOD RemDays(nDay)          INLINE   ::RemDay(nDay)
   METHOD RemMonths(nMonth)      INLINE   ::RemMonth(nMonth)
   METHOD RemYears(nYear)        INLINE   ::RemYear(nYear)

ENDCLASS

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD New(xDateIni,cTimeIni) CLASS TDateTime
****************************************************************************
*
* Initiate the object
* Parametros: Nenhum
* Retorno: Self (Object)
*
* Autor .......: Samir
* Date ........: 2/3/2010 às 10:30:20
*
****************************************************************************

   Default xDateIni := Date(), cTimeIni := Time()
   
   If ValType(xDateIni) = "C"
      ::Date := cTod(xDateIni)
   elseif ValType(xDateIni) = "D"
      ::Date := xDateIni
   else
      ::Date := Date()
   end
   
   ::Time := cTimeIni
   
   ::Secs := Secs(cTimeIni)

   ::nSecsMinute := 60                  //-- FIXO
   ::nSecsHour   := ::nSecsMinute * 60  //-- FIXO
   ::nSecsDay    := ::nSecsHour   * 24  //-- FIXO

   //-- Atualizar Seconds do Month/Year
   ::UpdateVar()

return Self

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD End() CLASS TDateTime
****************************************************************************
*
* Release the object from memory
* Parametros: Nenhum
* Retorno: Nil
*
* Autor .......: Samir
* Date ........: 2/3/2010 às 10:30:20
*
****************************************************************************

   Self := Nil

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD UpdateVar() CLASS TDateTime
****************************************************************************
*
* Update the second counting vars
* Parametros:
* Retorno: Nil
*
* Autor: Samir
* 2/3/2010 - 13:52:18
*
****************************************************************************

   ::nSecsMonth   := ::nSecsDay    * LastDay(::Date)
   ::nSecsYear    := ::nSecsDay    * If ( IsBissexto(::Date), 366, 365 )

return nil
/*------------------------------------------------------------------------*/

****************************************************************************
METHOD Verify() CLASS TDateTime
****************************************************************************
*
* Verify if happen and Date update based on the Seconds
* Parametros: Nenhum
* Retorno: Nil
*
* Autor .......: Samir
* Date ........: 2/3/2010 às 10:30:20
*
****************************************************************************

Local lRewind := .F.

   //-- Verify if is negative or positive
   If ::Secs < 0
   
      lRewind := .T.
     
      ::Secs := Abs(::Secs)
     
   end

   //-- Increase
   If !lRewind

      //-- Verify if increase one Day
      While ::Secs > ::nSecsDay

         ::Secs -= ::nSecsDay
         ::AddDay()

      end

      //-- Update time with the rest
      ::Time := TString(::Secs)

   //-- Decrease
   else

      //-- Verify if decrease one Day
      While ::Secs > ::nSecsDay

         ::Secs -= ::nSecsDay
         ::RemDay()

      end

      //-- If time is negative, make reference to the last day
      ::RemDay()

      //-- Update time with the rest
      ::Time := TString(::nSecsDay - ::Secs)

   end
   
   ::UpdateVar()

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD AddHour(nHour) CLASS TDateTime
****************************************************************************
*
* Add Hour
* Parametros: nHour
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:33:57
*
****************************************************************************

   Default nHour := 1
   
   ::Secs += nHour * ::nSecsHour
   
   ::Verify()

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD AddMinute(nMinute) CLASS TDateTime
****************************************************************************
*
* Add Minute
* Parametros: nMinute
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:34:30
*
****************************************************************************

   Default nMinute := 1
   
   ::Secs += nMinute * ::nSecsMinute

   ::Verify()

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD AddSecond(nSecond) CLASS TDateTime
****************************************************************************
*
* Add Second
* Parametros: nSecond
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:34:58
*
****************************************************************************

local Result := nil

   Default nSecond := 1

   ::Secs += nSecond

   ::Verify()

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD AddDay(nDay) CLASS TDateTime
****************************************************************************
*
* Add Day
* Parametros: nDay
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:37:46
*
****************************************************************************

local Result := nil, nMax := 0, nEndMonth := 0, nAux := 0, cAux := ""

   Default nDay := 1

   ::Date := ::Date + nDay

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD AddMonth(nMonth) CLASS TDateTime
****************************************************************************
*
* Add Month
* Parametros: nMonth
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:34:30
*
****************************************************************************

local Result := nil

   Default nMonth := 1

   ::Date := IncMonth(::Date,nMonth)

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD AddYear(nYear) CLASS TDateTime
****************************************************************************
*
* Add Year
* Parametros: nYear
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:34:58
*
****************************************************************************


local Result := nil, nDayAux := 0, nMonthAux := 0, nYearAux := 0, cDateAux := ""

   Default nYear := 1

   nDayAux      := Day(  ::Date())
   nMonthAux    := Month(::Date())
   nYearAux     := Year( ::Date())

   nYearAux += nYear

   cDateAux += StrZero(nDayAux,2)   + "/"
   cDateAux += StrZero(nMonthAux,2) + "/"
   cDateAux += StrZero(nYearAux,4)

   ::Date := CtoD(cDateAux)

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD RemHour(nHour) CLASS TDateTime
****************************************************************************
*
* Add Hour
* Parametros: nHour
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:33:57
*
****************************************************************************

local Result := nil

   Default nHour := 1
   
   ::Secs -= nHour * ::nSecsHour

   ::Verify()

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD RemMinute(nMinute) CLASS TDateTime
****************************************************************************
*
* Add Minute
* Parametros: nMinute
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:34:30
*
****************************************************************************

local Result := nil

   Default nMinute := 1
   
   ::Secs -= nMinute * ::nSecsMinute

   ::Verify()
   
return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD RemSecond(nSecond) CLASS TDateTime
****************************************************************************
*
* Add Second
* Parametros: nSecond
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:34:58
*
****************************************************************************

local Result := nil

   Default nSecond := 1

   ::Secs -= nSecond
   
   ::Verify()

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD RemDay(nDay) CLASS TDateTime
****************************************************************************
*
* Add Day
* Parametros: nDay
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:37:46
*
****************************************************************************

local nMax := 0

   Default nDay := 1
   
   ::Date := ::Date - nDay
   
return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD RemMonth(nMonth) CLASS TDateTime
****************************************************************************
*
* Add Month
* Parametros: nMonth
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:34:30
*
****************************************************************************

   Default nMonth := 1
   
   ::Date := DecMonth(::Date,nMonth)

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD RemYear(nYear) CLASS TDateTime
****************************************************************************
*
* Add Year
* Parametros: nYear
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:34:58
*
****************************************************************************

local nDayAux := 0, nMonthAux := 0, nYearAux := 0, cDateAux := ""

   Default nYear := 1
   
   nDayAux      := Day(::Date())
   nMonthAux    := Month(::Date())
   nYearAux     := Year(::Date())
   
   nYearAux -= nYear
   
   cDateAux += StrZero(nDayAux,2)   + "/"
   cDateAux += StrZero(nMonthAux,2) + "/"
   cDateAux += StrZero(nYearAux,4)
   
   ::Date := CtoD(cDateAux)

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD Absolute(dDateReferencia) CLASS TDateTime
****************************************************************************
*
* Obter um valor absoluto em Seconds da Date mais Time
* Parametros: dDateReferencia
* Retorno: nValorAbsoluto
*
* Autor: Samir
* 17/11/2010 - 10:51:08
*
****************************************************************************

local nValorAbsoluto := 0, nDays := 0

   Default dDateReferencia := cTod("01/01/1920")

   nDays := Abs( ::Date - dDateReferencia)

   nValorAbsoluto := nDays * ::nSecsDay

   nValorAbsoluto += ::Secs

return nValorAbsoluto
/*------------------------------------------------------------------------*/

****************************************************************************
static function IsBissexto(xDate)
****************************************************************************
*
* Verificar se um ano é bissexto ou não
* Parametros: xDate
* Retorno: lResult
*
* Autor: Samir
* 2/3/2010 - 11:15:02
*
****************************************************************************

local lResult := .T., nYear := 0

   If ValType(xDate) = "D"
      nYear := Year(xDate)
   elseif ValType(xDate) = "N"
      nYear := xDate
   elseif ValType(xDate) = "C"
      If IsDigit(xDate)
         If Len(xDate) = 2 .or. Len(xDate) = 4
            nYear := Val(xDate)
         else
            lResult := .F.
         end
      else
         lResult := .F.
      end
   else
      lResult := .F.
   end

   If lResult

      If nYear % 4 = 0 .and. nYear % 100 != 0
         lResult := .T.
      Elseif nYear % 100 = 0 .and. nYear % 400 = 0
         lResult := .T.
      Else
         lResult := .F.
      End

   else

      Msg("Parâmetro de Date inválida")
   
   end

Return lResult

/*------------------------------------------------------------------------*/
 
Email: SamirSSabreu@gmail.com
xHarbour 1.2.3 + Fwhh 20.2
User avatar
sambomb
 
Posts: 388
Joined: Mon Oct 13, 2008 11:26 am
Location: Itaocara - RJ - Brasil

Re: SOLVED: Subtructing Time

Postby sambomb » Fri Feb 17, 2012 11:37 am

oDateTime := TDateTime():New()

oDateTime:SetDate( Date() )
oDateTime:SetTime( Time() )

? oDateTime:Date
? oDateTime:Time

oDateTime:AddDay(1)

? oDateTime:Date
? oDateTime:Time

oDateTime:RemSeconds( Secs( "01:00:00" ) )

? oDateTime:Date
? oDateTime:Time
Email: SamirSSabreu@gmail.com
xHarbour 1.2.3 + Fwhh 20.2
User avatar
sambomb
 
Posts: 388
Joined: Mon Oct 13, 2008 11:26 am
Location: Itaocara - RJ - Brasil

Re: SOLVED: Subtructing Time

Postby Daniel Garcia-Gil » Sat Mar 10, 2012 11:27 am

Hello sambomb

the class look fine but not work :-(

this method not exists
METHOD AddTime
METHOD RemTime

this function not exists
LastDay
IncMonth
DecMonth
Msg (it's no matter)

thanks for your contribution, can you post the missed code??
User avatar
Daniel Garcia-Gil
 
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 pm
Location: Isla de Margarita

Re: SOLVED: Subtructing Time

Postby sambomb » Wed Mar 14, 2012 2:51 pm

Try this:


Code: Select all  Expand view

/***************************************************************************
* Programa ....: DateTime.PRG
* Autor .......: Samir
* Date ........: 2/3/2010 às 10:30:13
* Revisado em .: 2/3/2010 às 10:30:13
*
* Classe para tratar uma Date e Time em conjunto
*
***************************************************************************/


#include 'FiveWin.ch'

CLASS TDateTime

   //-- Propriedades -----------------------------------------------------//

   //-- Atributos a serem acessados
   DATA Date                AS Date       INIT Date()       READONLY
   DATA Time                AS Character  INIT Time()       READONLY
   DATA Secs                AS Numeric    Init 0            READONLY

   //-- Uso interno da classe
   DATA nSecsMinute         AS Numeric    Init 0            HIDDEN
   DATA nSecsHour           AS Numeric    Init 0            HIDDEN
   DATA nSecsDay            AS Numeric    Init 0            HIDDEN
   DATA nSecsMonth          AS Numeric    Init 0            HIDDEN
   DATA nSecsYear           AS Numeric    Init 0            HIDDEN

   //-- Métodos ----------------------------------------------------------//

   //-- Construção
   METHOD New(cDateIni,cTimeIni)                            CONSTRUCTOR
   METHOD End()


   METHOD Absolute(dDateReference)

   //-- Uso Interno
   METHOD Verify()                                          HIDDEN
   METHOD UpdateVar()                                       HIDDEN

   //-- Modificar atributos da classe
   METHOD SetDate(dDate) INLINE ( ::Date := dDate )
   METHOD SetTime(cTime) INLINE ( ::Time := cTime, ::Secs := Secs(cTime) )

   //-- Add Time
   METHOD AddHour(nTime)                                    //PUBLIC
   METHOD AddMinute(nMinute)                                //PUBLIC
   METHOD AddSecond(nSecond)                                //PUBLIC

   //-- Add Date
   METHOD AddDay(nDay)                                      //PUBLIC
   METHOD AddMonth(nMonth)                                  //PUBLIC
   METHOD AddYear(nYear)                                    //PUBLIC

   //-- Remove Tempo
   METHOD RemHour(nTime)                                    //PUBLIC
   METHOD RemMinute(nMinute)                                //PUBLIC
   METHOD RemSecond(nSecond)                                //PUBLIC

   //-- Remove Date
   METHOD RemDay(nDay)                                      //PUBLIC
   METHOD RemMonth(nMonth)                                  //PUBLIC
   METHOD RemYear(nYear)                                    //PUBLIC

   //-- Compatibilidade ( Aceitar comandos no plural ) -------------------//

   //-- Add Tempo
   METHOD AddHours(nHour)        INLINE   ::AddHour(nHour)
   METHOD AddMinutes(nMinute)    INLINE   ::AddMinute(nMinute)
   METHOD AddSeconds(nSecond)    INLINE   ::AddSecond(nSecond)

   //-- Add Date
   METHOD AddDays(nDay)          INLINE   ::AddDays(nDay)
   METHOD AddMonths(nMonth)      INLINE   ::AddMonth(nMonth)
   METHOD AddYears(nYear)        INLINE   ::AddYear(nYear)

   //-- Remove Tempo
   METHOD RemHour(nHour)         INLINE   ::RemHour(nHour)
   METHOD RemMinutes(nMinute)    INLINE   ::RemMinute(nMinute)
   METHOD RemSeconds(nSecond)    INLINE   ::RemSecond(nSecond)

   //-- Remove Date
   METHOD RemDays(nDay)          INLINE   ::RemDay(nDay)
   METHOD RemMonths(nMonth)      INLINE   ::RemMonth(nMonth)
   METHOD RemYears(nYear)        INLINE   ::RemYear(nYear)

ENDCLASS

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD New(xDateIni,cTimeIni) CLASS TDateTime
****************************************************************************
*
* Initiate the object
* Parametros: Nenhum
* Retorno: Self (Object)
*
* Autor .......: Samir
* Date ........: 2/3/2010 às 10:30:20
*
****************************************************************************

   Default xDateIni := Date(), cTimeIni := Time()
   
   If ValType(xDateIni) = "C"
      ::Date := cTod(xDateIni)
   elseif ValType(xDateIni) = "D"
      ::Date := xDateIni
   else
      ::Date := Date()
   end
   
   ::Time := cTimeIni
   
   ::Secs := Secs(cTimeIni)

   ::nSecsMinute := 60                  //-- FIXO
   ::nSecsHour   := ::nSecsMinute * 60  //-- FIXO
   ::nSecsDay    := ::nSecsHour   * 24  //-- FIXO

   //-- Atualizar Seconds do Month/Year
   ::UpdateVar()

return Self

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD End() CLASS TDateTime
****************************************************************************
*
* Release the object from memory
* Parametros: Nenhum
* Retorno: Nil
*
* Autor .......: Samir
* Date ........: 2/3/2010 às 10:30:20
*
****************************************************************************

   Self := Nil

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD UpdateVar() CLASS TDateTime
****************************************************************************
*
* Update the second counting vars
* Parametros:
* Retorno: Nil
*
* Autor: Samir
* 2/3/2010 - 13:52:18
*
****************************************************************************

   ::nSecsMonth   := ::nSecsDay    * LastDay(::Date)
   ::nSecsYear    := ::nSecsDay    * If ( IsBissexto(::Date), 366, 365 )

return nil
/*------------------------------------------------------------------------*/

****************************************************************************
METHOD Verify() CLASS TDateTime
****************************************************************************
*
* Verify if happen and Date update based on the Seconds
* Parametros: Nenhum
* Retorno: Nil
*
* Autor .......: Samir
* Date ........: 2/3/2010 às 10:30:20
*
****************************************************************************

Local lRewind := .F.

   //-- Verify if is negative or positive
   If ::Secs < 0
   
      lRewind := .T.
     
      ::Secs := Abs(::Secs)
     
   end

   //-- Increase
   If !lRewind

      //-- Verify if increase one Day
      While ::Secs > ::nSecsDay

         ::Secs -= ::nSecsDay
         ::AddDay()

      end

      //-- Update time with the rest
      ::Time := TString(::Secs)

   //-- Decrease
   else

      //-- Verify if decrease one Day
      While ::Secs > ::nSecsDay

         ::Secs -= ::nSecsDay
         ::RemDay()

      end

      //-- If time is negative, make reference to the last day
      ::RemDay()

      //-- Update time with the rest
      ::Time := TString(::nSecsDay - ::Secs)

   end
   
   ::UpdateVar()

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD AddHour(nHour) CLASS TDateTime
****************************************************************************
*
* Add Hour
* Parametros: nHour
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:33:57
*
****************************************************************************

   Default nHour := 1
   
   ::Secs += nHour * ::nSecsHour
   
   ::Verify()

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD AddMinute(nMinute) CLASS TDateTime
****************************************************************************
*
* Add Minute
* Parametros: nMinute
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:34:30
*
****************************************************************************

   Default nMinute := 1
   
   ::Secs += nMinute * ::nSecsMinute

   ::Verify()

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD AddSecond(nSecond) CLASS TDateTime
****************************************************************************
*
* Add Second
* Parametros: nSecond
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:34:58
*
****************************************************************************

local Result := nil

   Default nSecond := 1

   ::Secs += nSecond

   ::Verify()

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD AddDay(nDay) CLASS TDateTime
****************************************************************************
*
* Add Day
* Parametros: nDay
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:37:46
*
****************************************************************************

local Result := nil, nMax := 0, nEndMonth := 0, nAux := 0, cAux := ""

   Default nDay := 1

   ::Date := ::Date + nDay

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD AddMonth(nMonth) CLASS TDateTime
****************************************************************************
*
* Add Month
* Parametros: nMonth
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:34:30
*
****************************************************************************

local Result := nil

   Default nMonth := 1

   ::Date := IncMonth(::Date,nMonth)

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD AddYear(nYear) CLASS TDateTime
****************************************************************************
*
* Add Year
* Parametros: nYear
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:34:58
*
****************************************************************************


local Result := nil, nDayAux := 0, nMonthAux := 0, nYearAux := 0, cDateAux := ""

   Default nYear := 1

   nDayAux      := Day(  ::Date())
   nMonthAux    := Month(::Date())
   nYearAux     := Year( ::Date())

   nYearAux += nYear

   cDateAux += StrZero(nDayAux,2)   + "/"
   cDateAux += StrZero(nMonthAux,2) + "/"
   cDateAux += StrZero(nYearAux,4)

   ::Date := CtoD(cDateAux)

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD RemHour(nHour) CLASS TDateTime
****************************************************************************
*
* Add Hour
* Parametros: nHour
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:33:57
*
****************************************************************************

local Result := nil

   Default nHour := 1
   
   ::Secs -= nHour * ::nSecsHour

   ::Verify()

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD RemMinute(nMinute) CLASS TDateTime
****************************************************************************
*
* Add Minute
* Parametros: nMinute
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:34:30
*
****************************************************************************

local Result := nil

   Default nMinute := 1
   
   ::Secs -= nMinute * ::nSecsMinute

   ::Verify()
   
return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD RemSecond(nSecond) CLASS TDateTime
****************************************************************************
*
* Add Second
* Parametros: nSecond
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:34:58
*
****************************************************************************

local Result := nil

   Default nSecond := 1

   ::Secs -= nSecond
   
   ::Verify()

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD RemDay(nDay) CLASS TDateTime
****************************************************************************
*
* Add Day
* Parametros: nDay
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:37:46
*
****************************************************************************

local nMax := 0

   Default nDay := 1
   
   ::Date := ::Date - nDay
   
return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD RemMonth(nMonth) CLASS TDateTime
****************************************************************************
*
* Add Month
* Parametros: nMonth
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:34:30
*
****************************************************************************

   Default nMonth := 1
   
   ::Date := DecMonth(::Date,nMonth)

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD RemYear(nYear) CLASS TDateTime
****************************************************************************
*
* Add Year
* Parametros: nYear
* Retorno: NIL
*
* Autor: Samir
* 2/3/2010 - 10:34:58
*
****************************************************************************

local nDayAux := 0, nMonthAux := 0, nYearAux := 0, cDateAux := ""

   Default nYear := 1
   
   nDayAux      := Day(::Date())
   nMonthAux    := Month(::Date())
   nYearAux     := Year(::Date())
   
   nYearAux -= nYear
   
   cDateAux += StrZero(nDayAux,2)   + "/"
   cDateAux += StrZero(nMonthAux,2) + "/"
   cDateAux += StrZero(nYearAux,4)
   
   ::Date := CtoD(cDateAux)

return nil

/*------------------------------------------------------------------------*/

****************************************************************************
METHOD Absolute(dDateReferencia) CLASS TDateTime
****************************************************************************
*
* Obter um valor absoluto em Seconds da Date mais Time
* Parametros: dDateReferencia
* Retorno: nValorAbsoluto
*
* Autor: Samir
* 17/11/2010 - 10:51:08
*
****************************************************************************

local nValorAbsoluto := 0, nDays := 0

   Default dDateReferencia := cTod("01/01/1920")

   nDays := Abs( ::Date - dDateReferencia)

   nValorAbsoluto := nDays * ::nSecsDay

   nValorAbsoluto += ::Secs

return nValorAbsoluto
/*------------------------------------------------------------------------*/

****************************************************************************
static function IsBissexto(xDate)
****************************************************************************
*
* Verificar se um ano é bissexto ou não
* Parametros: xDate
* Retorno: lResult
*
* Autor: Samir
* 2/3/2010 - 11:15:02
*
****************************************************************************

local lResult := .T., nYear := 0

   If ValType(xDate) = "D"
      nYear := Year(xDate)
   elseif ValType(xDate) = "N"
      nYear := xDate
   elseif ValType(xDate) = "C"
      If IsDigit(xDate)
         If Len(xDate) = 2 .or. Len(xDate) = 4
            nYear := Val(xDate)
         else
            lResult := .F.
         end
      else
         lResult := .F.
      end
   else
      lResult := .F.
   end

   If lResult

      If nYear % 4 = 0 .and. nYear % 100 != 0
         lResult := .T.
      Elseif nYear % 100 = 0 .and. nYear % 400 = 0
         lResult := .T.
      Else
         lResult := .F.
      End

   else

      MsgInfo("Invalid Date parameter")
   
   end

Return lResult

/*------------------------------------------------------------------------*/

****************************************************************************
function LastDay( dDateVal )
****************************************************************************
Local nLastDay, nMonthNum, nNumDays

   If dDateVal = NIL
      dDateVal := Date()
   ElseIf Valtype( dDateVal ) == 'N'
      nMonthNum := dDateVal
   ElseIf Valtype( dDateVal ) == 'D'
      nMonthNum := Month( dDateVal )
   Else
      Return 0
   Endif

   nNumDays  := 31

   Do Case
      Case nMonthNum = 4 .or. nMonthNum = 6 .or. nMonthNum = 9 .or. ;
         nMonthNum = 11
         nNumDays := 30
      Case nMonthNum = 2
         If Year( dDateVal ) % 4 = 0 .and. Year( dDateVal ) % 100 != 0
            nNumDays := 29
         Elseif Year( dDateVal ) % 100 = 0 .and. Year( dDateVal ) % 400 = 0
            nNumDays := 29
         Else
            nNumDays := 28
         Endif
   Endcase

Return( nNumDays )

/*------------------------------------------------------------------------*/

****************************************************************************
function IncMonth(dDate, nMonth)
****************************************************************************

local Result := dDate, nDia := 0, nAno := 0, nMes := 0, cData := ""

   nDia := Day(dDate)
   nMes := Month(dDate)
   nAno := Year(dDate)

   nMes += nMonth
   nMonth := nMes

   if nMes > 12
      if nMes % 12 == 0
         nMes := 12
      else
         nMes := nMes % 12
         nAno += Trunc(nMonth / 12,0)
      end
   end

   if (AllTrim(Str(nMes,2,0)) $ "4,6,9,11") .and. nDia >= 30
      nDia := 30
   elseif (AllTrim(Str(nMes,2,0)) == "2") .and. nDia >= 28
      nDia := 28
   end

   cData := AllTrim(Str(nDia,2,0)) + '/' + AllTrim(Str(nMes,2,0)) + '/' + ;
            AllTrim(Str(nAno,4,0))

   if Empty(CTOD(cData))
      MsgAlert(cData)
   end

   Result := CtoD(cData)

Return Result

/*------------------------------------------------------------------------*/

****************************************************************************
function DecMonth(dDate, nMonth)
****************************************************************************

local Result := dDate, nDia := 0, nAno := 0, nMes := 0, cData := ""

   nDia := Day(dDate)
   nMes := Month(dDate)
   nAno := Year(dDate)

   nAno -= Trunc(nMonth / 12,0)

   nMes -= ( nMonth % 12 )

   if nMes <= 0

      if nMonth % 12 == 0
         //nMes := 12
         nMes := Month(dDate)
      else

         nAno -= 1
         nMes += 12

      end
   end

   if (AllTrim(Str(nMes,2,0)) $ "4,6,9,11") .and. nDia = 31
      nDia := 30
   elseif (AllTrim(Str(nMes,2,0)) == "2") .and. nDia >= 29
      nDia := 28
   end

   cData := AllTrim(Str(nDia,2,0)) + '/' + AllTrim(Str(nMes,2,0)) + '/' + ;
            AllTrim(Str(nAno,4,0))

   Result := CtoD(cData)

Return Result

/*------------------------------------------------------------------------*/
 
Email: SamirSSabreu@gmail.com
xHarbour 1.2.3 + Fwhh 20.2
User avatar
sambomb
 
Posts: 388
Joined: Mon Oct 13, 2008 11:26 am
Location: Itaocara - RJ - Brasil


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 49 guests