3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "support/lyxtime.h"
15 #include "support/debug.h"
16 #include "support/environment.h"
17 #include "support/lstrings.h"
18 #include "support/qstring_helpers.h"
34 docstring formatted_datetime(time_t t, string const & fmt)
37 #if (QT_VERSION >= QT_VERSION_CHECK(5, 8, 0))
39 qres = QLocale().toString(QDateTime::fromSecsSinceEpoch(t),
40 QLocale::ShortFormat);
42 qres = QLocale().toString(QDateTime::fromSecsSinceEpoch(t),
46 qres = QLocale().toString(QDateTime::fromTime_t(t),
47 QLocale::ShortFormat);
49 qres = QLocale().toString(QDateTime::fromTime_t(t), toqstr(fmt));
51 return qstring_to_ucs4(qres);
55 time_t from_asctime_utc(string t)
57 // Example for the format: "Sun Nov 6 10:39:39 2011\n"
58 // Generously remove trailing '\n' (and other whitespace if needed)
59 t = trim(t, " \t\r\n");
60 // toDateTime() is too stupid to recognize variable amounts of
61 // whitespace (needed because ctime() outputs double spaces before
62 // single digit day numbers and hours)
63 t = subst(t, " ", " ");
64 QString const format("ddd MMM d H:mm:ss yyyy");
66 QDateTime loc_dt = loc.toDateTime(toqstr(t), format);
67 if (!loc_dt.isValid()) {
68 LYXERR(Debug::LOCALE, "Could not parse `" << t
69 << "ยด (invalid format)");
70 return static_cast<time_t>(-1);
72 loc_dt.setTimeSpec(Qt::UTC);
73 #if (QT_VERSION >= QT_VERSION_CHECK(5, 8, 0))
74 return loc_dt.toSecsSinceEpoch();
76 return loc_dt.toTime_t();
80 } // namespace support