00001 /* 00002 Copyright 2003 by Marc J. Rochkind. All rights reserved. 00003 May be copied only for purposes and under conditions described 00004 on the Web page www.basepath.com/aup/copyright.htm. 00005 00006 The Example Files are provided "as is," without any warranty; 00007 without even the implied warranty of merchantability or fitness 00008 for a particular purpose. The author and his publisher are not 00009 responsible for any damages, direct or incidental, resulting 00010 from the use or non-use of these Example Files. 00011 00012 The Example Files may contain defects, and some contain deliberate 00013 coding mistakes that were included for educational reasons. 00014 You are responsible for determining if and how the Example Files 00015 are to be used. 00016 00017 */ 00018 #include "ux.hpp" 00019 #include <iomanip> 00020 #include <sys/time.h> 00021 00022 using namespace Ux; 00023 00024 time_t Timetm::mktime(void) 00025 { 00026 time_t t; 00027 00028 errno = 0; 00029 if ((t = ::mktime(this)) == -1) { 00030 if (errno == 0) 00031 errno = EINVAL; // SUS defines no errno values -- make one up 00032 throw Error(errno); 00033 } 00034 return t; 00035 } 00036 00037 const char *Timetm::asctime(bool keep_nl) 00038 { 00039 if (::asctime_r(this, str.timestr) == NULL) 00040 throw Error(EINVAL); // SUS defines no errno values -- make one up 00041 if (!keep_nl) 00042 str.timestr[strlen(str.timestr) - 1] = '\0'; 00043 return str; 00044 } 00045 00046 void Timetm::getdate(const char *s) 00047 { 00048 struct tm *t; 00049 00050 if ((t = ::getdate(s)) == NULL) 00051 throw Error(getdate_err, EC_GETDATE); 00052 set(*t); 00053 } 00054 00055 void Timetm::gmtime(const time_t *t) 00056 { 00057 time_t tx; 00058 00059 if (t == NULL) { 00060 tx = Timet::time(); 00061 t = &tx; 00062 } 00063 if (::gmtime_r(t, this) == NULL) 00064 throw Error(errno); 00065 } 00066 00067 void Timetm::localtime(const time_t *t) 00068 { 00069 time_t tx; 00070 00071 if (t == NULL) { 00072 tx = Timet::time(); 00073 t = &tx; 00074 } 00075 if (::localtime_r(t, this) == NULL) 00076 throw Error(errno); 00077 } 00078 00079 void Timetm::set(const struct tm& t) 00080 { 00081 tm_sec = t.tm_sec; 00082 tm_min = t.tm_min; 00083 tm_hour = t.tm_hour; 00084 tm_mday = t.tm_mday; 00085 tm_mon = t.tm_mon; 00086 tm_year = t.tm_year; 00087 tm_wday = t.tm_wday; 00088 tm_yday = t.tm_yday; 00089 tm_isdst = t.tm_isdst; 00090 str.clear(); 00091 } 00092 00093 char *Timetm::strptime(const char *s, const char *format) 00094 { 00095 char *p; 00096 00097 errno = 0; 00098 if ((p = ::strptime(s, format, this)) == NULL) { 00099 if (errno == 0) 00100 errno = EINVAL; // SUS defines no errno values -- make one up 00101 throw Error(errno); 00102 } 00103 return p; 00104 } 00105 00106 size_t Timetm::strftime(char *buf, size_t bufsize, const char *format) 00107 { 00108 size_t n; 00109 00110 errno = 0; 00111 if ((n = ::strftime(buf, bufsize, format, this)) == 0) { 00112 if (errno == 0) 00113 errno = EINVAL; // SUS defines no errno values -- make one up 00114 throw Error(errno); 00115 } 00116 return n; 00117 } 00118 00119 size_t Timetm::wcsftime(wchar_t *buf, size_t bufsize, const wchar_t *format) 00120 { 00121 size_t n; 00122 00123 errno = 0; 00124 if ((n = ::wcsftime(buf, bufsize, format, this)) == 0) { 00125 if (errno == 0) 00126 errno = EINVAL; // SUS defines no errno values -- make one up 00127 throw Error(errno); 00128 } 00129 return n; 00130 } 00131 00132 const char *Timestr::ctime(const time_t *t, bool keep_nl) 00133 { 00134 time_t tx; 00135 00136 if (t == NULL) { 00137 tx = Timet::time(); 00138 t = &tx; 00139 } 00140 if (::ctime_r(t, timestr) == NULL) 00141 throw Error(errno); 00142 if (!keep_nl) 00143 timestr[strlen(timestr) - 1] = '\0'; 00144 return timestr; 00145 } 00146 00147 /* static */ time_t Timet::time(void) 00148 { 00149 time_t t; 00150 00151 errno = 0; 00152 if ((t = ::time(NULL)) == -1) { 00153 if (errno == 0) 00154 errno = EINVAL; // SUS defines no errno values -- make one up 00155 throw Error(errno); 00156 } 00157 return t; 00158 } 00159 00160 double Timet::difftime(time_t time0) 00161 { 00162 return ::difftime(tt, time0); 00163 } 00164 00165 //#include <sys/time.h> 00166 void Timeval::gettimeofday(void) 00167 { 00168 errno = 0; 00169 if (::gettimeofday(this, NULL) != 0) { 00170 if (errno == 0) 00171 errno = EINVAL; // SUS defines no errno values -- make one up 00172 throw Error(errno); 00173 } 00174 }