]> git.lyx.org Git - lyx.git/blob - src/support/lyxtime.cpp
Account for old versions of Pygments
[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 string const formatted_time(time_t t, string const & fmt)
35 {
36         struct tm * loc_tm = localtime(&t);
37         char date[50];
38         strftime(date, sizeof(date), fmt.c_str(), loc_tm);
39         return string(date);
40 }
41
42
43 docstring formatted_datetime(time_t t, string const & fmt)
44 {
45         QString qres;
46         if (fmt.empty())
47                 qres = QLocale().toString(QDateTime::fromTime_t(t),
48                                           QLocale::ShortFormat);
49         else
50                 qres = QLocale().toString(QDateTime::fromTime_t(t), toqstr(fmt));
51         return qstring_to_ucs4(qres);
52 }
53
54
55 time_t from_asctime_utc(string t)
56 {
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");
65         QLocale loc("C");
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);
71         }
72         loc_dt.setTimeSpec(Qt::UTC);
73         return loc_dt.toTime_t();
74 }
75
76 } // namespace support
77 } // namespace lyx