(BCB5) TDateTime conversion


I am looking for functions which can convert from a TDateTime to a time_t and vice versa.
Please can someone help me ?


Who is Participating?
LoungeLizardConnect With a Mentor Commented:
Well, TDateTime is effectively a double, indicating the number of days since 30/12/1899 00:00:00 and time_t is an long int that indicates the number of seconds since 1/1/1970 00:00:00 which means there is a constant difference between them. Just figure out what that is (use Excel or whatever) and use that in conversions.
The following example converts a time_t value to a file time, using the Int32x32To64 function.

void TimetToFileTime( time_t t, LPFILETIME pft )
    LONGLONG ll = Int32x32To64(t, 10000000) + 116444736000000000;
    pft->dwLowDateTime = (DWORD) ll;
    pft->dwHighDateTime = ll >>32;
After you have obtained a file time, you can convert this value to system time using the FileTimeToSystemTime function.

Then you can use the FileDateToDateTime to get your TDateTime.

Good luck!!

zaroblanAuthor Commented:
Are there no available functions to do this without needing to use three levels of indirection ?
7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

zaroblanAuthor Commented:
// According to the online help "time_t" is just a "long" which contains
// the number of seconds since 1st Jan 1970 0:00:00 GMT. And it says that
// TDateTime has a constructor which takes a double representing the number
// of days since 31st Dec 1899.

const int EpochDiff = 25569; // days between 30/12/1899 and 01/01/1970
const int SecsInDay = 86400; // number of seconds in a day

TDateTime TimeT_TDateTime(time_t Convertee)
  TDateTime Result(((double)Convertee) / SecsInDay + EpochDiff);
  return Result;

// Now to reverse this operation you can cast a TDateTime into a
// double and then apply the reverse mathematics:

time_t TDateTime_TimeT(TDateTime Convertee)
  time_t Result((long)((((double)Convertee) - EpochDiff) * SecsInDay));
  return Result;
Yup, that's what I meant.
zaroblanAuthor Commented:
Thanks for the pointer in the right direction ... would have been rated as excellent if you had supplied a code sample :)
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.

All Courses

From novice to tech pro — start learning today.