X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fsupport%2Flyxtime.cpp;h=5415ac4eeb034057234b363d3931d7d004ca40ba;hb=522f3517e1d7f61ed2bbcafe0632f50cb3e8ae2f;hp=b24e19e91793f146fcfb55c9642d770de3dc884a;hpb=848c89564fce9c4bfec79b915746dc6bc47abc3d;p=lyx.git diff --git a/src/support/lyxtime.cpp b/src/support/lyxtime.cpp index b24e19e917..5415ac4eeb 100644 --- a/src/support/lyxtime.cpp +++ b/src/support/lyxtime.cpp @@ -12,17 +12,26 @@ #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_type current_time() +time_t current_time() { return time(0); } -string const formatted_time(time_type t, string const & fmt) +string const formatted_time(time_t t, string const & fmt) { struct tm * loc_tm = localtime(&t); char date[50]; @@ -30,4 +39,39 @@ string const formatted_time(time_type t, string const & fmt) return string(date); } + +docstring formatted_datetime(time_t t, string const & fmt) +{ + QString qres; + if (fmt.empty()) + qres = QLocale().toString(QDateTime::fromTime_t(t), + QLocale::ShortFormat); + else + qres = QLocale().toString(QDateTime::fromTime_t(t), toqstr(fmt)); + return qstring_to_ucs4(qres); +} + + +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