00001
00002
00003 #define __EXTENSIONS__
00004 #include <sys/time.h>
00005 #include <time.h>
00006
00007 #include <string.h>
00008 #include <iostream>
00009
00010 #include "date.h"
00011
00012
00013
00014
00015 extern "C" {
00016 time_t get_date(const char*,time_t*);
00017 }
00018
00019 const string DateError::NAME("DateError");
00020
00021 Date::Date() {
00022 struct timeval t;
00023
00024
00025
00026
00027 gettimeofday(&t, 0);
00028 time_ = t.tv_sec;
00029 }
00030
00031 Date::Date(time_t t): time_(t) {
00032 }
00033
00034 Date::Date(unsigned int year, unsigned int month, unsigned int day,
00035 unsigned int hrs=0, unsigned int min=0, unsigned int sec=0): time_(0) {
00036 struct tm tm;
00037 localtime_r(&time_,&tm);
00038 tm.tm_sec = sec;
00039 tm.tm_min = min;
00040 tm.tm_hour = hrs-1;
00041 tm.tm_mday = day;
00042 tm.tm_mon = month;
00043 tm.tm_year = year-1900;
00044 time_ = mktime(&tm);
00045 }
00046
00047 unsigned short
00048 Date::year() const {
00049 struct tm tm;
00050 localtime_r(&time_,&tm);
00051 return tm.tm_year + 1900;
00052 }
00053
00054 unsigned short
00055 Date::month() const {
00056 struct tm tm;
00057 localtime_r(&time_,&tm);
00058 return tm.tm_mon;
00059 }
00060
00061 unsigned short
00062 Date::day() const {
00063 struct tm tm;
00064 localtime_r(&time_,&tm);
00065 return tm.tm_mday;
00066 }
00067
00068 unsigned short
00069 Date::hours() const {
00070 struct tm tm;
00071 localtime_r(&time_,&tm);
00072 return tm.tm_hour;
00073 }
00074
00075 unsigned short
00076 Date::minutes() const {
00077 struct tm tm;
00078 localtime_r(&time_,&tm);
00079 return tm.tm_min;
00080 }
00081
00082 unsigned short
00083 Date::seconds() const {
00084 struct tm tm;
00085 localtime_r(&time_,&tm);
00086 return tm.tm_sec;
00087 }
00088
00089 unsigned short
00090 Date::wday() const {
00091 struct tm tm;
00092 localtime_r(&time_,&tm);
00093 return tm.tm_wday;
00094 }
00095
00096 unsigned short
00097 Date::yday() const {
00098 struct tm tm;
00099 localtime_r(&time_,&tm);
00100 return tm.tm_yday;
00101 }
00102
00103 void
00104 Date::year(unsigned short yr) {
00105 struct tm tm;
00106 localtime_r(&time_,&tm);
00107 tm.tm_year = yr - 1900;
00108 time_ = mktime(&tm);
00109 }
00110
00111 void
00112 Date::month(unsigned short mo) {
00113 struct tm tm;
00114 localtime_r(&time_,&tm);
00115 tm.tm_mon = mo;
00116 time_ = mktime(&tm);
00117 }
00118
00119 void
00120 Date::day(unsigned short dy) {
00121 struct tm tm;
00122 localtime_r(&time_,&tm);
00123 tm.tm_mday = dy;
00124 time_ = mktime(&tm);
00125 }
00126
00127 void
00128 Date::hours(unsigned short hr) {
00129 struct tm tm;
00130 localtime_r(&time_,&tm);
00131 tm.tm_hour = hr;
00132 time_ = mktime(&tm);
00133 }
00134
00135 void
00136 Date::minutes(unsigned short mins) {
00137 struct tm tm;
00138 localtime_r(&time_,&tm);
00139 tm.tm_min = mins;
00140 time_ = mktime(&tm);
00141 }
00142
00143 void
00144 Date::seconds(unsigned short secs) {
00145 struct tm tm;
00146 localtime_r(&time_,&tm);
00147 tm.tm_sec = secs;
00148 time_ = mktime(&tm);
00149 }
00150
00151 string
00152 Date::str(const char* fmt) const {
00153 static const char* d_fmt = "%a %b %d %H:%M:%S %Y";
00154
00155 if (fmt==0)
00156 fmt = d_fmt;
00157 const int size = strlen(fmt)*20;
00158 char* buf = new char[size+1];
00159
00160 struct tm tm;
00161 localtime_r(&time_,&tm);
00162
00163 strftime(buf, size, fmt, &tm);
00164 string s(buf);
00165 delete buf;
00166 return s;
00167 }
00168
00169 string
00170 Date::gmt(const char* fmt) const {
00171 static const char* d_fmt = "%a %b %d %H:%M:%S %Z %Y";
00172
00173 if (fmt==0)
00174 fmt = d_fmt;
00175 const int size = strlen(fmt)*20;
00176 char* buf = new char[size+1];
00177
00178 struct tm tm;
00179 gmtime_r(&time_,&tm);
00180
00181 strftime(buf, size, fmt, &tm);
00182 string s(buf);
00183 delete buf;
00184 return s;
00185 }
00186
00187
00188
00189 Date::Date (const string& s) throw(DateError)
00190 {
00191 static const string error(": cannot parse \"");
00192 time_ = get_date(s.c_str(),0);
00193 if (time_<0)
00194 throw DateError(error+s+"\"");
00195 }
00196
00197 void
00198 Date::time(time_t t) {
00199 time_ = t;
00200 }
00201
00202