Converting C# DateTime field into Native C object

Discussion in 'C Programming' started by minboymike, Mar 23, 2006.

  1. minboymike

    minboymike Guest

    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?
     
    minboymike, Mar 23, 2006
    #1
    1. Advertising

  2. 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
     
    Vladimir S. Oka, Mar 23, 2006
    #2
    1. Advertising

  3. minboymike

    minboymike Guest

    Hello,

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

    Here is a link:
    http://msdn.microsoft.com/library/d.../cpref/html/frlrfsystemdatetimeclasstopic.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).
     
    minboymike, Mar 23, 2006
    #3
  4. minboymike wrote:
    > Hello,
    >
    > The DateTime object i am speaking about is found under .NET.
    >
    > Here is a link:
    > http://msdn.microsoft.com/library/d.../cpref/html/frlrfsystemdatetimeclasstopic.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
     
    Vladimir S. Oka, Mar 23, 2006
    #4
  5. minboymike

    minboymike Guest

    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.
     
    minboymike, Mar 23, 2006
    #5
  6. minboymike wrote:
    > Hello,
    >
    > The DateTime object i am speaking about is found under .NET.
    >
    > Here is a link:
    > http://msdn.microsoft.com/library/d.../cpref/html/frlrfsystemdatetimeclasstopic.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.
     
    void * clvrmnky(), Mar 23, 2006
    #6
  7. minboymike

    minboymike Guest

    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
     
    minboymike, Mar 23, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    712
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    538
    Tim Peters
    Sep 9, 2003
  3. mp
    Replies:
    1
    Views:
    392
    John Machin
    Jul 28, 2006
  4. Martin
    Replies:
    0
    Views:
    338
    Martin
    Dec 27, 2008
  5. Replies:
    2
    Views:
    763
    M.-A. Lemburg
    Jan 6, 2009
Loading...

Share This Page