Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

date.C

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

httpstats-stage04 [ 7 April, 2001]