Skip to content

Instantly share code, notes, and snippets.

@markcastle
Last active October 27, 2018 18:01
Show Gist options
  • Save markcastle/16387596b288309314febe8e73d01a64 to your computer and use it in GitHub Desktop.
Save markcastle/16387596b288309314febe8e73d01a64 to your computer and use it in GitHub Desktop.
Simple utilities for generating Unix Epoch times from UTC in c# (and Unity)
/*
Copyright 2017 Captive Reality Ltd
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is
hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Source: https://gist.github.com/markcastle/16387596b288309314febe8e73d01a64
Description:
Utilities for generating Unix Epoch times from UTC in c# (and Unity)
*/
/// <summary>
/// Utilities for generating Unix Epoch times from UTC.
/// </summary>
public class Epoch
{
/// <summary>
/// Unix High Resolution Epoch Time (Milliseconds since UTC 1/1/1970 00:00:00)
/// </summary>
/// <returns>double</returns>
public static double EpochTimeHiRes()
{
return (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
}
/// <summary>
/// Unix High Resolution Epoch Time (Milliseconds since UTC 1/1/1970 00:00:00)
/// </summary>
/// <returns>decimal</returns>
public static decimal EpochTimeHiResAsDecimal()
{
return Convert.ToDecimal((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds);
}
/// <summary>
/// Unix High Resolution Epoch Time (Milliseconds since UTC 1/1/1970 00:00:00)
/// </summary>
/// <returns>string</returns>
public static string EpochTimeHiResAsString()
{
return (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds.ToString().Replace(".", "");
}
/// <summary>
/// Unix Epoch Time (Seconds since UTC 1/1/1970 00:00:00)
/// </summary>
/// <returns>double</returns>
public static double epochTime()
{
return (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
}
/// <summary>
/// Unix Epoch Days (Days since UTC 1/1/1970 00:00:00)
/// </summary>
/// <returns>int</returns>
public static int epochDays()
{
return (int)(((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds) / 86400);
}
/// <summary>
/// Unix Epoch Hours (Hours since UTC 1/1/1970 00:00:00)
/// </summary>
/// <returns>int</returns>
public static int epochHours()
{
return (int)(((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds) / 3600);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment