• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4325
  • Last Modified:

Size of DateTime in C#.

Hi there;

1) Could you tell me the size/length of DateTime?
2) I need a 8 byte truncation of DateTime or something like that.

Kind regards.
0
jazzIIIlove
Asked:
jazzIIIlove
  • 4
  • 3
  • 3
  • +2
8 Solutions
 
viralypatelCommented:
you can use the sizeof operator to find the size of data types. Sample code here :
http://social.msdn.microsoft.com/Forums/en-IE/clr/thread/96747ab7-7d89-4846-9e83-46f71b8ccc66
0
 
AndyAinscowCommented:
DateTime is a struct with numbers of member functions.  Truncating it to 8 bytes is probably not a good (or useful) idea.

Exactly what do you want the 8 bytes for / what information should it contain ?
0
 
Todd GerbertIT ConsultantCommented:
Maybe you want DateTime.ToFileTime(), DateTime.ToBinary() or DateTime.ToOADate() - depending on what you're trying to accomplish.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jazzIIIloveAuthor Commented:
I need 8 bytes time_t equivalent for C#. I  don't mean to truncate if there is an equivalent for this.

I tried to use Tick method for this, but not sure whether it produces 8 bytes.

Kind regards.
0
 
Todd GerbertIT ConsultantCommented:
Why do you need a time_t?
0
 
AndyAinscowCommented:
There are a number of functions that convert DateTime (.NET) into types that can be used with unmanaged code.
A time_t is 64 bit and the method ToFileTime will supply a long (64 bit) equivalent.

Having said that I don't know if the two values are identical for the same date/time - they could be based on a different start date and have different intervals.

Again to repeat myself in my first comment - what do you want it for ?
0
 
jazzIIIloveAuthor Commented:
My C# application needs to create 8 bytes of time_t equivalent value as another C++ application of mine uses this value as input.

The processing takes place via a binary file that time_t resides as binary.

So should i use tick or above which should i use?

Kind regards.
0
 
AndyAinscowCommented:
I'd check if the ToFileTime of a .net DateTime does give the same value as a time_t with the same date and time values.  If it does then that is your value you require.

Now, if the values differ I would check if the difference is constant when using a different date and time to initialise - to see if the difference is just an initial date that differs.  That would then give you the value you needed to adjust by.

Worse case - there is no way to convert simply.  Then you have a look at another function eg. the ToOADate which would give you an alternative that you could process in the C++ part to make the time_t.
0
 
EDDYKTCommented:
DateTime now = DateTime.UtcNow;
DateTime old = new DateTime(1970, 1, 1);
int  a = Convert.ToInt32((now - old).TotalSeconds);
0
 
Todd GerbertIT ConsultantCommented:
I like EDDYKT's suggestion above (http:#a36910163) - just to elaborate a little bit:

A time_t is actually a 32-bit value (though in my crtdefs.h it's #define'd as 64-bit).  It is the number of seconds to have elapsed since January 1, 1970 12:00:00AM UTC, so you can just subtract the current date/time from 1/1/1970 at midnight to get a time_t; just make sure that both DateTime's are UTC before subtracting them (or both are local time).

For some reason this seemingly simple task feels like a difficult brain teaser to me, so I'm not 100% sure on this - maybe 99% ;)  I think this is correct:
class Program
{
	static void Main(string[] args)
	{
		DateTime dt = DateTime.Now;
		long time_t = DateTimeToTimeT(dt);

		Console.WriteLine("Current time_t: {0}", time_t);

		Console.ReadKey();
	}

	static long DateTimeToTimeT(DateTime time)
	{
		DateTime epoch = new DateTime(1970, 1, 1, 00, 00, 00, DateTimeKind.Utc);
		DateTime d = epoch.ToLocalTime();
		if (time.Kind == DateTimeKind.Local)
			return (long)(time.ToUniversalTime() - epoch).TotalSeconds;
		else
			return (long)(time - epoch).TotalSeconds;
	}

}

Open in new window


I also found this Knowledge Base article: http://support.microsoft.com/kb/167296/.  That article describes going from a time_t to a FILETIME, but here's the reverse of the steps in that article:
class Program
{
	static void Main(string[] args)
	{
		Console.WriteLine("Current time_t: {0}",
			FileTimeToUnixTime(DateTime.Now.ToFileTime()));
		Console.ReadKey();
	}

	static long FileTimeToUnixTime(long filetime)
	{
		return (filetime / 10000000) - 11644473600;
	}

}

Open in new window


Both methods produce the same results.
0
 
Todd GerbertIT ConsultantCommented:
Or, if you wanna be all fancy-pants, you can write an extension method so you can just call DateTime.ToUnixTime():

using System;

class Program
{
	static void Main(string[] args)
	{
		long time_t = DateTime.Now.ToUnixTime();

		Console.WriteLine("Current time_t: {0}", time_t);
		Console.ReadKey();
	}
}

public static class DateTimeExtensions
{
	public static long ToUnixTime(this DateTime time)
	{
		return (time.ToFileTime() / 10000000) - 11644473600;
	}
}

Open in new window

0
 
jazzIIIloveAuthor Commented:
Hmm, this question's comments exceed my expectations absolutely....

Ok, btw, I got other C# questions, mind looking at :)

Kind regards.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now