From: <man...@us...> - 2015-02-02 13:41:01
|
Revision: 4743 http://sourceforge.net/p/modplug/code/4743 Author: manxorist Date: 2015-02-02 13:40:54 +0000 (Mon, 02 Feb 2015) Log Message: ----------- [Ref] Move date and time handling code from all over the place into namespace mpt::Date in misc_util.h. [Ref] Convert related code to mpt::ustring. Modified Paths: -------------- trunk/OpenMPT/common/Logging.cpp trunk/OpenMPT/common/misc_util.cpp trunk/OpenMPT/common/misc_util.h trunk/OpenMPT/libopenmpt/libopenmpt_impl.cpp trunk/OpenMPT/mptrack/TrackerSettings.cpp trunk/OpenMPT/soundlib/Sndfile.cpp trunk/OpenMPT/soundlib/Sndfile.h Modified: trunk/OpenMPT/common/Logging.cpp =================================================================== --- trunk/OpenMPT/common/Logging.cpp 2015-01-31 15:58:28 UTC (rev 4742) +++ trunk/OpenMPT/common/Logging.cpp 2015-02-02 13:40:54 UTC (rev 4743) @@ -36,68 +36,9 @@ static const std::size_t LOGBUF_SIZE = 1024; -#if defined(MODPLUG_TRACKER) - -static uint64 GetTime100ns() -//-------------------------- -{ - FILETIME filetime; - GetSystemTimeAsFileTime(&filetime); - return ((uint64)filetime.dwHighDateTime << 32 | filetime.dwLowDateTime); -} - - -static std::string TimeAsString(uint64 time100ns) -//------------------------------------------------- -{ - - FILETIME filetime; - SYSTEMTIME systime; - filetime.dwHighDateTime = (DWORD)(((uint64)time100ns) >> 32); - filetime.dwLowDateTime = (DWORD)((uint64)time100ns); - FileTimeToSystemTime(&filetime, &systime); - - std::string result; - TCHAR buf[LOGBUF_SIZE]; - - GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, &systime, "yyyy-MM-dd ", buf, LOGBUF_SIZE); - result.append(buf); - - GetTimeFormat(LOCALE_SYSTEM_DEFAULT, TIME_FORCE24HOURFORMAT, &systime, "HH:mm:ss.", buf, LOGBUF_SIZE); - result.append(buf); - - sprintf(buf, "%03u", (unsigned)systime.wMilliseconds); - result.append(buf); - - return result; -} - -#endif // MODPLUG_TRACKER - - #ifndef NO_LOGGING -#if defined(MODPLUG_TRACKER) - - -static std::string TimeDiffAsString(uint64 ms) -//-------------------------------------------- -{ - return mpt::fmt::dec<6>(ms); -} - - -static std::wstring TimeDiffAsStringW(uint64 ms) -//---------------------------------------------- -{ - return mpt::wfmt::dec<6>(ms); -} - - -#endif // MODPLUG_TRACKER - - static noinline void DoLog(const mpt::log::Context &context, mpt::ustring message) //-------------------------------------------------------------------------------- { @@ -105,7 +46,7 @@ message = mpt::String::RTrim(message, MPT_USTRING("\r\n")); #if defined(MODPLUG_TRACKER) static uint64_t s_lastlogtime = 0; - uint64 cur = GetTime100ns(); + uint64 cur = mpt::Date::ANSI::Now(); uint64 diff = cur/10000 - s_lastlogtime; s_lastlogtime = cur/10000; #ifdef LOG_TO_FILE @@ -117,13 +58,26 @@ } if(s_logfile) { - fprintf(s_logfile, "%s+%s %s(%i): %s [%s]\n", TimeAsString(cur).c_str(), TimeDiffAsString(diff).c_str(), context.file, context.line, mpt::ToCharset(mpt::CharsetUTF8, message).c_str(), context.function); + fprintf(s_logfile, mpt::ToCharset(mpt::CharsetUTF8, mpt::String::Print(MPT_USTRING("%1+%2 %3(%4): %5 [%6]\n" + , mpt::Date::ANSI::ToString(cur) + , mpt::ufmt::dec<6>(diff) + , mpt::ToUnicode(mpt::CharsetASCII, context.file) + , context.line + , message + , mpt::ToUnicode(mpt::CharsetASCII, context.function) + ))).c_str()); fflush(s_logfile); } } #endif // LOG_TO_FILE { - OutputDebugStringW(mpt::String::PrintW(L"%1(%2): +%3 %4 [%5]\n", mpt::ToWide(mpt::CharsetASCII, context.file), context.line, TimeDiffAsStringW(diff), message, mpt::ToWide(mpt::CharsetASCII, context.function)).c_str()); + OutputDebugStringW(mpt::String::PrintW(L"%1(%2): +%3 %4 [%5]\n" + , mpt::ToWide(mpt::CharsetASCII, context.file) + , context.line + , mpt::wfmt::dec<6>(diff) + , message + , mpt::ToWide(mpt::CharsetASCII, context.function) + ).c_str()); } #else // !MODPLUG_TRACKER std::clog @@ -297,7 +251,7 @@ LARGE_INTEGER qpcNow; qpcNow.QuadPart = 0; QueryPerformanceCounter(&qpcNow); - uint64 ftNow = GetTime100ns(); + uint64 ftNow = mpt::Date::ANSI::Now(); // sort according to index in case of overflows std::stable_sort(Entries.begin(), Entries.end()); @@ -316,7 +270,7 @@ qpcValid = true; } - f << "Dump: " << TimeAsString(ftNow) << std::endl; + f << "Dump: " << mpt::ToCharset(mpt::CharsetUTF8, mpt::Date::ANSI::ToString(ftNow)) << std::endl; f << "Captured events: " << Entries.size() << std::endl; if(qpcValid && (Entries.size() > 0)) { @@ -334,7 +288,7 @@ std::string time; if(qpcValid) { - time = TimeAsString( ftNow - static_cast<int64>( static_cast<double>(qpcNow.QuadPart - entry.Timestamp) * (10000000.0 / static_cast<double>(qpcFreq.QuadPart) ) ) ); + time = mpt::ToCharset(mpt::CharsetUTF8, mpt::Date::ANSI::ToString( ftNow - static_cast<int64>( static_cast<double>(qpcNow.QuadPart - entry.Timestamp) * (10000000.0 / static_cast<double>(qpcFreq.QuadPart) ) ) ) ); } else { time = mpt::String::Print<std::string>("0x%1", mpt::fmt::hex0<16>(entry.Timestamp)); Modified: trunk/OpenMPT/common/misc_util.cpp =================================================================== --- trunk/OpenMPT/common/misc_util.cpp 2015-01-31 15:58:28 UTC (rev 4742) +++ trunk/OpenMPT/common/misc_util.cpp 2015-02-02 13:40:54 UTC (rev 4743) @@ -101,12 +101,65 @@ #endif -namespace Util +namespace mpt { +namespace Date +{ -time_t MakeGmTime(tm *timeUtc) -//---------------------------- +#if defined(MODPLUG_TRACKER) + +namespace ANSI { + +uint64 Now() +//---------- +{ + FILETIME filetime; + GetSystemTimeAsFileTime(&filetime); + return ((uint64)filetime.dwHighDateTime << 32 | filetime.dwLowDateTime); +} + +mpt::ustring ToString(uint64 time100ns) +//------------------------------------- +{ + static const std::size_t bufsize = 256; + + mpt::ustring result; + + FILETIME filetime; + SYSTEMTIME systime; + filetime.dwHighDateTime = (DWORD)(((uint64)time100ns) >> 32); + filetime.dwLowDateTime = (DWORD)((uint64)time100ns); + FileTimeToSystemTime(&filetime, &systime); + + WCHAR buf[bufsize]; + + GetDateFormatW(LOCALE_SYSTEM_DEFAULT, 0, &systime, L"yyyy-MM-dd", buf, bufsize); + result.append(mpt::ToUnicode(buf)); + + result.append(MPT_USTRING(" ")); + + GetTimeFormatW(LOCALE_SYSTEM_DEFAULT, TIME_FORCE24HOURFORMAT, &systime, L"HH:mm:ss", buf, bufsize); + result.append(mpt::ToUnicode(buf)); + + result.append(MPT_USTRING(".")); + + result.append(mpt::ufmt::dec0<3>((unsigned)systime.wMilliseconds)); + + return result; + +} + +} // namespace ANSI + +#endif // MODPLUG_TRACKER + +namespace Unix +{ + +time_t FromUTC(tm *timeUtc) +//------------------------- +{ #if MPT_COMPILER_MSVC return _mkgmtime(timeUtc); #else // !MPT_COMPILER_MSVC @@ -139,9 +192,64 @@ #endif // MPT_COMPILER_MSVC } -} // namespace Util +} // namespace Unix +mpt::ustring ToShortenedISO8601(tm date) +//-------------------------------------- +{ + // We assume date in UTC here. + // There are too many differences in supported format specifiers in strftime() + // and strftime does not support reduced precision ISO8601 at all. + // Just do the formatting ourselves. + mpt::ustring result; + mpt::ustring timezone = MPT_USTRING("Z"); + if(date.tm_year == 0) + { + return result; + } + result += mpt::ufmt::dec0<4>(date.tm_year + 1900); + if(date.tm_mon < 0 || date.tm_mon > 11) + { + return result; + } + result += MPT_USTRING("-") + mpt::ufmt::dec0<2>(date.tm_mon + 1); + if(date.tm_mday < 1 || date.tm_mday > 31) + { + return result; + } + result += MPT_USTRING("-") + mpt::ufmt::dec0<2>(date.tm_mday); + if(date.tm_hour == 0 && date.tm_min == 0 && date.tm_sec == 0) + { + return result; + } + if(date.tm_hour < 0 || date.tm_hour > 23) + { + return result; + } + if(date.tm_min < 0 || date.tm_min > 59) + { + return result; + } + result += MPT_USTRING("T"); + if(date.tm_isdst > 0) + { + timezone = MPT_USTRING("+01:00"); + } + result += mpt::ufmt::dec0<2>(date.tm_hour) + MPT_USTRING(":") + mpt::ufmt::dec0<2>(date.tm_min); + if(date.tm_sec < 0 || date.tm_sec > 61) + { + return result + timezone; + } + result += MPT_USTRING(":") + mpt::ufmt::dec0<2>(date.tm_sec); + result += timezone; + return result; +} +} // namespace Date +} // namespace mpt + + + #ifdef MODPLUG_TRACKER namespace Util Modified: trunk/OpenMPT/common/misc_util.h =================================================================== --- trunk/OpenMPT/common/misc_util.h 2015-01-31 15:58:28 UTC (rev 4742) +++ trunk/OpenMPT/common/misc_util.h 2015-02-02 13:40:54 UTC (rev 4743) @@ -374,11 +374,44 @@ } + +namespace mpt +{ +namespace Date +{ + +#if defined(MODPLUG_TRACKER) + +namespace ANSI +{ +// uint64 counts 100ns since 1601-01-01T00:00Z + +uint64 Now(); + +mpt::ustring ToString(uint64 time100ns); // i.e. 2015-01-15 18:32:01.718 + +} // namespacee ANSI + +#endif // MODPLUG_TRACKER + +namespace Unix +{ +// time_t counts 1s since 1970-01-01T00:00Z + +time_t FromUTC(tm *timeUtc); + +} // namespace Unix + +mpt::ustring ToShortenedISO8601(tm date); // i.e. 2015-01-15T18:32:01Z + +} // namespace Date +} // namespace mpt + + + namespace Util { - time_t MakeGmTime(tm *timeUtc); - // Minimum of 3 values template <class T> inline const T& Min(const T& a, const T& b, const T& c) {return std::min(std::min(a, b), c);} Modified: trunk/OpenMPT/libopenmpt/libopenmpt_impl.cpp =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmpt_impl.cpp 2015-01-31 15:58:28 UTC (rev 4742) +++ trunk/OpenMPT/libopenmpt/libopenmpt_impl.cpp 2015-02-02 13:40:54 UTC (rev 4743) @@ -828,7 +828,7 @@ if ( m_sndFile->GetFileHistory().empty() ) { return std::string(); } - return mod_string_to_utf8( m_sndFile->GetFileHistory()[m_sndFile->GetFileHistory().size() - 1].AsISO8601() ); + return mpt::ToCharset(mpt::CharsetUTF8, m_sndFile->GetFileHistory()[m_sndFile->GetFileHistory().size() - 1].AsISO8601() ); } else if ( key == std::string("message") ) { std::string retval = m_sndFile->songMessage.GetFormatted( SongMessage::leLF ); if ( retval.empty() ) { Modified: trunk/OpenMPT/mptrack/TrackerSettings.cpp =================================================================== --- trunk/OpenMPT/mptrack/TrackerSettings.cpp 2015-01-31 15:58:28 UTC (rev 4742) +++ trunk/OpenMPT/mptrack/TrackerSettings.cpp 2015-02-02 13:40:54 UTC (rev 4743) @@ -340,7 +340,7 @@ lastUpdate.tm_year -= 1900; lastUpdate.tm_mon--; } - time_t outTime = Util::MakeGmTime(&lastUpdate); + time_t outTime = mpt::Date::Unix::FromUTC(&lastUpdate); if(outTime < 0) outTime = 0; CUpdateCheck::SetUpdateSettings ( Modified: trunk/OpenMPT/soundlib/Sndfile.cpp =================================================================== --- trunk/OpenMPT/soundlib/Sndfile.cpp 2015-01-31 15:58:28 UTC (rev 4742) +++ trunk/OpenMPT/soundlib/Sndfile.cpp 2015-02-02 13:40:54 UTC (rev 4743) @@ -492,8 +492,8 @@ // -! NEW_FEATURE#0027 -std::string FileHistory::AsISO8601() const -//---------------------------------------- +mpt::ustring FileHistory::AsISO8601() const +//----------------------------------------- { tm date = loadDate; if(openTime > 0) @@ -501,7 +501,7 @@ // Calculate the date when editing finished. double openSeconds = (double)openTime / (double)HISTORY_TIMER_PRECISION; tm tmpLoadDate = loadDate; - time_t loadDateSinceEpoch = Util::MakeGmTime(&tmpLoadDate); + time_t loadDateSinceEpoch = mpt::Date::Unix::FromUTC(&tmpLoadDate); double timeScaleFactor = difftime(2, 1); time_t saveDateSinceEpoch = loadDateSinceEpoch + Util::Round<time_t>(openSeconds / timeScaleFactor); const tm * tmpSaveDate = gmtime(&saveDateSinceEpoch); @@ -512,51 +512,7 @@ } // We assume date in UTC here. // This is not 100% correct because FileHistory does not contain complete timezone information. - // There are too many differences in supported format specifiers in strftime() - // and strftime does not support reduced precision ISO8601 at all. - // Just do the formatting ourselves. - std::string result; - std::string timezone = std::string("Z"); - if(date.tm_year == 0) - { - return result; - } - result += mpt::fmt::dec0<4>(date.tm_year + 1900); - if(date.tm_mon < 0 || date.tm_mon > 11) - { - return result; - } - result += std::string("-") + mpt::fmt::dec0<2>(date.tm_mon + 1); - if(date.tm_mday < 1 || date.tm_mday > 31) - { - return result; - } - result += std::string("-") + mpt::fmt::dec0<2>(date.tm_mday); - if(date.tm_hour == 0 && date.tm_min == 0 && date.tm_sec == 0) - { - return result; - } - if(date.tm_hour < 0 || date.tm_hour > 23) - { - return result; - } - if(date.tm_min < 0 || date.tm_min > 59) - { - return result; - } - result += std::string("T"); - if(date.tm_isdst > 0) - { - timezone = std::string("+01:00"); - } - result += mpt::fmt::dec0<2>(date.tm_hour) + std::string(":") + mpt::fmt::dec0<2>(date.tm_min); - if(date.tm_sec < 0 || date.tm_sec > 61) - { - return result + timezone; - } - result += std::string(":") + mpt::fmt::dec0<2>(date.tm_sec); - result += timezone; - return result; + return mpt::Date::ToShortenedISO8601(date); } Modified: trunk/OpenMPT/soundlib/Sndfile.h =================================================================== --- trunk/OpenMPT/soundlib/Sndfile.h 2015-01-31 15:58:28 UTC (rev 4742) +++ trunk/OpenMPT/soundlib/Sndfile.h 2015-02-02 13:40:54 UTC (rev 4743) @@ -243,7 +243,7 @@ // Time the file was open in the editor, in 1/18.2th seconds (frequency of a standard DOS timer, to keep compatibility with Impulse Tracker easy). uint32 openTime; // Return the date as a (possibly truncated if not enough precision is available) ISO 8601 formatted date. - std::string AsISO8601() const; + mpt::ustring AsISO8601() const; }; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |