00001 /*************************************************************************** 00002 * Copyright (C) 2005-2007 Stefan Schwarzer, Jens Schneider, * 00003 * Matthias Hardt, Guido Madaus * 00004 * * 00005 * Copyright (C) 2007-2008 BerLinux Solutions GbR * 00006 * Stefan Schwarzer & Guido Madaus * 00007 * * 00008 * Copyright (C) 2009-2013 BerLinux Solutions GmbH * 00009 * * 00010 * Authors: * 00011 * Stefan Schwarzer <stefan.schwarzer@diskohq.org>, * 00012 * Matthias Hardt <matthias.hardt@diskohq.org>, * 00013 * Jens Schneider <jens.schneider@diskohq.org>, * 00014 * Guido Madaus <guido.madaus@diskohq.org>, * 00015 * Patrick Helterhoff <patrick.helterhoff@diskohq.org>, * 00016 * René Bählkow <rene.baehlkow@diskohq.org> * 00017 * * 00018 * This library is free software; you can redistribute it and/or * 00019 * modify it under the terms of the GNU Lesser General Public * 00020 * License version 2.1 as published by the Free Software Foundation. * 00021 * * 00022 * This library is distributed in the hope that it will be useful, * 00023 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00025 * Lesser General Public License for more details. * 00026 * * 00027 * You should have received a copy of the GNU Lesser General Public * 00028 * License along with this library; if not, write to the * 00029 * Free Software Foundation, Inc., * 00030 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * 00031 **************************************************************************/ 00032 00033 #include "mmstools/mmsdatetime.h" 00034 #include "mmstools/tools.h" 00035 #include <stdlib.h> 00036 00037 MMSDateTime::MMSDateTime() : 00038 timest(time(NULL)) { 00039 00040 struct tm mytime; 00041 00042 localtime_r(&(this->timest),&mytime); 00043 this->daynum = mytime.tm_mday; 00044 this->monthnum = mytime.tm_mon; 00045 this->yearnum = 1900 + mytime.tm_year; 00046 this->dayofweek = mytime.tm_wday; 00047 this->hour = mytime.tm_hour; 00048 this->minute = mytime.tm_min; 00049 this->seconds = mytime.tm_sec; 00050 } 00051 00052 MMSDateTime::MMSDateTime(string timestr, string format) { 00053 struct tm mytime; 00054 if(format == "YYYY-MM-DD hh:mm:ss") { 00055 this->yearstr = timestr.substr(0,4); 00056 this->yearnum = strToInt(this->yearstr); 00057 this->monthnum = atoi(timestr.substr(5,2).c_str()) -1; 00058 this->daynum = atoi(timestr.substr(8,2).c_str()); 00059 this->hour = atoi(timestr.substr(11,2).c_str()); 00060 this->minute = atoi(timestr.substr(14,2).c_str()); 00061 this->seconds = atoi(timestr.substr(17,2).c_str()); 00062 } 00063 00064 mytime.tm_mday = this->daynum; 00065 mytime.tm_mon = this->monthnum; 00066 mytime.tm_year = this->yearnum -1900; 00067 mytime.tm_hour = this->hour; 00068 mytime.tm_min = this->minute; 00069 mytime.tm_sec = this->seconds; 00070 00071 this->timest = mktime(&mytime); 00072 } 00073 00074 MMSDateTime::MMSDateTime(time_t stamp) : 00075 timest(stamp) { 00076 struct tm mytime; 00077 00078 localtime_r(&(this->timest),&mytime); 00079 this->daynum = mytime.tm_mday; 00080 this->monthnum = mytime.tm_mon; 00081 this->yearnum = 1900 + mytime.tm_year; 00082 this->dayofweek = mytime.tm_wday; 00083 this->hour = mytime.tm_hour; 00084 this->minute = mytime.tm_min; 00085 this->seconds = mytime.tm_sec; 00086 } 00087 00088 MMSDateTime::~MMSDateTime() { 00089 00090 } 00091 00092 string &MMSDateTime::getDay() { 00093 return this->daystr; 00094 } 00095 00096 string &MMSDateTime::getMonth() { 00097 return this->monthstr; 00098 } 00099 00100 string &MMSDateTime::getYear() { 00101 return this->yearstr; 00102 } 00103 00104 string &MMSDateTime::getDbDate() { 00105 this->dbdate = iToStr(this->yearnum) + "-" 00106 + ((this->monthnum < 10) ? "0" : "") + iToStr(this->monthnum + 1) + "-" 00107 + ((this->daynum < 10) ? "0" : "") + iToStr(this->daynum) + " " 00108 + ((this->hour < 10) ? "0" : "") + iToStr(this->hour) + ":" 00109 + ((this->minute < 10) ? "0" : "") + iToStr(this->minute) + ":" 00110 + ((this->seconds < 10) ? "0" : "") + iToStr(this->seconds); 00111 00112 return this->dbdate; 00113 } 00114 00115 int MMSDateTime::getDayNum() { 00116 return this->daynum; 00117 } 00118 00119 int MMSDateTime::getMonthNum() { 00120 return this->monthnum; 00121 } 00122 00123 int MMSDateTime::getYearNum() { 00124 return this->yearnum; 00125 }