1 /* timeval.cc 2 * 3 * Copyright (C) 2002 The gtkmm Development Team 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Lesser General Public 7 * License as published by the Free Software Foundation; either 8 * version 2.1 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public 16 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 #include <glibmm/timeval.h> 20 21 namespace Glib 22 { 23 #ifndef GLIBMM_DISABLE_DEPRECATED 24 G_GNUC_BEGIN_IGNORE_DEPRECATIONS 25 void assign_current_time()26TimeVal::assign_current_time() 27 { 28 g_get_current_time(this); 29 } 30 31 bool assign_from_iso8601(const Glib::ustring & iso_date)32TimeVal::assign_from_iso8601(const Glib::ustring& iso_date) 33 { 34 return g_time_val_from_iso8601(iso_date.c_str(), this); 35 } 36 G_GNUC_END_IGNORE_DEPRECATIONS 37 38 void add(const TimeVal & rhs)39TimeVal::add(const TimeVal& rhs) 40 { 41 g_return_if_fail(tv_usec >= 0 && tv_usec < G_USEC_PER_SEC); 42 g_return_if_fail(rhs.tv_usec >= 0 && rhs.tv_usec < G_USEC_PER_SEC); 43 44 tv_usec += rhs.tv_usec; 45 46 if (tv_usec >= G_USEC_PER_SEC) 47 { 48 tv_usec -= G_USEC_PER_SEC; 49 ++tv_sec; 50 } 51 52 tv_sec += rhs.tv_sec; 53 } 54 55 void subtract(const TimeVal & rhs)56TimeVal::subtract(const TimeVal& rhs) 57 { 58 g_return_if_fail(tv_usec >= 0 && tv_usec < G_USEC_PER_SEC); 59 g_return_if_fail(rhs.tv_usec >= 0 && rhs.tv_usec < G_USEC_PER_SEC); 60 61 tv_usec -= rhs.tv_usec; 62 63 if (tv_usec < 0) 64 { 65 tv_usec += G_USEC_PER_SEC; 66 --tv_sec; 67 } 68 69 tv_sec -= rhs.tv_sec; 70 } 71 72 void add_seconds(long seconds)73TimeVal::add_seconds(long seconds) 74 { 75 g_return_if_fail(tv_usec >= 0 && tv_usec < G_USEC_PER_SEC); 76 77 tv_sec += seconds; 78 } 79 80 void subtract_seconds(long seconds)81TimeVal::subtract_seconds(long seconds) 82 { 83 g_return_if_fail(tv_usec >= 0 && tv_usec < G_USEC_PER_SEC); 84 85 tv_sec -= seconds; 86 } 87 88 void add_milliseconds(long milliseconds)89TimeVal::add_milliseconds(long milliseconds) 90 { 91 g_return_if_fail(tv_usec >= 0 && tv_usec < G_USEC_PER_SEC); 92 93 tv_usec += (milliseconds % 1000) * 1000; 94 95 if (tv_usec < 0) 96 { 97 tv_usec += G_USEC_PER_SEC; 98 --tv_sec; 99 } 100 else if (tv_usec >= G_USEC_PER_SEC) 101 { 102 tv_usec -= G_USEC_PER_SEC; 103 ++tv_sec; 104 } 105 106 tv_sec += milliseconds / 1000; 107 } 108 109 void subtract_milliseconds(long milliseconds)110TimeVal::subtract_milliseconds(long milliseconds) 111 { 112 add_milliseconds(-1 * milliseconds); 113 } 114 115 G_GNUC_BEGIN_IGNORE_DEPRECATIONS 116 void add_microseconds(long microseconds)117TimeVal::add_microseconds(long microseconds) 118 { 119 g_time_val_add(this, microseconds); 120 } 121 122 void subtract_microseconds(long microseconds)123TimeVal::subtract_microseconds(long microseconds) 124 { 125 g_time_val_add(this, -1 * microseconds); 126 } 127 128 Glib::ustring as_iso8601() const129TimeVal::as_iso8601() const 130 { 131 gchar* retval = g_time_val_to_iso8601(const_cast<Glib::TimeVal*>(this)); 132 if (retval) 133 { 134 Glib::ustring iso_date(retval); 135 g_free(retval); 136 return iso_date; 137 } 138 return Glib::ustring(); 139 } 140 G_GNUC_END_IGNORE_DEPRECATIONS 141 #endif // GLIBMM_DISABLE_DEPRECATED 142 143 } // namespace Glib 144