Skip to content

Instantly share code, notes, and snippets.

@zaknafeyn
Created July 21, 2016 15:19
Show Gist options
  • Save zaknafeyn/1d1316636715d44b80662e13f685f11f to your computer and use it in GitHub Desktop.
Save zaknafeyn/1d1316636715d44b80662e13f685f11f to your computer and use it in GitHub Desktop.
Uri C# extensions
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