Announcement

Collapse
No announcement yet.

Unix Date Time Stamp to Local Time??

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

  • Unix Date Time Stamp to Local Time??

    Does anyone have source or a simple routine to converte Unix Date
    Time Stamp to local time?

    eg...

    965916407 <---- August 10th 2000, 9:06:47 AM

    This number is the amount of second after January 1st 1980,
    anyone work with this before?

    Thanks,
    Greg


    ------------------
    -Greg
    -Greg
    [email protected]
    MCP,MCSA,MCSE,MCSD

  • #2
    Have you looked at this:
    Code:
    BOOL DosDateTimeToFileTime(
      WORD wFatDate,          // 16-bit MS-DOS date
      WORD wFatTime,          // 16-bit MS-DOS time
      LPFILETIME lpFileTime   // 64-bit file time
    );
    ------------------
    Fred
    mailto:[email protected][email protected]</A>
    http://www.oxenby.se

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

    Comment


    • #3
      A Unix time/date is definitely not a DOS time/date!!! However, it's not too different from the 64-bit time/date value used in 32-bit Windows.

      Both Unix and Windows time/date values are based on counting up from a starting point. With Unix, it's seconds... not from 1-1-1980, I think, Gregory-- maybe 1-1-1970? With Windows, the time/date starts at 1-1-1601, and goes up in 100-nanosecond intervals.

      So, all you need to do to convert from Unix is to multiply by the number of 100-nanosecond intervals in a second (I dunno offhand how long a nanosecond is) and add the difference in starting points. See FileTimeToSystemTime and related APIs.


      ------------------
      Tom Hanlin
      PowerBASIC Staff

      Comment


      • #4
        Originally posted by Fred Oxenby:
        Have you looked at this:
        Code:
        BOOL DosDateTimeToFileTime(
          WORD wFatDate,          // 16-bit MS-DOS date
          WORD wFatTime,          // 16-bit MS-DOS time
          LPFILETIME lpFileTime   // 64-bit file time
        );
        Fred,

        I don't see how that is going to work. Unix uses more then a 64
        bit date/time handler, I see a 72 bit date/time handler and
        then you have to count in the extra bits for milliseconds as
        well.



        ------------------
        -Greg
        -Greg
        [email protected]
        MCP,MCSA,MCSE,MCSD

        Comment


        • #5
          Greg,

          It doesn't seem like the unixDate of 965916407 could possibly be the number
          of seconds since Jan 1, 1980. Each day should contain 24x3000 = 86400 secs,
          and this translates to 11180 days, which translates to over 30 years.

          I think I have the code pieced together if we could just get past this
          obsticle.

          ------------------

          Comment


          • #6
            Sorry, this is the information you need, not the Dos-stuff
            http://support.microsoft.com/support.../q167/2/96.asp

            Number of seconds since Jan 1st 1970
            Code:
            LONGLONG Int32x32To64(
              LONG Multiplier,    // first signed 32-bit integer
              LONG Multiplicand   // second signed 32-bit integer
            );
            
            Parameters
            Multiplier 
            [in] Specifies the first signed 32-bit integer for the multiplication. 
            Multiplicand 
            [in] Specifies the second signed 32-bit integer for the multiplication.
            ------------------
            Fred
            mailto:[email protected][email protected]</A>

            http://www.oxenby.se


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

            Comment


            • #7
              Greg,

              I made two adjustments to get the following code to work:

              1. Used year 1970 instead of 1980
              2. Subtracted 18000 from the unix date value which is equivalent to
              5 hours before midnight of 1-1-1970.

              see www.tondering.dk/claus/cal/node3.html

              Code:
              'Convert unix date using Julian days in the Gregorian Calendar
              'The unix date is the number of seconds since Jan 1, 1970
              '  965916407 -> 08-10-2000, 9:06:47am
              
              #COMPILE EXE
              #DIM ALL
              DECLARE FUNCTION unixDate(unix AS LONG) AS STRING
              
              FUNCTION PBMAIN
                 MSGBOX unixDate(965916407)
              END FUNCTION
              
              FUNCTION unixDate(unix AS LONG) AS STRING
                 LOCAL a AS LONG, y AS LONG, m AS LONG
                 LOCAL month AS LONG, day AS LONG, year AS LONG
                 LOCAL hour AS LONG, minute AS LONG, second AS LONG
                 LOCAL JD AS LONG, JD0 AS LONG, txt AS STRING
                 LOCAL s AS STRING, s1 AS STRING, s2 AS STRING
                 LOCAL numDays AS LONG, numSecs AS LONG
                 LOCAL b AS LONG, c AS LONG, d AS LONG, e AS LONG
                 'month = 1
                 'day = 1
                 'year = 1970
                 'a = (14 - month)\12
                 'y = year + 4800 - a
                 'm = month + 12*a - 3
                 'JD0 = day + (153*m + 2)\5 + 365*y + y\4 - y\100 + y\400 - 32045
                 'MSGBOX STR$(JD0)
                 
                 unix = unix - 18000
                 numDays = unix\86400
                 numSecs = unix MOD 86400
                 JD = 2440588 + numDays
                 a = JD + 32044
                 b = (4*a + 3)\146097
                 c = a - (146097*b)\4
                 d = (4*c + 3)\1461
                 e = c - (1461*d)\4
                 m = (5*e + 2)\153
                 day = e - (153*m + 2)\5 + 1
                 month = m + 3 - 12*(m\10)
                 year = 100*b + d - 4800 + m\10
                 hour = numSecs\3600
                 numSecs = numSecs MOD 3600
                 minute = numSecs\60
                 second = numSecs MOD 60
                 IF hour > 12 THEN hour = hour - 12: s = "pm" ELSE s = "am"
                 s1 = TRIM$(STR$(minute)): IF LEN(s1)=1 THEN s1 = "0" + s1
                 s2 = TRIM$(STR$(second)): IF LEN(s2)=1 THEN s2 = "0" + s2
                 txt = TRIM$(STR$(month))+"-"+TRIM$(STR$(day))+"-"+TRIM$(STR$(year))+", "
                 txt = txt + TRIM$(STR$(hour)) + ":" + s1 + ":" + s2 + s
                 FUNCTION = txt
              END FUNCTION
              ------------------

              Comment


              • #8
                Code:
                #Include "Win32api.inc"
                
                Union u_FILETIME
                  FT As Filetime
                  Q  As Quad
                End Union
                
                Declare Sub UnixTimeToFileTime(UnixTime As Dword, WFT As FILETIME)
                
                Function PbMain()As Long
                Dim WFT As FILETIME
                Dim ST As SYSTEMTIME
                Dim UnixTime As Dword
                    UnixTime = 965916407
                    UnixTimeToFileTime UnixTime,WFT
                    Call FileTimeToSystemTime(WFT,ST)
                    Print ST.wYear
                    Print ST.wMonth
                    Print ST.wDay
                    Print ST.wHour
                    Print ST.wMinute
                    Print ST.wSecond
                    waitkey$
                    
                End Function
                
                Sub UnixTimeToFileTime(UnixTime As Dword, WFT As FILETIME)
                Local U_WFT As U_FILETIME
                     U_WFT.Q = (UnixTime * 10000000) + 116444736000000000
                     WFT = U_WFT.FT
                End Sub
                ------------------
                Fred
                mailto:[email protected].se[email protected]</A>
                http://www.oxenby.se

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

                Comment


                • #9
                  Charles Dietz,

                  Works Perfect

                  ------------------
                  -Greg
                  -Greg
                  [email protected]
                  MCP,MCSA,MCSE,MCSD

                  Comment


                  • #10
                    Fred Oxenby,

                    Works Perfect too!!!

                    ------------------
                    -Greg
                    -Greg
                    [email protected]
                    MCP,MCSA,MCSE,MCSD

                    Comment

                    Working...
                    X