Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Converting UNIX timestamps to VB Date format

0 views
Skip to first unread message

Kevin Carlson

unread,
Apr 9, 1998, 3:00:00 AM4/9/98
to

Didn't see anything on dejanews regarding this, so here goes ...

I'm attaching to a database that contains timestamp data in the
traditional UNIX format, i.e. a long integer containing the number of
elapsed seconds since 1/1/70. I'd like to find an algorithm for
converting this to the Date format for reporting purposes (CDate isn't
it - I know there is a C function to convert and save the time as a
struct but I'd rather do everything in VB for simplicity).

Any ideas?

TIA,
Kevin

Peter Dubuque

unread,
Apr 10, 1998, 3:00:00 AM4/10/98
to

Kevin Carlson <Kevin.L...@boeing.com> wrote:

> I'm attaching to a database that contains timestamp data in the
> traditional UNIX format, i.e. a long integer containing the number of
> elapsed seconds since 1/1/70. I'd like to find an algorithm for
> converting this to the Date format for reporting purposes (CDate isn't
> it - I know there is a C function to convert and save the time as a
> struct but I'd rather do everything in VB for simplicity).

Try DateAdd("s", lUNIXTimeStamp, "1/1/1970").

--
Peter F. Dubuque - dub...@tiac.net - Enemy of Reason(TM) O-

Andrew MacLean

unread,
Apr 14, 1998, 3:00:00 AM4/14/98
to

Hi Kevin. We had to do this where we work as well.

The easiest way to do this is:

Function UnixToPCDate(Seconds As Long, Optional TimeZone As Integer)

Const Epoch = "1/1/70"
Const SecondsInHour = 3600

If IsMissing(TimeZone) Then
UnixToPCDate = DateAdd("s", Seconds, Epoch)
Else
UnixToPCDate = DateAdd("s", Seconds + (TimeZone * SecondsInHour),
Epoch)
End If

End Function

This allows you to pass in the TimeZone as an optional argument. You could
also choose to read this value from the Windows registry.

Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type

Type TIME_ZONE_INFORMATION
Bias As Long
StandardName(32) As Integer
StandardDate As SYSTEMTIME
StandardBias As Long
DayLightName(32) As Integer
DaylightDate As SYSTEMTIME
DaylightBias As Long
End Type

Declare Function GetTimeZoneInformation Lib "kernel32"
(lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long

Function TimeZone() As String

Dim lngRet As Long
Dim intX As Integer
Dim strRet As String
Dim TZI As TIME_ZONE_INFORMATION

lngRet = GetTimeZoneInformation(TZI)
For intX = 0 To 31
If TZI.StandardName(intX) = 0& Then Exit For
strRet = strRet & Chr(TZI.StandardName(intX))
Next
TimeZone = strRet

End Function


0 new messages