what is c time_t equivalent for c#

13,693

Solution 1

Depends on how time_t was defined in the Standard C header files the DLL was compiled against.

If time_t is 64-bit, the C# equivalent is long.

If time_t is 32-bit, then it has the Year 2038 bug and you should ask whoever wrote the DLL for a non-buggy version.

Solution 2

I do not think I should say they are equivalent, but you can convert t_time to DateTime in such a way:

 int t= 1070390676; // value of time_t in an ordinary integer
 System.DateTime dt= new System.DateTime(1970,1,1).AddSeconds(t); 

And this example is from How can I convert date-time data in time_t to C# DateTime class format?

I should also say that UInt32 is used for t_time,too.check DateTime to time_t

Solution 3

According to Wikipedia's article on Time_t you could use a integer (Int32 or Int64)

Unix and POSIX-compliant systems implement time_t as an integer or real-floating type (typically a 32- or 64-bit integer) which represents the number of seconds since the start of the Unix epoch: midnight UTC of January 1, 1970 (not counting leap seconds).

Solution 4

Bastardo's solution did not help me. I was facing an issue with DST, so an additional conversion to local time was required, or the resulting time differed by one hour. This is what I do:

return new DateTime(1970, 1, 1).ToLocalTime().AddSeconds(time_t);
Share:
13,693
Sergey
Author by

Sergey

I'm a student.

Updated on July 28, 2022

Comments

  • Sergey
    Sergey almost 2 years

    There is a native method from dll written in c which takes a parameter of type time_t. Is it possible to use C# uint or ulong for this parameter?

  • Anton K
    Anton K over 9 years
    The most convenient way
  • Bastardo
    Bastardo over 9 years
    @jheriko thank you very much for your comment. I've already stated that this would not be equivalent. However, seeing this answer here with some upvotes, I believe this answer gave some people ideas and helped them. Could you please tell me what is incorrect?
  • jheriko
    jheriko over 9 years
    sorry i was so short. i was looking for a solution to this problem and just ended up using p/invoke to call from the msvcrt
  • jheriko
    jheriko over 9 years
    but my original point could have been better delivered as "doesn't actually work". showing something that almost works is interesting, but its not an answer. its also miles away from the question of "what size is time_t?" which is essentially what is being asked here.
  • Bastardo
    Bastardo over 9 years
    @jheriko thanks for explanation, sorry, which part exactly does not work in this example? In this case was AntonK being ironic by saying "the most convenient way"?
  • jheriko
    jheriko over 9 years
    its pretty simple. the number is wrong. that is all.
  • jheriko
    jheriko over 9 years
    also, i think antonk is just misguided.... maybe this is convenient, but its measurably not accurate (its not a 'drop-in' replacement for the C time function). even more importantly it is not an answer to the question... thats the main reason i down vote. its tangentally relevant to the original question at best.
  • Dan Parsonson
    Dan Parsonson over 6 years
    Please see stackoverflow.com/a/48500008/1036520 - this is correct but for the omission of specifying UTC.
  • Ladislav
    Ladislav over 3 years
    For me it worked only this way: return new DateTime(1970, 1, 1).AddSeconds(time_t).ToLocalTime();
  • dahall
    dahall over 3 years
    Or better yet, new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local).AddSeconds(time_t)
  • Syroot
    Syroot over 3 years
    Good call. I'd edit my answer to use that, if you like.