Boost logo

Boost :

From: Jeff Garland (jeff_at_[hidden])
Date: 2003-09-03 08:32:40


On Tue, 02 Sep 2003 19:00:42 -0400, David Abrahams wrote
> The "fractional seconds" concept is undocumented. My guess it's
> something like:
>
> x.fractional_seconds() == x.ticks() % seconds(1).ticks()
>
> This needs to be nailed down.

Yep the docs don't say enough on this.

Basically, time durations are represented as a count at a compile-time
specified resolution. That is, a count of 1 can represent a millisecond,
nanosecond, femtosecond (just to pick one ;-), or whatever. So fractional
seconds is a generic way of handling the sub-second remainder of these without
having to provide every possible combination. That is, the fractional seconds
provides the count of 'whatever time units' that are left over after stripping
away the duration greater than a second. To really use fractional_seconds you
call the resolution traits by calling:

time_duration::rep_type::res_adjust()

This method provides you with the count of fractional seconds used by the time
duration. This little program might illustrate things...

//print_resolution.cpp
#include "boost/date_time/posix_time/posix_time.hpp"
#include <iostream>

//Must match with time_resolutions enum in date_time/time_defs.h
const char* const resolution_names[] = {"Second", "Deci", "Centi", "Milli",
"Ten_Thousanth", "Micro", "Nano"};

int
main()
{
  using namespace boost::posix_time;

  std::cout << "Resolution: "
            << resolution_names[time_duration::rep_type::resolution()]
            << " -- Ticks per second: "
            << time_duration::rep_type::res_adjust() << std::endl;
  
}

//output
Resolution: Micro -- Ticks per second: 1000000

> Also, the assymetry of those nice Construction by Count factories
> down to nanosec(x) with the accessors which only include units down
> to seconds() but not millisec()...nanosec() is disturbing and
> frankly inconvenient.

Ok, but I'm interested in your use case as once I have a time_duration I don't
normally care about a particular sub-second resolution. But now that I think
about it would seems like it might be possible to provide the inverse interface...

> BTW, why plural hours, minutes, seconds, but singular millisec,
> microsec, nanosec?

Yikes! They probably all should be plural. Problem is the abbreviated
'millisecs' doesn't sound right to me, so perhaps that's the reason....

Jeff


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk