Announcement

Collapse
No announcement yet.

Need help with Windows date and time.

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

  • Need help with Windows date and time.

    Hello all,

    Can somebody explain to me how to ADD or SUBTRACT a single day from the SYSTEMTIME structure. I convert the SYSTEMTIME structure to a 64bit number with SystemTimeToFileTime and I tried adding what I thought was a day but nothing changed when I put my result back through FileTimeToSystemTime.

    1000 miliseconds in a second
    60 seconds in a minute
    60 minutes in an hour
    24 hours in a day

    so...

    1Day = (1000 * 60 * 60 * 24)


    All I want is to add or subtract a day from the SYSTEMTIME structure and have the Month and Year adjust accordingly, could somebody help here I'm about ready to toss my computer out the fourth floor here!


    Thanks



    ------------------
    Cheers!

  • #2
    You should *NEVER* perform a math on a SYSTEMTIME structure.
    That is rule no one what ever someone else tells you.
    Use SystemTimeToFileTime to get a FILETIME struct
    The FILETIME structure is a 64-bit value representing the number
    of 100-nanosecond intervals since January 1, 1601
    Make your math and use FileTimeToSystemTime to convert back
    to a SYSTEMTIME struct.
    '----
    If you only want to add days, not fractions of days to a SYSTEMTIME
    use SystemTimeToVariantTime and you will have the time in a double
    where Integerpart is a day-number, and fraction is the time

    ------------------
    Fred
    mailto:[email protected][email protected]</A>
    http://www.oxenby.se



    [This message has been edited by Fred Oxenby (edited August 09, 2001).]
    Fred
    mailto:[email protected][email protected]</A>
    http://www.oxenby.se

    Comment


    • #3
      mark,

      see this:
      http://www.powerbasic.com/support/pb...ad.php?t=23049

      ------------------
      peter.
      mailto[email protected][email protected]</a>
      Regards,
      Peter

      "Simplicity is a prerequisite for reliability"

      Comment


      • #4
        Mark
        Hear is something I cobble together very quickly that illustrates
        using Systemtime and Filetime API calls to to add and subtract days,
        with very limited testing it seems to do what you're asking, hope
        this helps.

        Regards Errol


        ' For PBCC 2
        Code:
        #INCLUDE "WIN32API.INC"
        
        %OneDay =  864000000000&&  ' 100-nanosecond increments in 1 day
        
        
        ' use our own type so we don't have to deal with a LARGE_INTEGER structure
        TYPE MYFILETIME
            qFileTime AS QUAD
        END TYPE
        
        FUNCTION PBMAIN () AS LONG
        LOCAL st AS SYSTEMTIME
        LOCAL ft AS MYFILETIME
        
        CALL GetLocalTime(ST)
        PRINT "st.wYear :"; st.wYear
        PRINT "st.wMonth:"; st.wMonth
        PRINT "st.wDay  :"; st.wDay
        
        CALL SystemTimeToFileTime(st, ft)
        ft.qFileTime = ft.qFileTime + %OneDay
        
        ' ft.qFileTime = ft.qFileTime + %OneDay * 5 'to add 5 days
        ' ft.qFileTime = ft.qFileTime - %OneDay * 10 'to go back 10 days
        ' and so on
        
        ' convert back to SystemTime time structure
        CALL FileTimeToSystemTime(ft, st)
        
        PRINT
        PRINT "After adding 1 day to date and time"
        
        PRINT "st.wYear :"; st.wYear
        PRINT "st.wMonth:"; st.wMonth
        PRINT "st.wDay  :"; st.wDay
        
        WAITKEY$
        END FUNCTION
        ------------------

        Comment


        • #5
          Code:
          #COMPILE EXE
          #INCLUDE "WIN32API.INC"
           
          %NaturalDay = 24 * 60 * 60 * 10000000
           
          GLOBAL st AS SYSTEMTIME
           
          UNION LARGETIME
            ft AS FILETIME
            qt AS QUAD
          END UNION
           
          FUNCTION DayNumber(BYVAL year AS LONG, _
                             BYVAL month AS LONG, _
                             BYVAL day AS LONG) AS LONG
            LOCAL lt AS LARGETIME
          
            IF year < 1601 THEN                            ' not allowed by Windows
              FUNCTION = %FALSE
              EXIT FUNCTION
            END IF
            st.wYear = year
            st.wMonth = month
            st.wDay = day
            dummy& = SystemTimeToFileTime(st, lt.ft)
            FUNCTION = lt.qt \ %NaturalDay
          END FUNCTION
           
          FUNCTION NumToDate(num AS LONG, lpFormat AS ASCIIZ) AS STRING
            LOCAL lt AS LARGETIME, szText AS ASCIIZ * 255
            LOCAL dwFlags AS LONG
          
            lt.qt = num * %NaturalDay
            dummy& = FileTimeToSystemTime(lt.ft, st)
            dwFlags = VAL(lpFormat)
            IF dwFlags THEN lpFormat = ""
            GetDateFormat %LOCALE_USER_DEFAULT, dwFlags, st, BYCOPY lpFormat, szText, SIZEOF(szText)
            FUNCTION = szText
          END FUNCTION
           
          FUNCTION PBMain() AS LONG
            LOCAL szLongDate AS ASCIIZ * 64
            LOCAL ToDay AS LONG, YesterDay AS LONG
           
            GetLocalTime st
            GetLocaleInfo %LOCALE_USER_DEFAULT, %LOCALE_SLONGDATE, szLongDate, SIZEOF(szLongdate)
              
            ToDay = DayNumber( st.wYear, st.wMonth, st.wDay)
            IF ISFALSE(ToDay) THEN
              MSGBOX "Windows can not handle years before 1601", 16, "Error"
              EXIT FUNCTION
            END IF
            YesterDay = ToDay - 1
            msg$ = "Today: " + NumToDate(ToDay, szLongDate) + $CRLF + _
                   "Yesterday: " + NumToDate(YesterDay, szLongDate)
            MSGBOX msg$, 64, "Substracting 1 day..."
          END FUNCTION


          ------------------
          mailto:[email protected][email protected]</A>
          www.basicguru.com/zijlema/

          Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
          http://zijlema.basicguru.eu
          *** Opinions expressed here are not necessarily untrue ***

          Comment


          • #6
            Thanks Guys!


            As it turns out I was only off by a few zeros.

            I had:
            1000 * 60 * 60 * 24 = 86,400,000

            When I should have had:
            1000000 * 60 * 60 * 24 = 864,000,000,000

            It was the old miliseconds VS nanoseconds debate. Thanks again for all the help guys, When "the net" doesn't have it the PB Forums do

            ------------------
            Cheers!

            [This message has been edited by mark smit (edited August 10, 2001).]

            Comment

            Working...
            X