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
40 #include <sys/timeb.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 string format_date(time_t seconds)
148 if (localtime_r((time_t *)&seconds, &ta) == NULL)
149 memset (&ta, 0, sizeof(struct tm));
151 strftime (buf, 49, "%d.%m.%Y", &ta);
155 void seconds_to_hour_minute(int seconds, int *hour, int *minute)
159 while (seconds >= 3600) {
165 if (minute != NULL) {
167 while (seconds >= 60) {
175 * Split seconds into hours, minutes and seconds
176 * @param [in] daysec Seconds since start of day
177 * @param [out] outhours hours
178 * @param [out] outminutes minutes
179 * @param [out] outseconds seconds
181 void split_daysec(int daysec, int *outhours, int *outminutes, int *outseconds)
183 int hours=daysec/3600;
186 int minutes=daysec/60;
199 std::string output_hour_minute(int hour, int minute, bool h_for_00, int seconds)
203 if (hour >= 0 && hour < 10)
207 if (!h_for_00 || minute != 0 || seconds > 0)
210 if (minute >= 0 && minute < 10)
220 if (seconds > 0 && seconds < 10)
228 string get_month_name(unsigned char month)
233 rtn = i18n("January");
236 rtn = i18n("February");
254 rtn = i18n("August");
257 rtn = i18n("September");
260 rtn = i18n("October");
263 rtn = i18n("November");
266 rtn = i18n("December");
271 out << i18n("Illegal month:") << " " << month;
281 ** implementaion of Interval
286 * @brief clears the interval (make it empty).
288 void Interval::clear()
290 m_lower_bound = m_upper_bound = 0;
291 } // eo Interval::clear()
295 * @brief tests if there is some overlapping with another interval
296 * @param other the other interval
297 * @return @a true if the two intervals have a non empty intersection.
299 bool Interval::intersects(const Interval& other) const
302 // // other start within this:
303 (other.m_lower_bound >= m_lower_bound and other.m_lower_bound < m_upper_bound )
304 // // other end within this:
305 or (other.m_upper_bound > m_lower_bound and other.m_upper_bound <= m_upper_bound )
306 // // other contains this
307 or (other.m_lower_bound <= m_lower_bound and other.m_upper_bound >= m_upper_bound )
309 } // eo Interval::intersects(const Interval&)
313 * @brief tests if the current interval (fully) contains another one.
314 * @param other the other interval.
315 * @return @a true if the current interval fully contains the other interval.
317 bool Interval::contains(const Interval& other) const
319 return (other.m_lower_bound >= m_lower_bound)
320 and (other.m_upper_bound <= m_upper_bound)
322 } // eo Interval::contains(const Interval& other) const
326 ** implementation of Intervals:
330 Intervals::Intervals()
332 } // eo Intervals::Intervals
335 void Intervals::clear()
338 } // eo Intervals::clear()
341 * @brief tests if one of the intervals of the list intersects with the given interval.
342 * @param other the interval to check for intersection.
343 * @return @a true if there is an intersection.
345 bool Intervals::intersects(const Interval& other) const
347 for(const_iterator it= begin();
351 if ( it->intersects(other) )
357 } // eo Intervals::intersects(const Interval&) const
361 * @brief tests if we have at least one intersection with another Intervals instance.
362 * @param other the other instance.
363 * @return @a true if there is an intersection.
365 bool Intervals::intersects(const Intervals& other) const
367 for(const_iterator it= begin();
371 if ( other.intersects( *it ) )
377 } // eo Intervals::intersects(const Intervals&) const
381 * @brief adds a new interval to the list.
382 * @param new_frame the new interval.
384 * Adds the interval to the list and joins overlapping intervals.
386 * @internal complexity O(n).
388 void Intervals::add(const Interval& new_frame)
390 if (not new_frame.is_valid() or new_frame.empty())
392 // well... we will not insert invalid or empty frames!
395 for (IntervalList::iterator it= m_intervals.begin();
396 it != m_intervals.end();
399 Interval& current_frame = *it;
400 if ( new_frame.m_lower_bound > current_frame.m_upper_bound )
402 // new_frame begins later than current end; go on:
405 // at this point: the begin of the new frame is less then the current end.
406 // now let's determine how we can insert the new frame:
408 if ( new_frame.m_upper_bound < current_frame.m_lower_bound )
410 // new disjoint frame; insert it before the current frame:
411 m_intervals.insert( it, new_frame );
415 // at this point: the end of the new frame is >= current begin.
416 if ( new_frame.m_upper_bound <= current_frame.m_upper_bound )
418 // the end of the new frame is within our current frame; we need to combine
419 if (new_frame.m_lower_bound < current_frame.m_lower_bound)
421 // the new interval starts earlier; we need to adjust our current frame:
422 current_frame.m_lower_bound = new_frame.m_lower_bound;
423 current_frame.m_changed = true;
425 // NOTE no "else" part needed since in that case our current frame already
426 // contains the new one!
431 // at this point: end of new frame > end of current frame
432 // so we need to extend the current frame; at least the end.
433 // But we need to deal with intersects of following frames... *sigh*
435 // first the simple part: let's see if we need to move the start:
436 if ( new_frame.m_lower_bound < current_frame.m_lower_bound)
438 // yes, we need to move the start:
439 current_frame.m_lower_bound = new_frame.m_lower_bound;
440 current_frame.m_changed= true;
443 // now let's extend the end:
444 current_frame.m_upper_bound = new_frame.m_upper_bound;
445 current_frame.m_changed = true;
447 // well... let's walk through the following frames; looking for more joins...:
448 IntervalList::iterator it2 = it;
449 while( ++(it2=it) != m_intervals.end()
450 and current_frame.m_upper_bound >= it2->m_lower_bound
453 Interval next_frame= *it2;
454 if ( current_frame.m_upper_bound < next_frame.m_upper_bound )
456 // in this case our end is within the next frame.
458 current_frame.m_upper_bound = next_frame.m_upper_bound;
460 // and remove the next frame since the current frame contains it (now):
461 m_intervals.erase(it2);
466 // at this point: new frame starts later than the last frame ends
467 // append the new frame:
468 m_intervals.push_back( new_frame );
469 } // eo Intervals::add(const Interval&)
473 * @brief subtracts a time interval from the list.
474 * @param del_frame the time interval to subtract.
476 * removes the time interval from the list; cut off parts from or remove existing
477 * intervals if they overlap.
479 * @internal complexity O(n).
481 void Intervals::sub(const Interval& del_frame)
483 if (not del_frame.is_valid() or del_frame.empty() )
487 for (IntervalList::iterator it= m_intervals.begin();
488 it != m_intervals.end();
491 Interval& current_frame = *it;
492 if ( del_frame.m_lower_bound >= current_frame.m_upper_bound )
494 // del_frame begins later than current end; go on:
498 // at this point: the begin of the del frame is less then the current end.
499 if ( del_frame.m_upper_bound < current_frame.m_lower_bound )
501 // end is before our start; nothing to do.
504 // at this point: the end of the del frame is >= current begin.
505 if ( del_frame.m_upper_bound < current_frame.m_upper_bound )
507 // del frame end point is within our interval.
508 if ( del_frame.m_lower_bound > current_frame.m_lower_bound)
510 // the del frame is within our interval... we need to split:
511 m_intervals.insert(it, Interval( current_frame.m_lower_bound, del_frame.m_lower_bound ) );
513 // adjust start of current frame:
514 if (current_frame.m_lower_bound < del_frame.m_upper_bound)
516 current_frame.m_lower_bound= del_frame.m_upper_bound;
517 current_frame.m_changed= true;
522 // at this point the end of the del frame is >= current end
523 if ( del_frame.m_lower_bound > current_frame.m_lower_bound )
525 // a part of the current interval needs to be preserved..
527 current_frame.m_upper_bound= del_frame.m_lower_bound;
528 current_frame.m_changed= true;
529 // and continue with the next interval:
533 // at this point; the whole frame needs to be deleted..
534 if ( it == m_intervals.begin())
536 m_intervals.erase(it);
537 it= m_intervals.begin();
541 IntervalList::iterator it2= it++;
542 m_intervals.erase(it2);
545 } // eo Intervals::sub(const Interval&)
549 * @brief returns if we contain an interval.
550 * @param other the interval to check.
551 * @return @a true if we cover the given interval, too.
553 bool Intervals::contains(const Interval& other) const
555 for(const_iterator it= begin();
559 if ( it->contains( other ))
565 } // eo Intervals::contains(const Interval&) const
569 * @brief returns if we contain an exact interval.
570 * @param other the interval to check.
571 * @return @a true if we axactly contains the given interval.
573 * @note thsi differs from contain in the way, that we return only @a true
574 * iff we have the given interval in our list; not only cover it.
576 bool Intervals::contains_exact(const Interval& other) const
578 for(const_iterator it= begin();
588 } // eo Intervals::contains_exact(const Interval&)const
592 * @brief returns if we contain another interval combination.
593 * @param other the intervals to check.
594 * @return @a true if we cover the given intervals, too.
596 * @internal we rely on the fact that the lists are sorted and contain
597 * disjoint intervals.
599 * So this method has a complexity of O(n).
601 bool Intervals::contains(const Intervals& other) const
603 const_iterator my_it= begin();
604 const_iterator other_it= other.begin();
605 while( my_it != end() and other_it!= other.end() )
607 // seek the first interval which contains the lower bound of the current other interval
608 while (my_it != end()
609 and my_it->m_lower_bound > other_it->m_lower_bound
610 and other_it->m_lower_bound >= my_it->m_upper_bound
619 if (not my_it->contains( *other_it ))
621 // if we don't contain the current other; we're done:
624 //else check the next other interval:
627 return (other_it == other.end());
628 } // eo Intervals::contains(const Intervals&) const
632 * @brief combines to interval combinates for equality
633 * @param other the other instance.
634 * @return @a true if the other is equal to the current.
636 * @internal since the lists are sorted, we compare the interval lists.
637 * Thus we have a complexity of O(n).
639 bool Intervals::operator==(const Intervals& other) const
641 // since we keep sorted lists: just compare the lists :-)
642 return m_intervals == other.m_intervals;
643 } // eo Intervals::operator==(const Intervals&)
646 Intervals& Intervals::operator+=(const Interval& other)
650 } // eo operator+=(const Interval&)
653 Intervals& Intervals::operator-=(const Interval& other)
657 } // eo operator-=(const Interval&)
661 * @brief adds the intervals of a second instance to us.
662 * @param other the other instance.
663 * @return self reference (allow chaining).
665 * @internal since we do simple loops over the other and our intervals
666 * we have a complexity of O(n^2).
668 * @todo optimize if complexity becomes a problem.
670 Intervals& Intervals::operator+=(const Intervals& other)
672 for(const_iterator it= other.begin();
679 } // eo operator+=(const Intervals&)
683 * @brief subtracts the intervals of a second instance from us.
684 * @param other the other instance.
685 * @return self reference (allow chaining).
687 * @internal since we do simple loops over the other and our intervals
688 * we have a complexity of O(n^2).
690 * @todo optimize if complexity becomes a problem.
692 Intervals& Intervals::operator-=(const Intervals& other)
700 for(const_iterator it= other.begin();
708 } // eo operator-=(const Intervals&)
718 * @brief fetches the value from the monotonic clock source.
719 * @param[out] seconds the seconds.
720 * @param[out] nano_seconds the nano seconds.
721 * @return @a true if the clock was successfully read.
723 bool monotonic_clock_gettime(long int& seconds, long int& nano_seconds)
725 struct timespec tp[1];
726 int res= clock_gettime (CLOCK_MONOTONIC, tp);
730 nano_seconds= tp->tv_nsec;
733 } // eo monotonic_clock_gettime(long int&,long int&)
737 * @brief fetches the value from the monotonic clock source.
738 * @return the time since system start in nanoseconds, 0 if read was unsuccessful
740 long long monotonic_clock_gettime_nano()
743 long int nano_seconds;
746 if (monotonic_clock_gettime(seconds,nano_seconds))
757 * @brief fetches the value from the monotonic clock source.
758 * @param[out] seconds the seconds.
759 * @param[out] nano_seconds the nano seconds.
760 * @return @a true if the clock was successfully read.
762 bool realtime_clock_gettime(long int& seconds, long int& nano_seconds)
764 struct timespec tp[1];
765 int res= clock_gettime(CLOCK_REALTIME, tp);
769 nano_seconds= tp->tv_nsec;
772 } // eo realtime_clock_gettime(long int&,long int&)
775 static const char *const iso8601_fmt_d = "%F";
776 static const char *const iso8601_fmt_t = "%T";
777 static const char *const iso8601_fmt_tz = "%TZ%z";
778 static const char *const iso8601_fmt_dt = "%FT%T";
779 static const char *const iso8601_fmt_dtz = "%FT%TZ%z";
782 * @brief Format a time structure according to ISO-8601, e. g.
783 * “2018-01-09T10:40:00Z+0100”; see \c strftime(3) for
786 * @param tm Time to format as broken-down \c struct tm.
787 * @param date Include the day part ([-]YYYY-MM-DD).
788 * @param time Include the time part (hh:mm:ss).
789 * @param tz Include the timezone ([±]ZZZZ); only heeded if
790 * \c time is requested as well.
792 * @return The formatted timestamp.
794 std::string format_iso8601 (const struct tm &tm, const bool date,
795 const bool time, const bool tz)
797 # define ISO8601_BUFSIZE 27 /* max: -YYYY-MM-DDThh:mm:ssZ+zzzz ⇒ 26 */
798 char buf [ISO8601_BUFSIZE] = { 0 };
799 const char *format = NULL;
804 format = iso8601_fmt_dtz;
806 format = iso8601_fmt_dt;
809 format = iso8601_fmt_d;
811 } else if (time && tz) {
812 format = iso8601_fmt_tz;
814 format = iso8601_fmt_t; /* default to %T */
817 const size_t n = strftime (buf, ISO8601_BUFSIZE, format, &tm);
821 return std::string (buf);
825 typedef struct tm * (*time_breakdown_fn) (const time_t *, struct tm *);
828 * @brief Format a UNIX timestamp according to ISO-8601. Converts
829 * to broken down time first.
831 * @param t Time to format as broken-down \c struct tm.
832 * @param date Include the day part ([-]YYYY-MM-DD).
833 * @param time Include the time part (hh:mm:ss).
834 * @param tz Include the timezone ([±]ZZZZ); only heeded if
835 * \c time is requested as well.
837 * @return The formatted timestamp.
839 std::string format_iso8601 (time_t t, const bool utc, const bool date,
840 const bool time, const bool tz)
842 time_breakdown_fn breakdown = utc ? gmtime_r : localtime_r;
846 if (breakdown (&t, &tm) == NULL) {
847 return std::string ("error analyzing timestamp: ") + strerror (errno);
850 return format_iso8601 (tm, date, time, tz);
860 static inline clockid_t
861 clockid_of_flags (const enum type::id id,
862 const enum type::variant var) NOEXCEPT
864 clockid_t cid = CLOCK_MONOTONIC_COARSE;
875 cid = CLOCK_MONOTONIC_RAW;
879 cid = CLOCK_MONOTONIC;
887 if (var == type::exact) {
888 cid = CLOCK_REALTIME;
890 cid = CLOCK_REALTIME_COARSE;
896 if (var & type::exact) {
897 cid = CLOCK_BOOTTIME;
903 if (var == type::thread) {
904 cid = CLOCK_THREAD_CPUTIME_ID;
906 cid = CLOCK_PROCESS_CPUTIME_ID;
915 static const struct timespec zero_time = { 0, 0 };
919 Time::Time (const enum type::id id,
920 const enum type::variant var) NOEXCEPT
928 Time::as_nanosec (void) const NOEXCEPT
930 return int64_t (this->value.tv_sec) * TIME_CONST_FACTOR_NANO
931 + this->value.tv_nsec;
935 Time::as_nanosec_L (void) const NOEXCEPT /* likely to overflow */
936 { return static_cast<long>(this->as_nanosec ()); }
939 Time::operator= (Time t2) NOEXCEPT
947 Time::operator= (struct timespec ts) NOEXCEPT
949 std::swap (this->value, ts);
950 this->id = clock::type::mono;
951 this->variant = clock::type::dflt;
958 Time::unset (void) NOEXCEPT
959 { this->value = zero_time; }
962 Time::set (void) NOEXCEPT
967 if (clock_gettime (clockid_of_flags (this->id, this->variant), &now)
982 Time::add (const time_t sec, const long nsec) NOEXCEPT
984 this->value.tv_sec += sec;
985 this->value.tv_nsec += nsec;
993 Time::subtract (const time_t sec, const long nsec) NOEXCEPT
995 this->value.tv_sec -= sec;
996 this->value.tv_nsec -= nsec;
1004 Time::scale (const time_t factor) NOEXCEPT
1006 this->value.tv_sec *= factor;
1007 this->value.tv_nsec *= factor;
1009 this->carry_nsec ();
1014 boost::optional<std::string>
1015 Time::format_iso8601 (const bool utc,
1018 const bool tz) const
1020 time_breakdown_fn breakdown = utc ? gmtime_r : localtime_r;
1023 if (breakdown (&this->value.tv_sec, &tm) == NULL) {
1027 return ::format_iso8601 (tm, date, time, tz);
1030 boost::optional<std::string>
1031 Time::make_nice_time (void) const
1033 /* XXX the cast below results in loss of precision with 64 bit time_t! */
1034 return ::make_nice_time (static_cast<int> (this->value.tv_sec));
1037 boost::optional<std::string>
1038 Time::format_full_time (void) const
1039 { return ::format_full_time (this->value.tv_sec); }
1041 boost::optional<std::string>
1042 Time::format_date (void) const
1043 { return ::format_date (this->value.tv_sec); }
1045 boost::optional<Time>
1046 now (const enum type::id id, const enum type::variant var) NOEXCEPT
1058 zero (const enum type::id id, const enum type::variant var) NOEXCEPT
1059 { return Time (id, var); }
1062 compare (const Time &t1, const Time &t2) NOEXCEPT
1064 if (t1.value.tv_sec < t2.value.tv_sec) {
1068 if (t1.value.tv_sec > t2.value.tv_sec) {
1072 if (t1.value.tv_nsec < t2.value.tv_nsec) {
1076 if (t1.value.tv_nsec > t2.value.tv_nsec) {
1083 } /* [namespace clock] */
1085 } /* [namespace I2n] */