Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

boost::posix_time::time_duration fractional seconds

250 views
Skip to first unread message

Christopher Pisz

unread,
Nov 18, 2014, 5:13:39 PM11/18/14
to
I wonder if I could gleam some advise on how to get the milliseconds out
of a time duration. Not the total milliseconds, mind you. Just the
milliseconds part of the time.

They define a method to get fractional seconds, so I am a bit boggled on
what I would write to give milliseconds (or the closest approximation
given the resolution)

I imagine I would get something like .2374 in one machine and .24 on
another, but I am not sure.






Brian Waldrop

unread,
Nov 18, 2014, 5:24:19 PM11/18/14
to
Christopher Pisz wrote:

> I wonder if I could gleam some advise on how to get the milliseconds out
> of a time duration. Not the total milliseconds, mind you. Just the
> milliseconds part of the time.

I guess they already are given in ms, so make sure you know the size of
the register 32 or 64 bits, then out mask only the LSB part or modulus 1000

Why the need of boost?

Öö Tiib

unread,
Nov 18, 2014, 5:29:01 PM11/18/14
to
On Wednesday, 19 November 2014 00:13:39 UTC+2, Christopher Pisz wrote:
> I wonder if I could gleam some advise on how to get the milliseconds out
> of a time duration. Not the total milliseconds, mind you. Just the
> milliseconds part of the time.
>
> They define a method to get fractional seconds, so I am a bit boggled on
> what I would write to give milliseconds (or the closest approximation
> given the resolution)

Does not something like that work:
int msec = (duration.total_nanoseconds() % 1000000000)/1000000;

?

> I imagine I would get something like .2374 in one machine and .24 on
> another, but I am not sure.

You get count of milliseconds that way above, accuracy guarantees
depend how "real time" the clocks on your hardware are.

Christopher Pisz

unread,
Nov 18, 2014, 5:39:44 PM11/18/14
to
On 11/18/2014 4:28 PM, 嘱 Tiib wrote:
> On Wednesday, 19 November 2014 00:13:39 UTC+2, Christopher Pisz wrote:
>> I wonder if I could gleam some advise on how to get the milliseconds out
>> of a time duration. Not the total milliseconds, mind you. Just the
>> milliseconds part of the time.
>>
>> They define a method to get fractional seconds, so I am a bit boggled on
>> what I would write to give milliseconds (or the closest approximation
>> given the resolution)
>
> Does not something like that work:
> int msec = (duration.total_nanoseconds() % 1000000000)/1000000;
>
> ?

No, because I do not want to include the year, month, day, hour, minute,
or seconds parts of the date time. I want just the fractional seconds
part as milliseconds.

Unless I am misunderstanding the documentation for total_nanoseconds and
its brothers.


I would think they'd give fractional second as a double, so that I could
multiply it by 1000, but it looks like it comes back as an int64, so
that leaves me not knowing what metric of time the int64 represents.

Brian Waldrop

unread,
Nov 18, 2014, 5:40:03 PM11/18/14
to
Öö Tiib wrote:

>> They define a method to get fractional seconds, so I am a bit boggled
>> on what I would write to give milliseconds (or the closest
>> approximation given the resolution)
>
> Does not something like that work:
> int msec = (duration.total_nanoseconds() % 1000000000)/1000000;

This takes a lot of water to manage. Out masking the bits is much faster.

Melzzzzz

unread,
Nov 18, 2014, 5:43:02 PM11/18/14
to

Christopher Pisz

unread,
Nov 18, 2014, 5:43:28 PM11/18/14
to
As far as I can tell I can only get fractional second and it comes back
as a 64 bit int, so I have no idea of knowing the metric of time the int
represents.

They have a get resolution method that returns an enum too.


> Why the need of boost?
>

Because the gregorian date and posix_time classes have served me well in
the past and I am replacing a broken chunk of code where yet another
programmer wanted to custom make his own data structures for datetime
incorrectly.

I can't use std::chrono, because I am bound to msvc2010 for this
particular library.

I also despise the old Windows C representations for datetime.




Christopher Pisz

unread,
Nov 18, 2014, 5:48:02 PM11/18/14
to
On 11/18/2014 4:42 PM, Melzzzzz wrote:

> What's wrong with:
> http://pubs.opengroup.org/onlinepubs/7908799/xsh/systime.h.html

Well, "wrong" would be a matter of personal opinion, but my choice to
use boost's datetime classes is based on the following:

I've already got boost in for other libraries it provides
It has a much much better interface with much much more utility.
It supports UTC, local time, and conversions
It supports conversion to and from popular string formats
I like its style and error handling (exceptions)
I don't like C style :)





Öö Tiib

unread,
Nov 18, 2014, 6:00:36 PM11/18/14
to
On Wednesday, 19 November 2014 00:39:44 UTC+2, Christopher Pisz wrote:
> On 11/18/2014 4:28 PM, 嘱 Tiib wrote:
> > On Wednesday, 19 November 2014 00:13:39 UTC+2, Christopher Pisz wrote:
> >> I wonder if I could gleam some advise on how to get the milliseconds out
> >> of a time duration. Not the total milliseconds, mind you. Just the
> >> milliseconds part of the time.
> >>
> >> They define a method to get fractional seconds, so I am a bit boggled on
> >> what I would write to give milliseconds (or the closest approximation
> >> given the resolution)
> >
> > Does not something like that work:
> > int msec = (duration.total_nanoseconds() % 1000000000)/1000000;
> >
> > ?
>
> No, because I do not want to include the year, month, day, hour, minute,
> or seconds parts of the date time. I want just the fractional seconds
> part as milliseconds.
>
> Unless I am misunderstanding the documentation for total_nanoseconds and
> its brothers.

I am not sure how you understand the documentation but ...
Ok ... I did choose a bad time to joke then, sorry.

I try with little more serious logic lesson then ... perhaps?
Imagine that 'total_milliseconds()' returns 6789.

1) How many seconds was in that duration?
2) How many milliseconds was in it?
3) Does not remainder (operator %) let you to separate those seconds from
milliseconds?

Öö Tiib

unread,
Nov 18, 2014, 6:07:44 PM11/18/14
to
This must be is some counter-joke. 1000 happens to not be power of 2
so I don't know how to get the remainder of divison by 1000 using
some outmasking the bits trick.

Christopher Pisz

unread,
Nov 18, 2014, 6:17:36 PM11/18/14
to
I see what you are saying now.
Have to check the mins and maxes though and make sure no overflow is
possible.


Brian Waldrop

unread,
Nov 18, 2014, 6:32:33 PM11/18/14
to
Öö Tiib wrote:

> On Wednesday, 19 November 2014 00:40:03 UTC+2, Brian Waldrop wrote:
>> Öö Tiib wrote:
>>
>> >> They define a method to get fractional seconds, so I am a bit
>> >> boggled on what I would write to give milliseconds (or the closest
>> >> approximation given the resolution)
>> >
>> > Does not something like that work:
>> > int msec = (duration.total_nanoseconds % 1000000000)/1000000;
>>
>> This takes a lot of water to manage. Out masking the bits is much
>> faster.
>
> This must be is some counter-joke. 1000 happens to not be power of 2 so
> I don't know how to get the remainder of divison by 1000 using some
> outmasking the bits trick.

Very easy, just pick out only the significant bits, once you know their
location. Make it 2¹⁰, ten bits, the LSB of that would count msecs 100%
for sure.

Christopher Pisz

unread,
Nov 18, 2014, 7:22:09 PM11/18/14
to
True, but would this not be what they were going for, given their
methods? (I sub param for class member m_dateTime for example's sake)


const unsigned long long getMilliseconds(boost::posix_time::ptime
m_dateTime) const
{
const unsigned long long fractionOfSecond =
m_dateTime.time_of_day().fractional_seconds();

switch(m_dateTime.time_of_day().resolution())
{
case boost::date_time::time_resolutions::sec:
return 0;

case boost::date_time::time_resolutions::tenth:
return fractionOfSecond * 100;

case boost::date_time::time_resolutions::hundredth:
return fractionOfSecond * 10;

case boost::date_time::time_resolutions::milli:
return fractionOfSecond;

case boost::date_time::time_resolutions::ten_thousandth:
return fractionOfSecond / 10;

case boost::date_time::time_resolutions::micro:
return fractionOfSecond / 1000;

case boost::date_time::time_resolutions::nano:
return fractionOfSecond / 1000000;

default:
throw std::exception("Unknown fractional second resolution");
}
}

Öö Tiib

unread,
Nov 19, 2014, 1:36:36 AM11/19/14
to
What "significant bits"? Can you show us algorithm?

For example value of 'total' is 6789 (1101010000101 in binary), ten last
bits are 1010000101 (645 in decimal) but 6789%1000 is 789 (1100010101
in binary).

I can't see what algorithm is faster than 'total%1000'.

Öö Tiib

unread,
Nov 19, 2014, 1:50:13 AM11/19/14
to
OK, but I don't see what is the point? How it is better than ... for
example ...

return m_dateTime.time_of_day().fractional_seconds()*
(1000/m_dateTime.time_of_day().ticks_per_second());

Martijn Lievaart

unread,
Nov 19, 2014, 3:25:13 AM11/19/14
to
And:

Boost is way more portable.

M4

Christopher Pisz

unread,
Nov 19, 2014, 3:56:16 PM11/19/14
to
The latter returns 0 in my unit tests while the former gives the correct
answer. I don't follow why you are using ticks_per_second. Also keep in
mind the types and truncation.




0 new messages