Help w/conversion to Unix Epoch DateTime in code

  • Thread starter Thread starter Tony!
  • Start date Start date
T

Tony!

I need to take the DateTime.Now value and convert it to a value like
1238965990

Essentially, need to mimic the conversion on this web site using C#
code, so I can create update statemens for rrdtool (which is a whole
'nother thing)

http://www.esqsoft.com/javascript_examples/date-to-epoch.htm



I've tried the below examples , but I don't get the value like
########## in CDT that I need for rrdtool. .


DateTime value = DateTime.Now;
TimeSpan span = (value - new DateTime(1970, 1, 1, 0, 0, 0,
0).ToUniversalTime());
TimeSpan spann = (value - new DateTime(1970, 1, 1, 0, 0, 0,
0).ToLocalTime());


Any code snippets/help would be appreciated.. ! Thanks!

Tony!
 
Tony! said:
I need to take the DateTime.Now value and convert it to a value like
1238965990

Essentially, need to mimic the conversion on this web site using C#
code, so I can create update statemens for rrdtool (which is a whole
'nother thing)

http://www.esqsoft.com/javascript_examples/date-to-epoch.htm

Try:

private static long k = (new DateTime(1970, 1, 1, 0, 0, 0) +
TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now)).Ticks;
public static int N2U(DateTime dt)
{
return (int)((dt.Ticks - k) / 10000000);
}
public static DateTime U2N(int t)
{
return new DateTime(t * 10000000L + k);
}

Arne
 
Try:

private static long k = (new DateTime(1970, 1, 1, 0, 0, 0) +
TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now)).Ticks;
public static int N2U(DateTime dt)
{
return (int)((dt.Ticks - k) / 10000000);
}
public static DateTime U2N(int t)
{
return new DateTime(t * 10000000L + k);
}

Arne


This works great!
Thank you very much!! :)

Tony!
 
Tony! said:
This works great!

I should note that the code assumes that the Now and the times being
converted are the same regarding summer time or not.

But it can become extremely messy around the time of switching to and
from summer time.

Arne
 
Back
Top