1 // src/DateTime.hh 2 // This file is part of libpbe; see http://decimail.org 3 // (C) 2006 Philip Endecott 4 5 // This program is free software; you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation; either version 2 of the License, or 8 // any later version. 9 // 10 // This program 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 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with this program; if not, write to the Free Software 17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18 19 #ifndef libpbe_DateTime_hh 20 #define libpbe_DateTime_hh 21 22 #include <iostream> 23 24 #include <time.h> 25 26 #include "Date.hh" 27 #include "Time.hh" 28 #include "Exception.hh" 29 30 31 namespace pbe { 32 33 struct DateTime { 34 35 Date date; 36 Time time; 37 DateTimepbe::DateTime38 DateTime() {} 39 DateTimepbe::DateTime40 DateTime(int year, int month, int day, 41 int hour=0, int minute=0, int second=0): 42 date(year,month,day), 43 time(hour,minute,second) {} 44 DateTimepbe::DateTime45 DateTime(time_t t) { 46 struct tm b; 47 localtime_r(&t, &b); // Or gmtime_r() ??? 48 date.year = b.tm_year + 1900; 49 date.month = b.tm_mon + 1; 50 date.day = b.tm_mday; 51 time.hour = b.tm_hour; 52 time.minute = b.tm_min; 53 time.second = b.tm_sec; 54 } 55 DateTimepbe::DateTime56 DateTime(Date date_): 57 date(date_), time(0,0,0) {} 58 DateTimepbe::DateTime59 DateTime(Date date_, Time time_): 60 date(date_), time(time_) {} 61 operator <pbe::DateTime62 bool operator<(const DateTime& rhs) const { 63 return (date<rhs.date) 64 || (date==rhs.date && time<rhs.time); 65 } 66 67 to_struct_tmpbe::DateTime68 void to_struct_tm(struct tm& b) const { 69 b.tm_year = date.year - 1900; 70 b.tm_mon = date.month - 1; 71 b.tm_mday = date.day; 72 b.tm_hour = time.hour; 73 b.tm_min = time.minute; 74 b.tm_sec = time.second; 75 } 76 to_time_tpbe::DateTime77 time_t to_time_t(void) const { 78 struct tm b; 79 to_struct_tm(b); 80 time_t t = mktime(&b); 81 if (t==-1) { 82 throw pbe::StrException("mktime() input is invalid or out of range"); 83 } 84 return t; 85 } 86 utc_to_time_tpbe::DateTime87 time_t utc_to_time_t(void) const { 88 struct tm b; 89 to_struct_tm(b); 90 time_t t = timegm(&b); 91 if (t==-1) { 92 throw pbe::StrException("mktime() input is invalid or out of range"); 93 } 94 return t; 95 } 96 day_of_weekpbe::DateTime97 int day_of_week(void) const { 98 struct tm b; 99 to_struct_tm(b); 100 time_t t = timegm(&b); 101 if (t==-1) { 102 throw pbe::StrException("mktime() input is invalid or out of range"); 103 } 104 return b.tm_wday+1; 105 } 106 107 }; 108 109 operator <<(std::ostream & strm,const DateTime & dt)110 inline std::ostream& operator<<(std::ostream& strm, const DateTime& dt) { 111 strm << dt.date << " " << dt.time; 112 return strm; 113 } 114 115 } 116 117 118 #endif 119