Asked  7 Months ago    Answers:  5   Viewed   48 times

How do you convert Unix epoch time into real time in C#? (Epoch beginning 1/1/1970)




You can do this with DateTimeOffset

DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(epochSeconds);
DateTimeOffset dateTimeOffset2 = DateTimeOffset.FromUnixTimeMilliseconds(epochMilliseconds);

And if you need the DateTime object instead of DateTimeOffset, then you can call the DateTime property

DateTime dateTime = dateTimeOffset.DateTime;

Original answer

I presume that you mean Unix time, which is defined as the number of seconds since midnight (UTC) on 1st January 1970.

private static readonly DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

public static DateTime FromUnixTime(long unixTime)
    return epoch.AddSeconds(unixTime);
Tuesday, June 1, 2021
answered 7 Months ago

This extension method should do the job:

private static double GetUnixEpoch(this DateTime dateTime)
    var unixTime = dateTime.ToUniversalTime() - 
        new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

    return unixTime.TotalSeconds;

And you can use it as such:

var unixTime1 = DateTime.Now.GetUnixEpoch(); // precisely now
var unixTime2 = (DateTime.Now + new TimeSpan(0, 10, 0)).GetUnixEpoch(); // 10 minutes in future

Note that you need to deal with all date-times in UTC (Universal Time), since that's how the start of the Unix Epoch is defined.

Wednesday, June 30, 2021
answered 6 Months ago

googling timeval give this first result. From that page:

It is often necessary to subtract two values of type struct timeval or struct timespec. Here is the best way to do this. It works even on some peculiar operating systems where the tv_sec member has an unsigned type.

 /* Subtract the `struct timeval' values X and Y,
    storing the result in RESULT.
    Return 1 if the difference is negative, otherwise 0.  */

 timeval_subtract (result, x, y)
      struct timeval *result, *x, *y;
   /* Perform the carry for the later subtraction by updating y. */
   if (x->tv_usec < y->tv_usec) {
     int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
     y->tv_usec -= 1000000 * nsec;
     y->tv_sec += nsec;
   if (x->tv_usec - y->tv_usec > 1000000) {
     int nsec = (x->tv_usec - y->tv_usec) / 1000000;
     y->tv_usec += 1000000 * nsec;
     y->tv_sec -= nsec;

   /* Compute the time remaining to wait.
      tv_usec is certainly positive. */
   result->tv_sec = x->tv_sec - y->tv_sec;
   result->tv_usec = x->tv_usec - y->tv_usec;

   /* Return 1 if result is negative. */
   return x->tv_sec < y->tv_sec;
Tuesday, August 10, 2021
answered 4 Months ago

You can use ctime, depending on your definition of "Normal time":

Example code:

use Time::Local; 
use Time::localtime; 
my $time=timelocal(1,2,3,24,6,2010);
print "$timen"; 
$theTime = ctime($time); 
print "$theTimen";


Sat Jul 24 03:02:01 2010

Also, you don't need to use Time::Localtime (which is why you get Time::tm instead of a standard array/string from Perl's internal localtime):

use Time::Local; 
my $time=timelocal(1,2,3,24,6,2010); 
print "$timen"; 
$theTime = localtime($time); 
print "$theTimen";

Sat Jul 24 03:02:01 2010
Friday, October 15, 2021
Nitin Vijay
answered 2 Months ago

The standard leaves the epoch of std::chrono::system_clock::time_point unspecified.

There are three implementations of std::chrono::system_clock::time_point I am aware of:

  1. libc++
  2. libstdc++
  3. VS

All three of these are thin wrappers around Unix Time, which counts the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970,not counting leap seconds.

All of them are based on a signed 64 bit integral type. None of them are steady. libc++ has a tick period of microseconds. libstdc++ has a tick period of nanoseconds, and VS has a tick period of 0.1 microseconds.

In case it is useful, here is a paper which demonstrates some formula to take advantage of the unspecified but common epoch to convert back and forth from the civil calendar without going through time_t.

The advantage of using std::chrono::system_clock::from_time_t is that it is guaranteed to work by the standard. The disadvantage is that in practice it will limit you to the precision of a second (though that precision is unspecified).

The advantage of assuming the epoch of std::chrono::system_clock to be 1970-01-01, even though it is unspecified, is that you will be correct on all known implementations, and the precision available for this alternative is much higher than that provided by time_t.


The draft C++2a spec now specifies system_clock to model Unix Time: Overview [time.clock.system.overview]

1 Objects of type system_clock represent wall clock time from the system-wide realtime clock. Objects of type sys_time<Duration> measure time since (and before) 1970-01-01 00:00:00 UTC excluding leap seconds. This measure is commonly referred to as Unix time. This measure facilitates an efficient mapping between sys_time and calendar types (26.8). [Example: sys_seconds{sys_days{1970y/January/1}}.time_since_epoch() is 0s. sys_seconds{sys_days{2000y/January/1}}.time_since_epoch() is 946’684’800s, which is 10’957 * 86’400s. —end example]

And the synopsis ([time.syn]) has previously defined:

template<class Duration>
  using sys_time  = time_point<system_clock, Duration>;
using sys_seconds = sys_time<seconds>;
using sys_days    = sys_time<days>;

We are hopeful that the "a" in "C++2a" will be "0".

Monday, November 1, 2021
Dulini Atapattu
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :