Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

(BCB5) TDateTime conversion

Posted on 2002-03-19
6
Medium Priority
?
1,324 Views
Last Modified: 2012-06-27
Greetings,

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

Thanks,

Robert
0
Comment
Question by:zaroblan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6881670
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!!

         
0
 

Author Comment

by:zaroblan
ID: 6881777
Are there no available functions to do this without needing to use three levels of indirection ?
0
 
LVL 2

Accepted Solution

by:
LoungeLizard earned 300 total points
ID: 6882012
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.
0
Technology Partners: 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!

 

Author Comment

by:zaroblan
ID: 6882041
// 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;
}
0
 
LVL 2

Expert Comment

by:LoungeLizard
ID: 6882072
Yup, that's what I meant.
0
 

Author Comment

by:zaroblan
ID: 6882092
Thanks for the pointer in the right direction ... would have been rated as excellent if you had supplied a code sample :)
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question