469,358 Members | 1,478 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

Converting C# DateTime field into Native C object

Hello everyone,

I am working on a project when I have to convert a C# program into
Native C for speed purposes. I am using a .dll that creates a DateTime
object. Obviously, there is not DateTime object in Native C, but I
would like to know if there is a work-around to parse out the
day/month/year from this DateTime object. Any ideas?

Mar 23 '06 #1
6 6378

minboymike wrote:
Hello everyone,

I am working on a project when I have to convert a C# program into
Native C for speed purposes. I am using a .dll that creates a DateTime
object. Obviously, there is not DateTime object in Native C, but I
would like to know if there is a work-around to parse out the
day/month/year from this DateTime object. Any ideas?


There's no such thing as "Native C". Do you mean ISO/ANSI Standard C?

If you want an answer to this, this group actually would be the right
place, had you went to the trouble of telling us what is a DateTime
object, so we can suggest C equivavalent.

If you specify what you need, there surely is a way of doing it in C.

--
BR, Vladimir

Mar 23 '06 #2
Hello,

The DateTime object i am speaking about is found under .NET.

Here is a link:
http://msdn.microsoft.com/library/de...classtopic.asp

Basically, as I said before, I am writing a C program and want to parse
out the day/month/year from this DateTime object, but do not see any
means to do this in C. The time.h library uses time_t object/struct,
but when converting, there is a tyep-mismatch (obviously), causing the
time_t object i created to be set to deafult (1/1/1970).

Mar 23 '06 #3

minboymike wrote:
Hello,

The DateTime object i am speaking about is found under .NET.

Here is a link:
http://msdn.microsoft.com/library/de...classtopic.asp

Basically, as I said before, I am writing a C program and want to parse
out the day/month/year from this DateTime object, but do not see any
means to do this in C. The time.h library uses time_t object/struct,
but when converting, there is a tyep-mismatch (obviously), causing the
time_t object i created to be set to deafult (1/1/1970).


If you're looking to read the binary representation of this (i.e. you
are given a pointer to it), then you really need to know how .NET
stores it. Knowing that, you can construct a C `struct` that matches,
and read it out, or you could just walk through it byte by byte and
reconstruct the fields. This is going to be very non-portable, not
least for the possibility that the raw binary representation in .NET
may change.

You may be better off writing the fields of interest (maybe all) into a
text file, or some file of known, and well defined type, from your .NET
code, and read it out from your C code. For "file" you may substitute
memory buffer as well. NB, I'm suggesting that you design and document
the exchange format used, so it's under your control.

--
BR, Vladimir

Mar 23 '06 #4
Thanks for such a quick reply....

I understand the approach you are coming from, but unfortunately, had I
had the option to write those specific fields to a file, then read them
into my C program, than i wouldn't have the problem to begin with.
This problem of type mis-matching between .NET object DATETIME and C
seems to be causing the most problems for me. Thanks for giving me your
2 cents! Much appreciated.

Mar 23 '06 #5
minboymike wrote:
Hello,

The DateTime object i am speaking about is found under .NET.

Here is a link:
http://msdn.microsoft.com/library/de...classtopic.asp

Basically, as I said before, I am writing a C program and want to parse
out the day/month/year from this DateTime object, but do not see any
means to do this in C. The time.h library uses time_t object/struct,
but when converting, there is a tyep-mismatch (obviously), causing the
time_t object i created to be set to deafult (1/1/1970).


Get the DateTime.Ticks Property and use that to construct a time_t
object. Either pass the long, or an object that contains only this long.
Mar 23 '06 #6
Thanks for the reply!

I found a work around to my problem. The sample code provided with the
..dll I was implementing actually uses a COleDateTime object to store
the DateTime object I was looking at. Simply #include "ATLComTime.h"
did the trick! Thanks for everyone's help on this

Mar 23 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Carl | last post: by
8 posts views Thread by Mika M | last post: by
18 posts views Thread by Dirk Hagemann | last post: by
11 posts views Thread by Keith Hughitt | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.