|
Boost Users : |
Subject: Re: [Boost-users] [Boost] [DateTime] dividing a time_period in Ntime_periods
From: Pablo Madoery (madoerypablo_at_[hidden])
Date: 2014-10-02 10:48:11
Thanks !
On Thu, Oct 2, 2014 at 11:04 AM, Igor Mironchik <igor.mironchik_at_[hidden]>
wrote:
> And one more... If you use C++11 then better to use emplace_back()
> instead of push_back()...
>
> *From:* Igor Mironchik <igor.mironchik_at_[hidden]>
> *Sent:* Thursday, October 02, 2014 5:02 PM
> *To:* boost-users_at_[hidden]
> *Subject:* Re: [Boost-users] [Boost] [DateTime] dividing a time_period in
> Ntime_periods
>
> Hi.
>
> Try this:
>
> list< time_period > splitTimePeriod( time_period timePeriod, int n )
>
> {
>
> const time_duration equalPeriod =
>
> ( timePeriod.end() - timePeriod.begin() ) / n;
>
>
> list< time_period > timePeriods;
>
>
> // tiempo de comienzo del primer perÃodo de tiempo
>
> ptime tpBegin = timePeriod.begin();
>
>
> // tiempo de fin del último perÃodo de tiempo
>
> const ptime tpLastEnd = timePeriod.end();
>
>
>
> for( int i = 0; i < n; ++i )
>
> {
>
> // tiempo de fin del perÃodo de tiempo
>
> ptime tpEnd;
>
>
> if( i == n - 1 )
>
> tpEnd = tpLastEnd;
>
> else
>
> {
>
> tpEnd = tpBegin + equalPeriod;
>
>
>
> // guarda para evitar que por cuestiones
>
> // de redondeo, el tiempo final de un perÃodo
>
> // sea mayor que el pasado inicialmente
>
> // como argumento de entrada
>
> if( tpEnd > tpLastEnd )
>
> tpEnd = tpLastEnd;
>
> }
>
>
> time_period p( tpBegin, tpEnd );
>
>
> // se inserta perÃodo de tiempo en la lista
>
> timePeriods.push_back( p );
>
>
> // se actualiza el comienzo del siguiente perÃodo de tiempo
>
> tpBegin = tpEnd;
>
> }
>
>
> return timePeriods;
>
> }
>
>
>
> It should be a little be more efficient.
>
>
>
> And I think that this is excess:
>
>
>
> // guarda para evitar que por cuestiones
>
> // de redondeo, el tiempo final de un perÃodo
>
> // sea mayor que el pasado inicialmente
>
> // como argumento de entrada
>
> if( tpEnd > tpLastEnd )
>
> tpEnd = tpLastEnd;
>
>
> *From:* Pablo Madoery <madoerypablo_at_[hidden]>
> *Sent:* Thursday, October 02, 2014 4:38 PM
> *To:* boost-users_at_[hidden]
> *Subject:* Re: [Boost-users] [Boost] [DateTime] dividing a time_period in
> Ntime_periods
>
> I have changed the method to this:
>
> list<time_period> splitTimePeriod(time_period timePeriod, int n)
> {
> time_duration equalPeriod = (timePeriod.end() - timePeriod.begin()) / n;
>
> list<time_period> timePeriods;
>
> // tiempo de comienzo del primer perÃodo de tiempo
> ptime tpBegin = timePeriod.begin();
>
> // tiempo de fin del último perÃodo de tiempo
> ptime tpLastEnd = timePeriod.end();
>
> for (int i = 0; i < n; i++)
> {
>
> // tiempo de comienzo del siguiente perÃodo de tiempo
> ptime tpNextBegin = tpBegin + equalPeriod;
>
> // tiempo de fin del perÃodo de tiempo
> ptime tpEnd;
>
> if (i == n - 1)
> {
> tpEnd = tpLastEnd;
> }
> else
> {
> tpEnd = dateTime::decrement(tpNextBegin, 1);
>
> // guarda para evitar que por cuestiones
> // de redondeo, el tiempo final de un perÃodo
> // sea mayor que el pasado inicialmente
> // como argumento de entrada
> if (tpEnd > tpLastEnd)
> {
> tpEnd = tpLastEnd;
> }
> }
>
> time_period p(tpBegin, tpEnd);
>
> // se inserta perÃodo de tiempo en la lista
> timePeriods.push_back(p);
>
> // se actualiza el comienzo del siguiente perÃodo de tiempo
> tpBegin = tpNextBegin;
> }
>
> return timePeriods;
> }
>
> ############################################################
> I didn't know I can make these operations to time_duration:
> time_duration equalPeriod = (timePeriod.end() - timePeriod.begin()) / n;
>
> Now it works faster.
>
> On Thu, Oct 2, 2014 at 10:21 AM, Pablo Madoery <madoerypablo_at_[hidden]>
> wrote:
> I think the time_duration s will be equal, but the time_periods will be
> something like this:
> 2014-Jan-01 00:00:00 -> 2014-Jan-01 00:00:02.999999
> 2014-Jan-01 00:00:03 -> 2014-Jan-01 00:00:05.999999
> 2014-Jan-01 00:00:06 -> 2014-Jan-01 00:00:08.999999
> 2014-Jan-01 00:00:09 -> 2014-Jan-01 00:00:11.999999
> 2014-Jan-01 00:00:12 -> 2014-Jan-01 00:00:14.999999
> 2014-Jan-01 00:00:15 -> 2014-Jan-01 00:00:17.999999
> 2014-Jan-01 00:00:18 -> 2014-Jan-01 00:00:21
>
> when calling the method like this
>
> ptime p1 = dateTime::toPtime("2014-1-1 00:00:00");
> ptime p2 = dateTime::toPtime("2014-1-1 00:00:21");
>
> time_period tp(p1,p2);
>
> list<time_period> l = dateTime::splitTimePeriod(tp, 7);
> list<time_period>::iterator i = l.begin();
> for(; i!= l.end(); ++i)
> {
> cout<<i->begin()<<" -> "<<i->end()<<endl;
> }
>
> On Thu, Oct 2, 2014 at 10:13 AM, Leon Mlakar <leon_at_[hidden]> wrote:
> On 02/10/14 15:09, Pablo Madoery wrote:
> Hi, I want to make a method which divides a time_period in N time periods,
> something like this:
>
> list<time_period> splitTimePeriod(time_period timePeriod, int n);
>
> I actually have made it and although it seems to work it results very
> inefficient regarding the time it took to divide a large time_period.
>
> The method I did is this:
>
> list<time_period> splitTimePeriod(time_period timePeriod, int n)
> {
> double secsTime = dateTime::diffSeconds(timePeriod.end(),
> timePeriod.begin());
>
> // tiempo en segundos de cada perÃodo de tiempo
> double ti = secsTime / n;
>
> list<time_period> timePeriods;
>
> // tiempo de comienzo del primer perÃodo de tiempo
> ptime tpBegin = timePeriod.begin();
>
> // tiempo de fin del último perÃodo de tiempo
> ptime tpLastEnd = timePeriod.end();
>
> for (int i = 0; i < n; i++)
> {
> int tUnits = ti * time_duration::ticks_per_second();
>
> // tiempo de comienzo del siguiente perÃodo de tiempo
> ptime tpNextBegin = dateTime::increment(tpBegin, tUnits);
>
> // tiempo de fin del perÃodo de tiempo
> ptime tpEnd;
>
> if (i == n - 1)
> {
> tpEnd = tpLastEnd;
> }
> else
> {
> tpEnd = dateTime::decrement(tpNextBegin, 1);
>
> // guarda para evitar que por cuestiones
> // de redondeo, el tiempo final de un perÃodo
> // sea mayor que el pasado inicialmente
> // como argumento de entrada
> if (tpEnd > tpLastEnd)
> {
> tpEnd = tpLastEnd;
> }
> }
>
> time_period p(tpBegin, tpEnd);
>
> // se inserta perÃodo de tiempo en la lista
> timePeriods.push_back(p);
>
> // se actualiza el comienzo del siguiente perÃodo de tiempo
> tpBegin = tpNextBegin;
> }
>
> return timePeriods;
> }
>
> Other methods I use are these:
>
> double diffSeconds(const ptime &dateA, const ptime &dateB)
> {
> time_duration diff = dateA - dateB;
> return diff.total_seconds();
> }
>
> ptime increment(const ptime &date, int n)
> {
> time_iterator it(date, dateTime::unit);
> for (int i = 0; i < n; i++)
> {
> ++it;
> }
> return *it;
> }
>
> ptime decrement(const ptime &date, int n)
> {
> time_iterator it(date, dateTime::unit);
> for (int i = 0; i < n; i++)
> {
> --it;
> }
> return *it;
> }
>
> const time_duration unit = microseconds(1);
>
> #################################################
> The question is: is there a better way to accomplish this?
>
> If you are splitting a time period into N periods of equal duration,
> wouldn't it be enough to return just one time period? The other n-1 are
> going to be equal, except perhaps for the last.
>
> Leon
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
>
>
> ------------------------------
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net