Convert time_t to DateTime, How? 
Author Message
 Convert time_t to DateTime, How?

I have a time_t (a UNIX time value) that I need to convert to a DateTime
value.  Has anyone run across this problem before?  I see lots of C++ stuff
to do it, but haven't found any C# example.  Well, back to Google to see if
I can find out anything else...

Thanks in advance



Mon, 13 Dec 2004 22:19:20 GMT  
 Convert time_t to DateTime, How?
Johnny,

    time_t represents the number of seconds that have occured since
1/1/1970.  Given this, you can do the following:

// Get the value from a time_t value, which is an int.  time_t_value is the
time_t value.
DateTime pdatDate = new DateTime(1970, 1, 1) + new TimeSpan(0, 0,
time_t_value);

    Hope this helps.

--
               - Nicholas Paldino [.NET MVP]


Quote:
> I have a time_t (a UNIX time value) that I need to convert to a DateTime
> value.  Has anyone run across this problem before?  I see lots of C++
stuff
> to do it, but haven't found any C# example.  Well, back to Google to see
if
> I can find out anything else...

> Thanks in advance



Mon, 13 Dec 2004 22:45:40 GMT  
 Convert time_t to DateTime, How?


Quote:
> I have a time_t (a UNIX time value) that I need to convert to a DateTime
> value.  Has anyone run across this problem before?  I see lots of C++
stuff
> to do it, but haven't found any C# example.  Well, back to Google to see
if
> I can find out anything else...

http://www.dotnet247.com/247reference/msgs/20/101708.aspx

The beginning of the Unix epoch is 1/1/1970.
Initialize a DateTime to that date, then AddSeconds(time_t).

-c



Mon, 13 Dec 2004 22:56:12 GMT  
 Convert time_t to DateTime, How?
Thanks for the quick reply, I figured out as much and made these two
functions;

/// <summary>
/// Convert a time_t value into a dot.net DateTime value
/// </summary>
/// <param name="time_t">Unix/Posix Time Value</param>
/// <returns>Dot.Net DateTime Value</returns>
public static DateTime ConvertFromTime_T(long time_t)
{
 DateTime dt70 = new DateTime( 1970, 1, 1, 0, 0, 0, 0 );
 long tickTotal = dt70.Ticks + (time_t * 10000000L);
 DateTime netTimeT = new DateTime(tickTotal);
 return netTimeT;

Quote:
}

/// <summary>
/// Convert a Dot.Net DateTime to a time_t value
/// </summary>
/// <param name="DotNetDate">The Dot.Net DateTime value</param>
/// <returns>The time_t value</returns>
public static long ConvertToTime_T(DateTime DotNetDate)
{
 DateTime dt70 = new DateTime( 1970, 1, 1, 0, 0, 0, 0 );
 long tickTotal = DotNetDate.Ticks - dt70.Ticks;
 tickTotal /= 10000000L;
 return tickTotal;

Quote:
}

While not as elegant as your conversion method, I am able to convert back
and forth in a similar fashion.  I found the basis for this on another
usenet board...

Thanks again



Quote:
> Johnny,

>     time_t represents the number of seconds that have occured since
> 1/1/1970.  Given this, you can do the following:

> // Get the value from a time_t value, which is an int.  time_t_value is
the
> time_t value.
> DateTime pdatDate = new DateTime(1970, 1, 1) + new TimeSpan(0, 0,
> time_t_value);

>     Hope this helps.

> --
>                - Nicholas Paldino [.NET MVP]



> > I have a time_t (a UNIX time value) that I need to convert to a DateTime
> > value.  Has anyone run across this problem before?  I see lots of C++
> stuff
> > to do it, but haven't found any C# example.  Well, back to Google to see
> if
> > I can find out anything else...

> > Thanks in advance



Mon, 13 Dec 2004 23:13:38 GMT  
 Convert time_t to DateTime, How?
Johnny,

    Why do the multiplication for the ticks yourself?  It seems you are
doing something that is already taken care of for you in the framework.

--
               - Nicholas Paldino [.NET MVP]


Quote:
> Thanks for the quick reply, I figured out as much and made these two
> functions;

> /// <summary>
> /// Convert a time_t value into a dot.net DateTime value
> /// </summary>
> /// <param name="time_t">Unix/Posix Time Value</param>
> /// <returns>Dot.Net DateTime Value</returns>
> public static DateTime ConvertFromTime_T(long time_t)
> {
>  DateTime dt70 = new DateTime( 1970, 1, 1, 0, 0, 0, 0 );
>  long tickTotal = dt70.Ticks + (time_t * 10000000L);
>  DateTime netTimeT = new DateTime(tickTotal);
>  return netTimeT;
> }

> /// <summary>
> /// Convert a Dot.Net DateTime to a time_t value
> /// </summary>
> /// <param name="DotNetDate">The Dot.Net DateTime value</param>
> /// <returns>The time_t value</returns>
> public static long ConvertToTime_T(DateTime DotNetDate)
> {
>  DateTime dt70 = new DateTime( 1970, 1, 1, 0, 0, 0, 0 );
>  long tickTotal = DotNetDate.Ticks - dt70.Ticks;
>  tickTotal /= 10000000L;
>  return tickTotal;
> }

> While not as elegant as your conversion method, I am able to convert back
> and forth in a similar fashion.  I found the basis for this on another
> usenet board...

> Thanks again


wrote

> > Johnny,

> >     time_t represents the number of seconds that have occured since
> > 1/1/1970.  Given this, you can do the following:

> > // Get the value from a time_t value, which is an int.  time_t_value is
> the
> > time_t value.
> > DateTime pdatDate = new DateTime(1970, 1, 1) + new TimeSpan(0, 0,
> > time_t_value);

> >     Hope this helps.

> > --
> >                - Nicholas Paldino [.NET MVP]



> > > I have a time_t (a UNIX time value) that I need to convert to a
DateTime
> > > value.  Has anyone run across this problem before?  I see lots of C++
> > stuff
> > > to do it, but haven't found any C# example.  Well, back to Google to
see
> > if
> > > I can find out anything else...

> > > Thanks in advance



Mon, 13 Dec 2004 23:14:22 GMT  
 Convert time_t to DateTime, How?
Because I wasn't smart enough to scroll down to see the TotalSeconds
property of the TimeSpan object....



Quote:
> Johnny,

>     Why do the multiplication for the ticks yourself?  It seems you are
> doing something that is already taken care of for you in the framework.

> --
>                - Nicholas Paldino [.NET MVP]



> > Thanks for the quick reply, I figured out as much and made these two
> > functions;

> > /// <summary>
> > /// Convert a time_t value into a dot.net DateTime value
> > /// </summary>
> > /// <param name="time_t">Unix/Posix Time Value</param>
> > /// <returns>Dot.Net DateTime Value</returns>
> > public static DateTime ConvertFromTime_T(long time_t)
> > {
> >  DateTime dt70 = new DateTime( 1970, 1, 1, 0, 0, 0, 0 );
> >  long tickTotal = dt70.Ticks + (time_t * 10000000L);
> >  DateTime netTimeT = new DateTime(tickTotal);
> >  return netTimeT;
> > }

> > /// <summary>
> > /// Convert a Dot.Net DateTime to a time_t value
> > /// </summary>
> > /// <param name="DotNetDate">The Dot.Net DateTime value</param>
> > /// <returns>The time_t value</returns>
> > public static long ConvertToTime_T(DateTime DotNetDate)
> > {
> >  DateTime dt70 = new DateTime( 1970, 1, 1, 0, 0, 0, 0 );
> >  long tickTotal = DotNetDate.Ticks - dt70.Ticks;
> >  tickTotal /= 10000000L;
> >  return tickTotal;
> > }

> > While not as elegant as your conversion method, I am able to convert
back
> > and forth in a similar fashion.  I found the basis for this on another
> > usenet board...

> > Thanks again


> wrote

> > > Johnny,

> > >     time_t represents the number of seconds that have occured since
> > > 1/1/1970.  Given this, you can do the following:

> > > // Get the value from a time_t value, which is an int.  time_t_value
is
> > the
> > > time_t value.
> > > DateTime pdatDate = new DateTime(1970, 1, 1) + new TimeSpan(0, 0,
> > > time_t_value);

> > >     Hope this helps.

> > > --
> > >                - Nicholas Paldino [.NET MVP]



> > > > I have a time_t (a UNIX time value) that I need to convert to a
> DateTime
> > > > value.  Has anyone run across this problem before?  I see lots of
C++
> > > stuff
> > > > to do it, but haven't found any C# example.  Well, back to Google to
> see
> > > if
> > > > I can find out anything else...

> > > > Thanks in advance



Tue, 14 Dec 2004 00:51:59 GMT  
 Convert time_t to DateTime, How?
It is a very good property in a developer to be honest with
yourself.

-c


Quote:
> Because I wasn't smart enough to scroll down to see the TotalSeconds
> property of the TimeSpan object....


wrote

> > Johnny,

> >     Why do the multiplication for the ticks yourself?  It seems you are
> > doing something that is already taken care of for you in the framework.

> > --
> >                - Nicholas Paldino [.NET MVP]



> > > Thanks for the quick reply, I figured out as much and made these two
> > > functions;

> > > /// <summary>
> > > /// Convert a time_t value into a dot.net DateTime value
> > > /// </summary>
> > > /// <param name="time_t">Unix/Posix Time Value</param>
> > > /// <returns>Dot.Net DateTime Value</returns>
> > > public static DateTime ConvertFromTime_T(long time_t)
> > > {
> > >  DateTime dt70 = new DateTime( 1970, 1, 1, 0, 0, 0, 0 );
> > >  long tickTotal = dt70.Ticks + (time_t * 10000000L);
> > >  DateTime netTimeT = new DateTime(tickTotal);
> > >  return netTimeT;
> > > }

> > > /// <summary>
> > > /// Convert a Dot.Net DateTime to a time_t value
> > > /// </summary>
> > > /// <param name="DotNetDate">The Dot.Net DateTime value</param>
> > > /// <returns>The time_t value</returns>
> > > public static long ConvertToTime_T(DateTime DotNetDate)
> > > {
> > >  DateTime dt70 = new DateTime( 1970, 1, 1, 0, 0, 0, 0 );
> > >  long tickTotal = DotNetDate.Ticks - dt70.Ticks;
> > >  tickTotal /= 10000000L;
> > >  return tickTotal;
> > > }

> > > While not as elegant as your conversion method, I am able to convert
> back
> > > and forth in a similar fashion.  I found the basis for this on another
> > > usenet board...

> > > Thanks again


> > wrote

> > > > Johnny,

> > > >     time_t represents the number of seconds that have occured since
> > > > 1/1/1970.  Given this, you can do the following:

> > > > // Get the value from a time_t value, which is an int.  time_t_value
> is
> > > the
> > > > time_t value.
> > > > DateTime pdatDate = new DateTime(1970, 1, 1) + new TimeSpan(0, 0,
> > > > time_t_value);

> > > >     Hope this helps.

> > > > --
> > > >                - Nicholas Paldino [.NET MVP]



> > > > > I have a time_t (a UNIX time value) that I need to convert to a
> > DateTime
> > > > > value.  Has anyone run across this problem before?  I see lots of
> C++
> > > > stuff
> > > > > to do it, but haven't found any C# example.  Well, back to Google
to
> > see
> > > > if
> > > > > I can find out anything else...

> > > > > Thanks in advance



Tue, 14 Dec 2004 02:56:49 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Converting from time_t to datetime.

2. how to convert string in listbox with datetime format to Datetime datatype

3. Getting datetime form SQL, ADO returning datetime, how to convert it in time_t ?

4. Convet C++ time_t to C# DateTime

5. Datetime strings to time_t if mktime() is unavailable

6. SqlServer Datetime field to time_t

7. Converting a struct tm in UTC to a time_t

8. convert type time_t to ASCII string?

9. converting a date string to time_t

10. Converting a struct tm in UTC to a time_t

11. Convert tm struct to time_t ?

12. Converting time_t to DATE

 

 
Powered by phpBB® Forum Software