]> git.lyx.org Git - lyx.git/blobdiff - src/support/lyxtime.cpp
Re-fix #11146 with recent LaTeX
[lyx.git] / src / support / lyxtime.cpp
index 7f2530d41553b4b8649ac5920cf888307a60365d..f3318eb3f0e35aeea8bf98021c1fb84a1061c11e 100644 (file)
 
 #include "support/lyxtime.h"
 
+#include "support/debug.h"
+#include "support/environment.h"
+#include "support/lstrings.h"
+#include "support/qstring_helpers.h"
+
+#include <QDateTime>
+#include <QLocale>
+
 using namespace std;
 
 namespace lyx {
+namespace support {
 
 time_t current_time()
 {
@@ -22,12 +31,38 @@ time_t current_time()
 }
 
 
-string const formatted_time(time_t t, string const & fmt)
+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)
 {
-       struct tm * loc_tm = localtime(&t);
-       char date[50];
-       strftime(date, sizeof(date), fmt.c_str(), loc_tm);
-       return string(date);
+       // 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<time_t>(-1);
+       }
+       loc_dt.setTimeSpec(Qt::UTC);
+       return loc_dt.toTime_t();
 }
 
+} // namespace support
 } // namespace lyx