]> git.lyx.org Git - lyx.git/blob - src/support/lyxtime.cpp
More requires --> required, for C++2a.
[lyx.git] / src / support / lyxtime.cpp
1 /**
2  * \file lyxtime.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "support/lyxtime.h"
14
15 #include "support/debug.h"
16 #include "support/environment.h"
17 #include "support/lstrings.h"
18 #include "support/qstring_helpers.h"
19
20 #include <QDateTime>
21 #include <QLocale>
22
23 using namespace std;
24
25 namespace lyx {
26 namespace support {
27
28 time_t current_time()
29 {
30         return time(0);
31 }
32
33
34 docstring formatted_datetime(time_t t, string const & fmt)
35 {
36         QString qres;
37         if (fmt.empty())
38                 qres = QLocale().toString(QDateTime::fromTime_t(t),
39                                           QLocale::ShortFormat);
40         else
41                 qres = QLocale().toString(QDateTime::fromTime_t(t), toqstr(fmt));
42         return qstring_to_ucs4(qres);
43 }
44
45
46 time_t from_asctime_utc(string t)
47 {
48         // Example for the format: "Sun Nov  6 10:39:39 2011\n"
49         // Generously remove trailing '\n' (and other whitespace if needed)
50         t = trim(t, " \t\r\n");
51         // toDateTime() is too stupid to recognize variable amounts of
52         // whitespace (needed because ctime() outputs double spaces before
53         // single digit day numbers and hours)
54         t = subst(t, "  ", " ");
55         QString const format("ddd MMM d H:mm:ss yyyy");
56         QLocale loc("C");
57         QDateTime loc_dt = loc.toDateTime(toqstr(t), format);
58         if (!loc_dt.isValid()) {
59                 LYXERR(Debug::LOCALE, "Could not parse `" << t
60                                 << "ยด (invalid format)");
61                 return static_cast<time_t>(-1);
62         }
63         loc_dt.setTimeSpec(Qt::UTC);
64         return loc_dt.toTime_t();
65 }
66
67 } // namespace support
68 } // namespace lyx