How can I convert a COleDateTime::GetCurrentTime() to a unix time stamp or more specifically I want to convert the value so I can use it in PHP with the date() function?
-
Do you mean the windows/atl/mfc class? http://msdn.microsoft.com/en-us/library/k2cfwb55%28VS.80%29.aspx If so, how does this relate to php? (it's a bit strange that this question has a php but not a win32 tag on it, isn't it?) – VolkerK Feb 19 '10 at 20:36
-
I think so. I am parsing xml data that was produced by a cpp app using that GetCurrentTime() method, so in PHP I need to take that value and convert it to something I can use in PHP. – Scott Szretter Feb 19 '10 at 21:03
-
Can you give an example from an actual xml document? – VolkerK Feb 19 '10 at 21:06
-
Ok, maybe there is additional code somewhere else, but the ultimate output to the xml file is: 40224.00000000 which is supposedly: 2010-02-15 12:00 AM – Scott Szretter Feb 21 '10 at 13:41
3 Answers
Here's a simple Python function to convert COleDateTime values to Python datetime objects. If nothing else, you can use this to convert the COleDateTime to a POSIX time format for ingestion by PHP. Assume your COleDateTime value (40224.00000000 in the original case) is passed into a floating point value "dateval".
def oledatetime_to_datetime(dateval):
from datetime import datetime, timedelta
import math
basedate = datetime(year=1899, month=12, day=30, hour=0, minute=0)
parts = math.modf(dateval)
days = timedelta(parts[1])
day_frac = timedelta(abs(parts[0]))
return basedate + days + day_frac
I just create a base datetime at the start date and time specified in the MSDN documentation (1899-12-30 @ 0:00) and add timedeltas for the number of days and partial days. It doesn't deal with timezones, but neither does COleDateTime (so far as I know). If nothing else it works for your test case, returning a Python datetime object for the date 2010-02-15 and time 0:00.

- 280,126
- 43
- 390
- 441

- 3,363
- 1
- 16
- 15
COleDateTime::GetCurrentTime()
returns an object, it doesn't output a string as far as I can tell in the MSDN Documentation. If you can provide an example of the string you are getting in the XML, it would be much easier to provide an answer for you.

- 2,846
- 2
- 22
- 16
That sounds like the DATE type:
The DATE type is implemented using an 8-byte floating-point number. Days are represented by whole number increments starting with 30 December 1899, midnight as time zero. Hour values are expressed as the absolute value of the fractional part of the number.
test code:
COleDateTime cdt(2010, 2, 15, 0, 0, 0); // 2010-02-15 12:00 AM
DATE d = cdt; // using COleDateTime::operator DATE
printf("%f", d);
prints 40224.000000
.
I don't think there is a built-in function to convert this back to any "native" php date/time value (could be wrong though).
edit: A (probably naive) starting point for a conversion function...
some sample data create by
LPCTSTR data[] = {
_T("1976-03-27 05:54:00"),
_T("1984-04-01 11:55:55"),
_T("1996-01-25 08:30:00"),
_T("2000-01-01 08:30:00"),
_T("2010-02-14 00:00:00"),
_T("2010-02-14 23:59:59"),
_T("2010-02-15 00:00:00"),
_T("2010-02-15 00:00:01"),
_T("2010-02-23 15:30:00"),
NULL
};
COleDateTime cdt;
for(int i=0; data[i]; i++) {
if ( !cdt.ParseDateTime(data[i]) ) {
_tprintf(_T("%s: error\n"), data[i]);
}
else {
_tprintf(_T("'%s'=>'%f'\n"), data[i], (DATE)cdt);
}
}
lead to
function DATE2unix($d) {
static $jd2DATE_offset = 2415019;
$date = intval($d);
$time = fmod($d, 1);
$ts = jdtounix($date+$jd2DATE_offset) + round($time*86400);
return $ts;
}
$data = array(
'1976-03-27 05:54:00'=>'27846.245833',
'1984-04-01 11:55:55'=>'30773.497164',
'1996-01-25 08:30:00'=>'35089.354167',
'2000-01-01 08:30:00'=>'36526.354167',
'2010-02-14 00:00:00'=>'40223.000000',
'2010-02-14 23:59:59'=>'40223.999988',
'2010-02-15 00:00:00'=>'40224.000000',
'2010-02-15 00:00:01'=>'40224.000012',
'2010-02-23 15:30:00'=>'40232.645833'
);
foreach($data as $target=>$d ) {
$ts = DATE2unix($d);
$date = gmdate('Y-m-d H:i:s', $ts);
echo 'd=', $d, ' | target=', $target, ' | date=', $date, ' : ', ($target===$date) ? 'ok':'error', "\n";
}
which prints
d=27846.245833 | target=1976-03-27 05:54:00 | date=1976-03-27 05:54:00 : ok
d=30773.497164 | target=1984-04-01 11:55:55 | date=1984-04-01 11:55:55 : ok
d=35089.354167 | target=1996-01-25 08:30:00 | date=1996-01-25 08:30:00 : ok
d=36526.354167 | target=2000-01-01 08:30:00 | date=2000-01-01 08:30:00 : ok
d=40223.000000 | target=2010-02-14 00:00:00 | date=2010-02-14 00:00:00 : ok
d=40223.999988 | target=2010-02-14 23:59:59 | date=2010-02-14 23:59:59 : ok
d=40224.000000 | target=2010-02-15 00:00:00 | date=2010-02-15 00:00:00 : ok
d=40224.000012 | target=2010-02-15 00:00:01 | date=2010-02-15 00:00:01 : ok
d=40232.645833 | target=2010-02-23 15:30:00 | date=2010-02-23 15:30:00 : ok
Though these 9 samples seem to work ok I'm not sure about intval()/round() and whether there are other conventions the php code must follow. If you're going to use this function, test it And then test it again with a lot more samples.
And keep in mind that this is limited to the unix timestamp range which is smaller than the range of COleDateTime.

- 95,432
- 20
- 163
- 226
-
I would settle for anything in php that would allow me to work with it - something that would let me take 40224.000000 and convert it to a date string, unix time stamp, etc. once I have it in one of those formats I can convert it from there...? – Scott Szretter Feb 23 '10 at 11:30