Skip to content

Instantly share code, notes, and snippets.

@kevrcress
Created May 2, 2014 22:57
Show Gist options
  • Save kevrcress/11488211 to your computer and use it in GitHub Desktop.
Save kevrcress/11488211 to your computer and use it in GitHub Desktop.
.NET
/// <summary>
/// Converts Unix time to a regular DateTime.
/// </summary>
/// <param name="unixTime"></param>
/// <returns></returns>
public static DateTime ConvertUnixTimestamp(double unixTime)
{
DateTime stdDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
stdDateTime = stdDateTime.AddMilliseconds(unixTime).ToLocalTime();
return stdDateTime;
}
''' <summary>
''' Converts Unix time to a regular DateTime.
''' </summary>
''' <param name="unixTime"></param>
''' <returns></returns>
''' <remarks></remarks>
Private Shared Function ConvertUnixTimestamp(ByVal unixTime As Double) As DateTime
Dim stdDateTime As New DateTime(1970, 1, 1, 0, 0, 0, 0)
stdDateTime = stdDateTime.AddMilliseconds(unixTime).ToLocalTime()
Return stdDateTime
End Function
public Household(long userId)
: this(userId, null) { }
public Household(long userId, OtherObject otherObj)
{
// Do stuff here.
}
''' <summary>
''' JSON dates are in Unix time, .NET doesn't handle these very well. This function parses JSON dates from a JSON string and turns them into
''' a normal DateTime format that .NET can handle.
''' </summary>
''' <param name="json"></param>
''' <remarks></remarks>
Public Shared Sub FixJsonDates(ByRef json As String)
Dim jsonDateRegex As String = "(\/Date\((-)?\d*\)\/)"
Dim matches = Regex.Matches(json, jsonDateRegex)
Dim matchDigitsRegex As String = "(\d*)"
For Each m As Match In matches
'Example of string matched: /Date(1367467200000)/
Dim x As String = m.ToString().Replace("/Date(", "")
x = x.ToString().Replace(")/", "")
Dim dblTime As Double = 0
If Double.TryParse(x, dblTime) Then
Dim convertedDateTime As New DateTime()
convertedDateTime = ConvertUnixTimestamp(dblTime)
Dim replacementString As String = String.Empty
replacementString = convertedDateTime.ToString()
json = json.Replace(m.ToString(), replacementString)
End If
Next
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment