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

boost::posix_time

24 views
Skip to first unread message

Christopher Pisz

unread,
Jul 9, 2015, 6:50:55 PM7/9/15
to
Does anyone by chance know how to get the difference between two
datetimes expressed as a total number of weeks, using boost::posix_time?


I am trying to calculate things like "is the current week a week, that
meets the condition of, every 3rd week from 07/09/2015?"

so i figure something like
if( (currentdatetime - startdatetime).AsTotalWeeks() % 3 == 0)


--
I have chosen to troll filter/ignore all subthreads containing the
words: "Rick C. Hodgins", "Flibble", and "Islam"
So, I won't be able to see or respond to any such messages
---

Alf P. Steinbach

unread,
Jul 9, 2015, 8:36:35 PM7/9/15
to
On 10-Jul-15 12:50 AM, Christopher Pisz wrote:
> Does anyone by chance know how to get the difference between two
> datetimes expressed as a total number of weeks, using boost::posix_time?
>
>
> I am trying to calculate things like "is the current week a week, that
> meets the condition of, every 3rd week from 07/09/2015?"
>
> so i figure something like
> if( (currentdatetime - startdatetime).AsTotalWeeks() % 3 == 0)

I read "the difference between two datetimes expressed as a total number
of weeks" as "the difference between two datetimes, with the difference
expressed as a total number of weeks".

At the risk of stating the obvious, a week is always 7 days.

But you need general calendar functionality to find the number of days
between two dates. As of C++14 such calendar functionality is not
provided by the standard library. You can go with a DIY solution based
on the C library's [1]tm structure, which involves computing leap years
etc., see <url:
http://stackoverflow.com/questions/24403829/find-difference-between-two-dates-c>.
Or you can go with one of the two Boost calendar sub-libraries, or
perhaps platform specific functionality.

Cheers & hth.,

- Alf

Notes:
[1] tm can be converted to time_t e.g. via mktime, which can be
converted to std::chrono::whatever_clock via its from_time_t member
function. See Josuttis' "The C++ Standard Library: A Tutorial and
Reference" or the excerpt at <url: <url:
http://www.informit.com/articles/article.aspx?p=1881386&seqNum=2>.

--
Using Thunderbird as Usenet client, Eternal September as NNTP server.
0 new messages