Announcement

Collapse
No announcement yet.

DateTime arithmatic

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • David L Morris
    replied
    Thanks Fred,

    Your example works well the code is very clear to follow.

    Regards

    Leave a comment:


  • Fred Oxenby
    replied
    To be able to do math on filetime, create a union (UFTIME)
    Then it is quite straightforward
    FILETIME resulotion (value) represents the number of 100-nanosecond
    intervals since January 1, 1601.
    Thats why %MSEC_TO_FTIME = 10000&&

    Code:
    #compile exe
    #Include "Win32api.inc"
     
    Union UFTIME
      FT       As FILETIME
      qdTime   As Quad
    End Union
     
    %DAYS_AS_MSEC   = 86400000&&
    %HOUR_AS_MSEC   = 3600000&&
    %MINS_AS_MSEC   = 60000&&
    %SECS_AS_MSEC   = 1000&&
    %MSEC_AS_FTIME  = 10000&&
     
    Function AddToFileTime (Days&,Hours&,Minutes&,Seconds&,MilliSecs&) As Quad
     Function = (Days&      * %DAYS_AS_MSEC + _
                Hours&      * %HOUR_AS_MSEC + _
                Minutes&    * %MINS_AS_MSEC + _
                Seconds&    * %SECS_AS_MSEC + _
                MilliSecs&) * %MSEC_AS_FTIME
    End Function
     
    Function FormatST(ST as SYSTEMTIME)as string
     Function = Format$(ST.wYear,"0000") & "-" & _
                Format$(ST.wMonth,"00") & "-" & _
                Format$(ST.wDay,"00") & " " & _
                Format$(ST.wHour,"00") & ":" & _
                Format$(ST.wMinute,"00") & ":" & _
                Format$(ST.wSecond,"00") & ":" & _
                Format$(ST.wMilliSeconds,"000")
    End Function
     
    Function PbMain()As Long
      Dim HH&,MM&,SS&,MS&
      Dim ST As SYSTEMTIME
      Dim QT As UFTIME
      Call GetLocalTime(ST)
      R1$ = FormatST$(ST) & " => Time just Now " & Chr$(13,10)
      Call SystemTimeToFileTime(ST,QT)
      QT.qdTime = QT.qdTime + AddToFileTime(31,10,10,10,0)
      Call FileTimeToSystemTime(QT,ST)
      R2$ = FormatST$(ST) & " => after adding some Time "
      Msgbox R1$ & R2$
    End Function
    -------------
    Fred
    mailto:[email protected][email protected]</A>
    http://www.oxenby.se


    [This message has been edited by Fred Oxenby (edited January 11, 2000).]

    Leave a comment:


  • David L Morris
    replied
    I think I might have cracked it with these small changes. I'll have to
    do some more validation testing:-

    #COMPILE EXE
    #INCLUDE "Win32API.INC"
    'Trying to convert the example from Microsoft recommendations for doing
    'simple arithmatic on dates and times
    'http://support.microsoft.com/support/kb/articles/Q188/7/68.ASP

    FUNCTION TimeStamp() AS STRING
    LOCAL tTime AS ASCIIZ * 64
    LOCAL st AS systemtime, ft AS FILETIME
    LOCAL ONE_SECOND AS QUAD, ONE_MINUTE AS QUAD, _
    ONE_HOUR AS QUAD, ONE_DAY AS QUAD, qwResult AS QUAD, _
    HiTime AS QUAD, LoTime??? 'Note change of HiTime to QUAD

    ONE_SECOND = 10000000 '100 nanosecond intervals
    ONE_MINUTE = (60 * ONE_SECOND)
    ONE_HOUR = (60 * ONE_MINUTE)
    ONE_DAY = (24 * ONE_HOUR)

    GetLocalTime st
    SystemTimeToFileTime st, ft
    'Here I am trying to copy the time into a quadword with c code:-
    '(((ULONGULONG) ft.dwHighDateTime) << 32) + ft.dwLowDateTime
    HiTime = CQUD(ft.dwHighDateTime)
    LoTime??? = CDWD(ft.dwLowDateTime)
    SHIFT LEFT HiTime, 32
    qwResult = CQUD(HiTime + LoTime???)

    qwResult = qwResult + 30 * ONE_DAY 'Attempt to add 30 days

    'Here I am trying to copy the result back to FILETIME with c code:-
    'ft.dwLowDateTime = (DWORD) (qwResult & 0xFFFFFFFF);
    'ft.dwHighDateTime = (DWORD) (qwResult >> 32);
    LoTime??? = CDWD(qwResult AND &HFFFFFFFF)
    SHIFT RIGHT qwResult, 32
    HiTime = qwResult
    ft.dwLowDateTime = CLNG(LoTime???)
    ft.dwHighDateTime = CLNG(HiTime)
    FileTimeToSystemTime ft, st

    FUNCTION = TRIM$(STR$(st.wyear)) + _
    FORMAT$(st.wMonth,"00") + _
    FORMAT$(st.wDay,"00") + _
    FORMAT$(st.wHour,"00") + _
    FORMAT$(st.wMinute,"00") + _
    FORMAT$(st.wSecond,"00") + "." + _
    FORMAT$(st.wMilliseconds, "000")
    END FUNCTION

    FUNCTION PBMAIN
    LOCAL TimeString AS STRING
    TimeString = TimeStamp
    MSGBOX TimeString, 48,"TimeString outside Function TimeStamp"
    'This now displays a string showing 30 days later than today

    END FUNCTION

    Leave a comment:


  • David L Morris
    started a topic DateTime arithmatic

    DateTime arithmatic

    Maybe someone can help on this attempt to follow a recommendation
    in the msdn Knowledge Base on how to best do datetime arithmatic.

    My problem is in the conversions to quadword, doing the arithmatic,
    and converting back.

    #COMPILE EXE
    #INCLUDE "Win32API.INC"
    'Trying to convert the example from Microsoft recommendations for doing
    'simple arithmatic on dates and times
    'http://support.microsoft.com/support/kb/articles/Q188/7/68.ASP

    FUNCTION TimeStamp() AS STRING
    LOCAL tTime AS ASCIIZ * 64
    LOCAL st AS systemtime, ft AS FILETIME
    LOCAL ONE_SECOND AS QUAD, ONE_MINUTE AS QUAD, _
    ONE_HOUR AS QUAD, ONE_DAY AS QUAD, qwResult AS QUAD, _
    HiTime???, LoTime???

    ONE_SECOND = 10000000 '100 nanosecond intervals
    ONE_MINUTE = (60 * ONE_SECOND)
    ONE_HOUR = (60 * ONE_MINUTE)
    ONE_DAY = (24 * ONE_HOUR)

    GetLocalTime st
    SystemTimeToFileTime st, ft
    'Here I am trying to copy the time into a quadword with c code:-
    '(((ULONGULONG) ft.dwHighDateTime) << 32) + ft.dwLowDateTime
    HiTime??? = (ft.dwHighDateTime)
    LoTime??? = (ft.dwLowDateTime)
    SHIFT LEFT HiTime???, 32
    qwResult = HiTime??? + LoTime???

    qwResult = qwResult + 30 * ONE_DAY 'Attempt to add 30 days

    'Here I am trying to copy the result back to FILETIME with c code:-
    'ft.dwLowDateTime = (DWORD) (qwResult & 0xFFFFFFFF);
    'ft.dwHighDateTime = (DWORD) (qwResult >> 32);
    LoTime = qwResult AND &HFFFFFFFF
    SHIFT RIGHT qwResult, 32
    HiTime = qwResult
    ft.dwLowDateTime = LoTime???
    ft.dwHighDateTime = HiTime???
    FileTimeToSystemTime ft, st

    FUNCTION = TRIM$(STR$(st.wyear)) + _
    FORMAT$(st.wMonth,"00") + _
    FORMAT$(st.wDay,"00") + _
    FORMAT$(st.wHour,"00") + _
    FORMAT$(st.wMinute,"00") + _
    FORMAT$(st.wSecond,"00") + "." + _
    FORMAT$(st.wMilliseconds, "000")
    END FUNCTION

    FUNCTION PBMAIN
    LOCAL TimeString AS STRING
    TimeString = TimeStamp
    MSGBOX TimeString, 48,"TimeString outside Function TimeStamp"
    'I was hoping to display a string showing 30 days later than today
    'but I get a date back in 1601 so my attempt to convert the c code
    'is not correct.
    END FUNCTION


Working...
X