X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fsupport%2Flyxtime.cpp;h=3150825d753f80c3dd95ae15b6147b5c4a8d5114;hb=12554c93d81f75f87c34040fd7737048d3518d6d;hp=ad97912d3ed94135af1279b41c1e15322b709444;hpb=06d65218c0cbde21060257cc565d60476635eb1c;p=lyx.git diff --git a/src/support/lyxtime.cpp b/src/support/lyxtime.cpp index ad97912d3e..3150825d75 100644 --- a/src/support/lyxtime.cpp +++ b/src/support/lyxtime.cpp @@ -12,9 +12,18 @@ #include "support/lyxtime.h" -using std::string; +#include "support/debug.h" +#include "support/environment.h" +#include "support/lstrings.h" +#include "support/qstring_helpers.h" + +#include +#include + +using namespace std; namespace lyx { +namespace support { time_t current_time() { @@ -30,4 +39,27 @@ string const formatted_time(time_t t, string const & fmt) return string(date); } + +time_t from_asctime_utc(string t) +{ + // Example for the format: "Sun Nov 6 10:39:39 2011\n" + // Generously remove trailing '\n' (and other whitespace if needed) + t = trim(t, " \t\r\n"); + // toDateTime() is too stupid to recognize variable amounts of + // whitespace (needed because ctime() outputs double spaces before + // single digit day numbers and hours) + t = subst(t, " ", " "); + QString const format("ddd MMM d H:mm:ss yyyy"); + QLocale loc("C"); + QDateTime loc_dt = loc.toDateTime(toqstr(t), format); + if (!loc_dt.isValid()) { + LYXERR(Debug::LOCALE, "Could not parse `" << t + << "´ (invalid format)"); + return static_cast(-1); + } + loc_dt.setTimeSpec(Qt::UTC); + return loc_dt.toTime_t(); +} + +} // namespace support } // namespace lyx