1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41:
| Tapani Leppänen <leppanen_tap...@pine.sahako.fi> wrote: >Hi, >Is there an easy ( or any ) way to convert C-type time_t to >Delphi-type TDateTime and vice versa ??? >TIA, >Tapsa >-- >Tapani Leppanen leppanen_tap...@pine.sahako.fi >Heinola Sawmill Machinery Inc. Automation
I'll take a wild shot at it:
time_t is an unsigned long representing seconds elapsed since 1970.
TDateTime is a decimal representation of time, with the value to the left of the decimal being a count of days elapsed since who knows when, and the value to the right of the decimal being a fraction of a day.
First, we'll need a reference: create a TDateTime variable for 0:00:00, 1970, using, I believe, StrToDate, and call that variable, say, dt1970.
To convert from a TDateTime variable to time_t, subtract dt1970 from your TDateTime variable, multiply by 24*60*60 (the number of seconds in a day), and cast to an unsigned long (i.e., a time_t).
To convert from a time_t variable to a TDateTime, cast your time_t variable to a TDateTime, divide by 24*60*60, and add dt1970.
That ought to do the trick.
P.S. ( If you're attempting to do conversions for dates prior to 1970, you may need to consider a more advanced algorithm). |