#region License
// Copyright (c) 2014 Harry Martinez <harry@zumicts.com>
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
#region
using System;
#endregion
namespace Zumicts.Utility
{
public static class TimeHelper
{
public readonly static DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
public static long GetCurrentUnixTimestamp()
{
return (long)(DateTime.UtcNow - UnixEpoch).TotalSeconds;
}
public static DateTime ConvertFromUnixTimestamp(long unixTime)
{
return UnixEpoch.AddSeconds(unixTime);
}
public static long ConvertToUnixTimestamp(DateTime time)
{
return (long)(time - UnixEpoch).TotalSeconds;
}
public static string ToTimeSince(long unixTime)
{
return ToTimeSince(ConvertFromUnixTimestamp(unixTime));
}
public static string ToTimeSince(DateTime time)
{
var now = DateTime.UtcNow;
var since = now - time;
if (since.TotalSeconds < 60)
return Math.Floor(since.TotalSeconds) + "s ago";
if (since.TotalMinutes < 60)
return Math.Floor(since.TotalMinutes) + "m ago";
if (since.TotalHours < 24)
return Math.Floor(since.TotalHours) + "h ago";
return Math.Floor(since.TotalDays) + "d ago";
}
}
}