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.
LVL 12
jazzIIIloveAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
AndyAinscowFreelance programmer / ConsultantCommented:
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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
AndyAinscowFreelance programmer / ConsultantCommented:
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
AndyAinscowFreelance programmer / ConsultantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.