]> git.lyx.org Git - lyx.git/blobdiff - src/support/lyxtime.cpp
Account for old versions of Pygments
[lyx.git] / src / support / lyxtime.cpp
index c302531e2e0b0eb2104450bd24958d3a146ffef8..5415ac4eeb034057234b363d3931d7d004ca40ba 100644 (file)
@@ -40,12 +40,23 @@ string const formatted_time(time_t t, string const & fmt)
 }
 
 
-time_t from_ctime(string t)
+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");
-#if QT_VERSION >= 0x040400
        // toDateTime() is too stupid to recognize variable amounts of
        // whitespace (needed because ctime() outputs double spaces before
        // single digit day numbers and hours)
@@ -58,43 +69,8 @@ time_t from_ctime(string t)
                                << "´ (invalid format)");
                return static_cast<time_t>(-1);
        }
+       loc_dt.setTimeSpec(Qt::UTC);
        return loc_dt.toTime_t();
-#elif defined(_WIN32)
-#error "The minimum required Qt version on windows is Qt 4.4."
-#else
-       // strptime() is not available on windows (defined by POSIX)
-
-       // strptime() uses the current locale, so we need to switch to "C"
-       LYXERR(Debug::LOCALE, "Setting LC_ALL and LC_TIME to C");
-       string oldLC_ALL = getEnv("LC_ALL");
-       string oldLC_TIME = getEnv("LC_TIME");
-       if (!setEnv("LC_ALL", "C"))
-               LYXERR(Debug::LOCALE, "\t... LC_ALL failed!");
-       if (!setEnv("LC_TIME", "C"))
-               LYXERR(Debug::LOCALE, "\t... LC_TIME failed!");
-
-       struct tm loc_tm;
-       char const * const format = "%a%n%b%n%d%n%T%n%Y";
-       char * remainder = strptime(t.c_str(), format, &loc_tm);
-
-       LYXERR(Debug::LOCALE, "Resetting LC_ALL and LC_TIME");
-       if(!setEnv("LC_TIME", oldLC_TIME))
-               LYXERR(Debug::LOCALE, "\t... LC_TIME failed!");
-       if (!setEnv("LC_ALL", oldLC_ALL))
-               LYXERR(Debug::LOCALE, "\t... LC_ALL failed!");
-
-       if (!remainder) {
-               LYXERR(Debug::LOCALE, "Could not parse `" << t
-                               << "´ (invalid format)");
-               return static_cast<time_t>(-1);
-       }
-       if (*remainder != '\0') {
-               LYXERR(Debug::LOCALE, "Could not parse `" << t
-                               << "´ (excess characters)");
-               return static_cast<time_t>(-1);
-       }
-       return mktime(&loc_tm);
-#endif
 }
 
 } // namespace support