I use Visual Basic in my application. Please don't try to use this code directly into the Downloader application, as it uses a different language. This is how I derive the true local times for the database:
These variables are set for a wide scope for the whole form:
Dim STRepoch_time_end As String
Dim STRepoch_time_start As String
And in the parsing of the JSON code, I set these values:
STRepoch_time_end = j("epoch_end")
STRepoch_time_start = j("epoch_start")
In the Import-To-Database sub, I use these lines to derive the true local time before inserting into the database:
Dim dteUnixDateEnd As DateTime
Dim dteLocalDateEnd As DateTime
Dim dteUnixDateStart As DateTime
Dim dteLocalDateStart As DateTime
Dim strUnixDateEnd As String
Dim strLocalDateEnd As String
Dim strUnixDateStart As String
Dim strLocalDateStart As String
dteUnixDateEnd = UnixTimeStampToDateTime(Val(STRepoch_time_end))
strUnixDateEnd = dteUnixDateEnd.ToString("yyyy-MM-dd" & "T" & "HH:mm:ss.fff" & "Z")
dteLocalDateEnd = UnixTimeStampToDateTimeLocal(Val(STRepoch_time_end))
strLocalDateEnd = dteLocalDateEnd.ToString("yyyy-MM-dd HH:mm:ss")
dteUnixDateStart = UnixTimeStampToDateTime(Val(STRepoch_time_start))
strUnixDateStart = dteUnixDateStart.ToString("yyyy-MM-dd" & "T" & "HH:mm:ss.fff" & "Z")
dteLocalDateStart = UnixTimeStampToDateTimeLocal(Val(STRepoch_time_start))
strLocalDateStart = dteLocalDateStart.ToString("yyyy-MM-dd HH:mm:ss")
And in the INSERT query, I use these values to insert into these fields:
epoch_time_end,epoch_time_start (field names)
strUnixDateEnd,strUnixDateStart (values)
And here are the functions which do the UNIX conversions to local time:
Public Shared Function UnixTimeStampToDateTime(ByVal unixTimeStamp As Double) As DateTime
Dim dtDateTime As System.DateTime = New DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc)
dtDateTime = dtDateTime.AddMilliseconds(unixTimeStamp).ToUniversalTime()
Return dtDateTime
End Function
Public Shared Function UnixTimeStampToDateTimeLocal(ByVal unixTimeStamp As Double) As DateTime
Dim dtDateTime As System.DateTime = New DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc)
dtDateTime = dtDateTime.AddMilliseconds(unixTimeStamp).ToLocalTime()
Return dtDateTime
End Function