2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
21 * @brief time related functions.
23 * @copyright Copyright © 2001-2008 by Intra2net AG
39 #include <sys/timeb.h>
40 #include <sys/syscall.h>
42 #include <timefunc.hxx>
46 // define missing POSIX.1b constants...
48 #ifndef CLOCK_REALTIME
49 #define CLOCK_REALTIME 0
51 #ifndef CLOCK_MONOTONIC
52 #define CLOCK_MONOTONIC 1
59 double prec_time(void)
66 ret=tb.time+(static_cast<float>(tb.millitm)/1000);
71 // converts ISO-DATE: 2003-06-13
72 time_t date_to_seconds(const std::string &date)
75 int year = -1, month = -1, day = -1;
77 string::size_type pos = date.find("-");
78 if (pos == string::npos)
81 istringstream in(string(date,0,pos));
85 string dstr(date, pos+1);
86 if ((pos = dstr.find("-")) == string::npos)
90 in.str(string(dstr, 0, pos));
95 in.str(string(dstr, pos+1));
98 if (year < 0 || month == -1 || day == -1)
102 memset(&tm_struct, 0, sizeof(struct tm));
103 tm_struct.tm_year = year;
104 tm_struct.tm_mon = month;
105 tm_struct.tm_mday = day;
106 tm_struct.tm_isdst = -1;
108 rtn = mktime (&tm_struct);
112 string make_nice_time(int seconds)
116 int days=seconds/86400;
120 split_daysec(seconds,&hours,&minutes,&seconds);
123 out << days << " " << i18n_plural("day", "days", days) << ", ";
126 out << setw(2) << hours << ':' << setw(2) << minutes << ':' << setw(2) << seconds;
131 string format_full_time(time_t seconds)
136 if (localtime_r((time_t *)&seconds, &ta) == NULL)
137 memset (&ta, 0, sizeof(struct tm));
139 strftime (buf, 49, "%d.%m.%Y %H:%M", &ta);
143 void seconds_to_hour_minute(int seconds, int *hour, int *minute)
147 while (seconds >= 3600) {
153 if (minute != NULL) {
155 while (seconds >= 60) {
163 * Split seconds into hours, minutes and seconds
164 * @param [in] daysec Seconds since start of day
165 * @param [out] outhours hours
166 * @param [out] outminutes minutes
167 * @param [out] outseconds seconds
169 void split_daysec(int daysec, int *outhours, int *outminutes, int *outseconds)
171 int hours=daysec/3600;
174 int minutes=daysec/60;
187 std::string output_hour_minute(int hour, int minute, bool h_for_00, int seconds)
191 if (hour >= 0 && hour < 10)
195 if (!h_for_00 || minute != 0 || seconds > 0)
198 if (minute >= 0 && minute < 10)
208 if (seconds > 0 && seconds < 10)
216 string get_month_name(unsigned char month)
221 rtn = i18n("January");
224 rtn = i18n("February");
242 rtn = i18n("August");
245 rtn = i18n("September");
248 rtn = i18n("October");
251 rtn = i18n("November");
254 rtn = i18n("December");
259 out << i18n("Illegal month:") << " " << month;
269 ** implementaion of Interval
274 * @brief clears the interval (make it empty).
276 void Interval::clear()
278 m_lower_bound = m_upper_bound = 0;
279 } // eo Interval::clear()
283 * @brief tests if there is some overlapping with another interval
284 * @param other the other interval
285 * @return @a true if the two intervals have a non empty intersection.
287 bool Interval::intersects(const Interval& other) const
290 // // other start within this:
291 (other.m_lower_bound >= m_lower_bound and other.m_lower_bound < m_upper_bound )
292 // // other end within this:
293 or (other.m_upper_bound > m_lower_bound and other.m_upper_bound <= m_upper_bound )
294 // // other contains this
295 or (other.m_lower_bound <= m_lower_bound and other.m_upper_bound >= m_upper_bound )
297 } // eo Interval::intersects(const Interval&)
301 * @brief tests if the current interval (fully) contains another one.
302 * @param other the other interval.
303 * @return @a true if the current interval fully contains the other interval.
305 bool Interval::contains(const Interval& other) const
307 return (other.m_lower_bound >= m_lower_bound)
308 and (other.m_upper_bound <= m_upper_bound)
310 } // eo Interval::contains(const Interval& other) const
314 ** implementation of Intervals:
318 Intervals::Intervals()
320 } // eo Intervals::Intervals
323 void Intervals::clear()
326 } // eo Intervals::clear()
329 * @brief tests if one of the intervals of the list intersects with the given interval.
330 * @param other the interval to check for intersection.
331 * @return @a true if there is an intersection.
333 bool Intervals::intersects(const Interval& other) const
335 for(const_iterator it= begin();
339 if ( it->intersects(other) )
345 } // eo Intervals::intersects(const Interval&) const
349 * @brief tests if we have at least one intersection with another Intervals instance.
350 * @param other the other instance.
351 * @return @a true if there is an intersection.
353 bool Intervals::intersects(const Intervals& other) const
355 for(const_iterator it= begin();
359 if ( other.intersects( *it ) )
365 } // eo Intervals::intersects(const Intervals&) const
369 * @brief adds a new interval to the list.
370 * @param new_frame the new interval.
372 * Adds the interval to the list and joins overlapping intervals.
374 * @internal complexity O(n).
376 void Intervals::add(const Interval& new_frame)
378 if (not new_frame.is_valid() or new_frame.empty())
380 // well... we will not insert invalid or empty frames!
383 for (IntervalList::iterator it= m_intervals.begin();
384 it != m_intervals.end();
387 Interval& current_frame = *it;
388 if ( new_frame.m_lower_bound > current_frame.m_upper_bound )
390 // new_frame begins later than current end; go on:
393 // at this point: the begin of the new frame is less then the current end.
394 // now let's determine how we can insert the new frame:
396 if ( new_frame.m_upper_bound < current_frame.m_lower_bound )
398 // new disjoint frame; insert it before the current frame:
399 m_intervals.insert( it, new_frame );
403 // at this point: the end of the new frame is >= current begin.
404 if ( new_frame.m_upper_bound <= current_frame.m_upper_bound )
406 // the end of the new frame is within our current frame; we need to combine
407 if (new_frame.m_lower_bound < current_frame.m_lower_bound)
409 // the new interval starts earlier; we need to adjust our current frame:
410 current_frame.m_lower_bound = new_frame.m_lower_bound;
411 current_frame.m_changed = true;
413 // NOTE no "else" part needed since in that case our current frame already
414 // contains the new one!
419 // at this point: end of new frame > end of current frame
420 // so we need to extend the current frame; at least the end.
421 // But we need to deal with intersects of following frames... *sigh*
423 // first the simple part: let's see if we need to move the start:
424 if ( new_frame.m_lower_bound < current_frame.m_lower_bound)
426 // yes, we need to move the start:
427 current_frame.m_lower_bound = new_frame.m_lower_bound;
428 current_frame.m_changed= true;
431 // now let's extend the end:
432 current_frame.m_upper_bound = new_frame.m_upper_bound;
433 current_frame.m_changed = true;
435 // well... let's walk through the following frames; looking for more joins...:
436 IntervalList::iterator it2 = it;
437 while( ++(it2=it) != m_intervals.end()
438 and current_frame.m_upper_bound >= it2->m_lower_bound
441 Interval next_frame= *it2;
442 if ( current_frame.m_upper_bound < next_frame.m_upper_bound )
444 // in this case our end is within the next frame.
446 current_frame.m_upper_bound = next_frame.m_upper_bound;
448 // and remove the next frame since the current frame contains it (now):
449 m_intervals.erase(it2);
454 // at this point: new frame starts later than the last frame ends
455 // append the new frame:
456 m_intervals.push_back( new_frame );
457 } // eo Intervals::add(const Interval&)
461 * @brief subtracts a time interval from the list.
462 * @param del_frame the time interval to subtract.
464 * removes the time interval from the list; cut off parts from or remove existing
465 * intervals if they overlap.
467 * @internal complexity O(n).
469 void Intervals::sub(const Interval& del_frame)
471 if (not del_frame.is_valid() or del_frame.empty() )
475 for (IntervalList::iterator it= m_intervals.begin();
476 it != m_intervals.end();
479 Interval& current_frame = *it;
480 if ( del_frame.m_lower_bound >= current_frame.m_upper_bound )
482 // del_frame begins later than current end; go on:
486 // at this point: the begin of the del frame is less then the current end.
487 if ( del_frame.m_upper_bound < current_frame.m_lower_bound )
489 // end is before our start; nothing to do.
492 // at this point: the end of the del frame is >= current begin.
493 if ( del_frame.m_upper_bound < current_frame.m_upper_bound )
495 // del frame end point is within our interval.
496 if ( del_frame.m_lower_bound > current_frame.m_lower_bound)
498 // the del frame is within our interval... we need to split:
499 m_intervals.insert(it, Interval( current_frame.m_lower_bound, del_frame.m_lower_bound ) );
501 // adjust start of current frame:
502 if (current_frame.m_lower_bound < del_frame.m_upper_bound)
504 current_frame.m_lower_bound= del_frame.m_upper_bound;
505 current_frame.m_changed= true;
510 // at this point the end of the del frame is >= current end
511 if ( del_frame.m_lower_bound > current_frame.m_lower_bound )
513 // a part of the current interval needs to be preserved..
515 current_frame.m_upper_bound= del_frame.m_lower_bound;
516 current_frame.m_changed= true;
517 // and continue with the next interval:
521 // at this point; the whole frame needs to be deleted..
522 if ( it == m_intervals.begin())
524 m_intervals.erase(it);
525 it= m_intervals.begin();
529 IntervalList::iterator it2= it++;
530 m_intervals.erase(it2);
533 } // eo Intervals::sub(const Interval&)
537 * @brief returns if we contain an interval.
538 * @param other the interval to check.
539 * @return @a true if we cover the given interval, too.
541 bool Intervals::contains(const Interval& other) const
543 for(const_iterator it= begin();
547 if ( it->contains( other ))
553 } // eo Intervals::contains(const Interval&) const
557 * @brief returns if we contain an exact interval.
558 * @param other the interval to check.
559 * @return @a true if we axactly contains the given interval.
561 * @note thsi differs from contain in the way, that we return only @a true
562 * iff we have the given interval in our list; not only cover it.
564 bool Intervals::contains_exact(const Interval& other) const
566 for(const_iterator it= begin();
576 } // eo Intervals::contains_exact(const Interval&)const
580 * @brief returns if we contain another interval combination.
581 * @param other the intervals to check.
582 * @return @a true if we cover the given intervals, too.
584 * @internal we rely on the fact that the lists are sorted and contain
585 * disjoint intervals.
587 * So this method has a complexity of O(n).
589 bool Intervals::contains(const Intervals& other) const
591 const_iterator my_it= begin();
592 const_iterator other_it= other.begin();
593 while( my_it != end() and other_it!= other.end() )
595 // seek the first interval which contains the lower bound of the current other interval
596 while (my_it != end()
597 and my_it->m_lower_bound > other_it->m_lower_bound
598 and other_it->m_lower_bound >= my_it->m_upper_bound
607 if (not my_it->contains( *other_it ))
609 // if we don't contain the current other; we're done:
612 //else check the next other interval:
615 return (other_it == other.end());
616 } // eo Intervals::contains(const Intervals&) const
620 * @brief combines to interval combinates for equality
621 * @param other the other instance.
622 * @return @a true if the other is equal to the current.
624 * @internal since the lists are sorted, we compare the interval lists.
625 * Thus we have a complexity of O(n).
627 bool Intervals::operator==(const Intervals& other) const
629 // since we keep sorted lists: just compare the lists :-)
630 return m_intervals == other.m_intervals;
631 } // eo Intervals::operator==(const Intervals&)
634 Intervals& Intervals::operator+=(const Interval& other)
638 } // eo operator+=(const Interval&)
641 Intervals& Intervals::operator-=(const Interval& other)
645 } // eo operator-=(const Interval&)
649 * @brief adds the intervals of a second instance to us.
650 * @param other the other instance.
651 * @return self reference (allow chaining).
653 * @internal since we do simple loops over the other and our intervals
654 * we have a complexity of O(n^2).
656 * @todo optimize if complexity becomes a problem.
658 Intervals& Intervals::operator+=(const Intervals& other)
660 for(const_iterator it= other.begin();
667 } // eo operator+=(const Intervals&)
671 * @brief subtracts the intervals of a second instance from us.
672 * @param other the other instance.
673 * @return self reference (allow chaining).
675 * @internal since we do simple loops over the other and our intervals
676 * we have a complexity of O(n^2).
678 * @todo optimize if complexity becomes a problem.
680 Intervals& Intervals::operator-=(const Intervals& other)
688 for(const_iterator it= other.begin();
696 } // eo operator-=(const Intervals&)
706 * @brief fetches the value from the monotonic clock source.
707 * @param[out] seconds the seconds.
708 * @param[out] nano_seconds the nano seconds.
709 * @return @a true if the clock was successfully read.
711 bool monotonic_clock_gettime(long int& seconds, long int& nano_seconds)
713 struct timespec tp[1];
714 int res= ::syscall(__NR_clock_gettime, CLOCK_MONOTONIC, tp);
718 nano_seconds= tp->tv_nsec;
721 } // eo monotonic_clock_gettime(long int&,long int&)
725 * @brief fetches the value from the monotonic clock source.
726 * @return the time since system start in nanoseconds, 0 if read was unsuccessful
728 long long monotonic_clock_gettime_nano()
731 long int nano_seconds;
734 if (monotonic_clock_gettime(seconds,nano_seconds))
745 * @brief fetches the value from the monotonic clock source.
746 * @param[out] seconds the seconds.
747 * @param[out] nano_seconds the nano seconds.
748 * @return @a true if the clock was successfully read.
750 bool realtime_clock_gettime(long int& seconds, long int& nano_seconds)
752 struct timespec tp[1];
753 int res= ::syscall(__NR_clock_gettime, CLOCK_REALTIME, tp);
757 nano_seconds= tp->tv_nsec;
760 } // eo realtime_clock_gettime(long int&,long int&)