Created
July 21, 2016 15:19
-
-
Save zaknafeyn/1d1316636715d44b80662e13f685f11f to your computer and use it in GitHub Desktop.
Uri C# extensions
This file contains 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 static class Extensions | |
{ | |
public static Uri Append(this Uri uri, params string[] paths) | |
{ | |
return new Uri(paths.Aggregate(uri.AbsoluteUri, (current, path) => | |
$"{current.TrimEnd('/')}/{path.TrimStart('/')}")); | |
} | |
public static Uri AppendQuery(this Uri uri, params string[] query) | |
{ | |
var uriBuilder = new UriBuilder(uri) { Query = string.Join("&", query) }; | |
return uriBuilder.Uri; | |
} | |
public static Uri AppendQuery(this Uri uri, string query) | |
{ | |
var uriBuilder = new UriBuilder(uri) { Query = query }; | |
return uriBuilder.Uri; | |
} | |
public static long ConvertToUnixTimestamp(this DateTime dateTime) | |
{ | |
var utcDateTime = TimeZoneInfo.ConvertTimeToUtc(dateTime); | |
var timeSpan = utcDateTime.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)); | |
var unixTimestamp = (long)Math.Round(timeSpan.TotalSeconds, 0); | |
return unixTimestamp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment