PowerBASIC Forums
  PowerBASIC for Windows
  Unix Date Time Stamp to Local Time??

Post New Topic  Post A Reply
profile | register | preferences | faq | search

UBBFriend: Email This Page to Someone! next newest topic | next oldest topic
Author Topic:   Unix Date Time Stamp to Local Time??
Gregery D Engle
Member
posted August 22, 2000 08:58 AM     Click Here to See the Profile for Gregery D Engle     Edit/Delete Message   Reply w/Quote
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

IP: Logged

Fred Oxenby
Member
posted August 22, 2000 10:45 AM     Click Here to See the Profile for Fred Oxenby     Edit/Delete Message   Reply w/Quote
Have you looked at this:

BOOL DosDateTimeToFileTime(
WORD wFatDate, // 16-bit MS-DOS date
WORD wFatTime, // 16-bit MS-DOS time
LPFILETIME lpFileTime // 64-bit file time
);

------------------
Fred
fred@oxenby.se
http://www.oxenby.se

IP: Logged

Tom Hanlin
Member
posted August 22, 2000 11:00 AM     Click Here to See the Profile for Tom Hanlin     Edit/Delete Message   Reply w/Quote
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

IP: Logged

Gregery D Engle
Member
posted August 22, 2000 11:24 AM     Click Here to See the Profile for Gregery D Engle     Edit/Delete Message   Reply w/Quote
quote:
Originally posted by Fred Oxenby:
Have you looked at this:

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

IP: Logged

Charles Dietz
Member
posted August 22, 2000 11:53 AM     Click Here to See the Profile for Charles Dietz     Edit/Delete Message   Reply w/Quote
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.

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

IP: Logged

Fred Oxenby
Member
posted August 22, 2000 01:22 PM     Click Here to See the Profile for Fred Oxenby     Edit/Delete Message   Reply w/Quote
Sorry, this is the information you need, not the Dos-stuff
http://support.microsoft.com/support/kb/articles/q167/2/96.asp

Number of seconds since Jan 1st 1970


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
fred@oxenby.se

http://www.oxenby.se

[This message has been edited by Fred Oxenby (edited August 22, 2000).]

IP: Logged

Charles Dietz
Member
posted August 22, 2000 01:40 PM     Click Here to See the Profile for Charles Dietz     Edit/Delete Message   Reply w/Quote
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


'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

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

IP: Logged

Fred Oxenby
Member
posted August 22, 2000 03:02 PM     Click Here to See the Profile for Fred Oxenby     Edit/Delete Message   Reply w/Quote

#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
fred@oxenby.se
http://www.oxenby.se

IP: Logged

Gregery D Engle
Member
posted August 22, 2000 03:03 PM     Click Here to See the Profile for Gregery D Engle     Edit/Delete Message   Reply w/Quote
Charles Dietz,

Works Perfect

------------------
-Greg

IP: Logged

Gregery D Engle
Member
posted August 22, 2000 03:15 PM     Click Here to See the Profile for Gregery D Engle     Edit/Delete Message   Reply w/Quote
Fred Oxenby,

Works Perfect too!!!

------------------
-Greg

IP: Logged

All times are EasternTime (US)

next newest topic | next oldest topic

Administrative Options: Close Topic | Archive/Move | Delete Topic
Post New Topic  Post A Reply
Hop to:

Contact Us | PowerBASIC BASIC Compilers

Copyright © 1999-2007 PowerBASIC, Inc. All Rights Reserved.


Ultimate Bulletin Board 5.45c