Created
May 2, 2014 22:57
-
-
Save kevrcress/11488211 to your computer and use it in GitHub Desktop.
.NET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' <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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Household(long userId) | |
: this(userId, null) { } | |
public Household(long userId, OtherObject otherObj) | |
{ | |
// Do stuff here. | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' <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