Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Convert Unix Timestamp into local date and time

Posted on 2001-09-05
7
Medium Priority
?
210 Views
Last Modified: 2012-05-04
I need to convert the following using visual basic:

UNIX Timestamp: 999719410.163

Result: Wed Sep  5 12:50:10 2001

Can someone please send me a function to do this.

Thanks in advance.

0
Comment
Question by:Steve Groner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6458663
Function ConvUNIXTimeStamp(ByVal UNIXTimeStamp As Currency) As Date
Const UNIXDate = #1/1/70#

ConvUNIXTimeStamp = DateAdd("s", UNIXTimeStamp, UNIXDate)

End Function

? ConvUNIXTimeStamp(999719410.163)
9/5/01 7:50:10 PM

I suspect the differences have to to with TimeZones as in UTC.

Anthony
0
 
LVL 1

Author Comment

by:Steve Groner
ID: 6458765
Close, but how do you allow for the current time zone change from where I am compared to UTC, I am in Southern California (PDT)
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6458916
This would depend on DST. Currently it is -7.

Anthony
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 400 total points
ID: 6458923
So the function could be re-written as:

Function ConvUNIXTimeStamp(ByVal UNIXTimeStamp As Currency, ByVal TimeZone As Currency) As Date
Const UNIXDate = #1/1/70#

ConvUNIXTimeStamp = DateAdd("s", UNIXTimeStamp + (TimeZone * 60 * 60), UNIXDate)

End Function

Anthony
0
 
LVL 1

Author Comment

by:Steve Groner
ID: 6458938
Thank you very much, I had already insert that, but was wondering if there was a way to automatically grab the timezone from the machine I am using.
0
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 6459010
To get the current locale's bias based on GMT and the computer's settings, try this:

First, add this information to a module:

Private Const TIME_ZONE_ID_UNKNOWN As Long = 1
Private Const TIME_ZONE_ID_STANDARD As Long = 1
Private Const TIME_ZONE_ID_DAYLIGHT As Long = 2
Private Const TIME_ZONE_ID_INVALID As Long = &HFFFFFFFF

Private 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

Private Type TIME_ZONE_INFORMATION
   Bias As Long
   StandardName(0 To ((32 * 2) - 1)) As Byte  'unicode
   StandardDate As SYSTEMTIME
   StandardBias As Long
   DaylightName(0 To ((32 * 2) - 1)) As Byte  'unicode
   DaylightDate As SYSTEMTIME
   DaylightBias As Long
End Type

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


Then, add this function:

Private Function ConvertUnixTimestampToGMT(ByVal UnixTimeStamp As Currency) As Date

   Const UnixBaseDate = #01/01/1970#

   Dim tzi As TIME_ZONE_INFORMATION
   Dim dwBias As Long

   Select Case GetTimeZoneInformation(tzi)
   Case TIME_ZONE_ID_DAYLIGHT:
              dwBias = tzi.Bias + tzi.DaylightBias
   Case Else: dwBias = tzi.Bias + tzi.StandardBias
   End Select

   GetCurrentTimeBias = DateAdd("n", -dwBias, DateAdd("s",UnixTimeStamp, UnixBaseDate))
   
End Function


0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6459014
The following function will give you the difference in time from the workstation you are using (providing it is set correctly in the Control Panel!).  By the way, my use of the Currency data type has to do with the fact that some timezones are in fractions. i.e +8.5.  If this is never going to be your case than change to integer.

Type TIME_ZONE_INFORMATION
   Bias As Long
   StandardName(63) As Byte
   StandardDate As SYSTEMTIME
   StandardBias As Long
   DaylightName(63) As Byte
   DaylightDate As SYSTEMTIME
   DaylightBias As Long
End Type

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

Function GetCurrentTimeBias() As Currency
Const TIME_ZONE_ID_DAYLIGHT As Long = 2
Dim tzi As TIME_ZONE_INFORMATION
Dim dwBias As Long

If GetTimeZoneInformation(tzi) = TIME_ZONE_ID_DAYLIGHT Then
   dwBias = tzi.Bias + tzi.DaylightBias
Else
   dwBias = tzi.Bias + tzi.StandardBias
End If

GetCurrentTimeBias = dwBias / 60
 
End Function
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question