Convert boost::posix_time::ptime to Windows FILETIME

When writing platform independent libraries at work, I use boost posix_time as the primary mechanism to generate timestamps. But when integrating the platform independent libraries to Windows world, the interface requires everything to be converted Windows FILETIME.

Recall, Windows FILETIME is a 64 bit structure that represents the number of 100-nanosecond intervals since January 1, 1601 (UTC).

Boost posix_time library has an API called from_ftime<ptime>(FILETIME ft), where it can create a ptime object from a Windows FILETIME.

Strangely, it’s counterpart does not exist. In other word, there is no to_ftime.


I really dislike writing this type of basic time conversion routine. It has probably been done before, and I am probably reinventing the wheel (a common disease in my profession).

Believe it or not, I could not find a solution online. At least, I found out I am not the first person to want to do this.

Anyway, here’s one way to do it.

#include &lt;boost/date_time/posix_time/posix_time.hpp&gt;
#include &lt;boost/date_time/gregorian/gregorian.hpp&gt;
#include &lt;windows.h&gt;
#include &lt;boost/cstdint.hpp&gt;

FILETIME PtimeToFileTime(boost::posix_time::ptime const &amp;pt)
	// extract the date from boost::posix_time to SYSTEMTIME
	boost::gregorian::date::ymd_type ymd =;

	st.wYear = ymd.year;
	st.wMonth = ymd.month;
	st.wDay =;
	st.wDayOfWeek =;

	// Now extract the hour/min/second field from time_duration
	boost::posix_time::time_duration td = pt.time_of_day();
	st.wHour = static_cast&lt;WORD&gt;(td.hours());
	st.wMinute = static_cast&lt;WORD&gt;(td.minutes());
	st.wSecond = static_cast&lt;WORD&gt;(td.seconds());

	// Although ptime has a fractional second field, SYSTEMTIME millisecond
	// field is 16 bit, and will not store microsecond. We will treat this
	// field separately later.
	st.wMilliseconds = 0;

	// Convert SYSTEMTIME to FILETIME structure
	SystemTimeToFileTime(&amp;st, &amp;ft);

	// Now we are almost done. The FILETIME has date, and time. It is
	// only missing fractional second.

	// Extract the raw FILETIME into a 64 bit integer.
	boost::uint64_t _100nsSince1601 = ft.dwHighDateTime;
	_100nsSince1601 &lt;&lt;=32;
	_100nsSince1601 |= ft.dwLowDateTime;

	// Add in the fractional second, which is in microsecond * 10 to get
	// 100s of nanosecond
	_100nsSince1601 += td.fractional_seconds()*10;

	// Now put the time back inside filetime.
	ft.dwHighDateTime = _100nsSince1601 &gt;&gt; 32;
	ft.dwLowDateTime = _100nsSince1601 &amp; 0x00000000FFFFFFFF;

	return ft;

And here’s how I verified it.

  1. Create a ptime object, and convert it to FILETIME with the routine above.
  2. Then use from_ftime<ptime>(FILETIME ft) to convert the generated FILETIME into another ptime object.
  3. Verify that the two ptime object is identical.
boost::posix_time::ptime now =

FILETIME ft = PtimeToFileTime(now);

std::cout &lt;&lt; boost::posix_time::to_iso_extended_string(now) &lt;&lt; std::endl;

boost::posix_time::ptime clone =

std::cout &lt;&lt; boost::posix_time::to_iso_extended_string(clone) &lt;&lt; std::endl;


On a side note

The routine PtimeToFileTime does not validate its input.

The year_month_day() routine could contain invalid/uninitialized fields.

SystemTimeToFileTime could fail.

I will leave that as an exercise.

3 thoughts on “Convert boost::posix_time::ptime to Windows FILETIME

  1. Alex says:

    Exactly what I was looking for.
    Btw, all lines after “SystemTimeToFileTime(&st, &ft);” can be shortened to
    (boost::uint64_t&)ft += td.fractional_seconds()*10;

Leave a Reply

Your email address will not be published. Required fields are marked *