X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fdatetime.cpp;h=404cb31bf56b1d8ef6ccf5df74fc599520474e03;hb=9b22dd53fe62e312c1647310b7ec43aa127090af;hp=7d3d4affedccf593a3ed3e4cca22cb09af654700;hpb=d8f51eac91f86a1e00a05a5058a8fa9eb8732464;p=fms.git diff --git a/src/datetime.cpp b/src/datetime.cpp index 7d3d4af..404cb31 100644 --- a/src/datetime.cpp +++ b/src/datetime.cpp @@ -14,7 +14,7 @@ DateTime::DateTime() DateTime::DateTime(const time_t &timet) { - Set(timet); + SetT(timet); } DateTime::DateTime(const struct tm *stm) @@ -36,19 +36,11 @@ void DateTime::Add(const int seconds, const int minutes, const int hours, const std::string DateTime::Format(const std::string &formatstring) const { - std::string returnval=""; - char *str=new char[512]; - memset(str,0,512); + std::vector str(256,0); - strftime(str,511,formatstring.c_str(),&m_tm); + size_t len=strftime(&str[0],str.size()-1,formatstring.c_str(),&m_tm); - if(str) - { - returnval=str; - delete [] str; - } - - return returnval; + return std::string(str.begin(),str.begin()+len); } void DateTime::Normalize() @@ -195,6 +187,11 @@ const bool DateTime::operator==(const struct tm &rhs) const return (m_tm.tm_year==rhs.tm_year && m_tm.tm_mon==rhs.tm_mon && m_tm.tm_mday==rhs.tm_mday && m_tm.tm_hour==rhs.tm_hour && m_tm.tm_min==rhs.tm_min && m_tm.tm_sec==rhs.tm_sec) ? true : false; } +const bool DateTime::operator<(const struct tm &rhs) const +{ + return (m_tm.tm_year tokens; - int vecpos; + std::vector::size_type vecpos; int tempint; year=month=day=hour=minute=second=-1; @@ -418,3 +415,27 @@ void DateTime::SetToLocalTime() m_tm=*localtime(&m_timet); Normalize(); } + +const time_t DateTime::TimeGM(struct tm *gmtimein) +{ + //This looks good but I don't think will work when TZ isn't set (Windows) + //http://developer.apple.com/documentation/Darwin/Reference/ManPages/man3/timegm.3.html + + //This should work + //http://lists2.ais.fraunhofer.de/pipermail/emx/1999-September/000874.html + + struct tm ttm; + time_t t, t2; + + ttm = *gmtimein; /* make a local copy to fiddle with */ + ttm.tm_isdst = 0; /* treat it as standard time */ + + t2 = t = mktime(&ttm); /* calculate the time as a local time */ + + ttm = *gmtime(&t2); /* now calculate the difference between */ + ttm.tm_isdst = 0; /* gm and local time */ + t2 = mktime(&ttm); + + t += t - t2; /* and adjust our answer by that difference */ + return t; +}