]> git.lyx.org Git - lyx.git/blob - src/support/lyxtime.cpp
Strip gecos on Unix systems on user name only.
[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 time_t from_ctime(string t)
44 {
45         // Example for the format: "Sun Nov  6 10:39:39 2011\n"
46         // Generously remove trailing '\n' (and other whitespace if needed)
47         t = trim(t, " \t\r\n");
48 #if QT_VERSION >= 0x040400
49         // toDateTime() is too stupid to recognize variable amounts of
50         // whitespace (needed because ctime() outputs double spaces before
51         // single digit day numbers and hours)
52         t = subst(t, "  ", " ");
53         QString const format("ddd MMM d H:mm:ss yyyy");
54         QLocale loc("C");
55         QDateTime loc_dt = loc.toDateTime(toqstr(t), format);
56         if (!loc_dt.isValid()) {
57                 LYXERR(Debug::LOCALE, "Could not parse `" << t
58                                 << "´ (invalid format)");
59                 return static_cast<time_t>(-1);
60         }
61         return loc_dt.toTime_t();
62 #elif defined(_WIN32)
63 #error "The minimum required Qt version on windows is Qt 4.4."
64 #else
65         // strptime() is not available on windows (defined by POSIX)
66
67         // strptime() uses the current locale, so we need to switch to "C"
68         LYXERR(Debug::LOCALE, "Setting LC_ALL and LC_TIME to C");
69         string oldLC_ALL = getEnv("LC_ALL");
70         string oldLC_TIME = getEnv("LC_TIME");
71         if (!setEnv("LC_ALL", "C"))
72                 LYXERR(Debug::LOCALE, "\t... LC_ALL failed!");
73         if (!setEnv("LC_TIME", "C"))
74                 LYXERR(Debug::LOCALE, "\t... LC_TIME failed!");
75
76         struct tm loc_tm;
77         char const * const format = "%a%n%b%n%d%n%T%n%Y";
78         char * remainder = strptime(t.c_str(), format, &loc_tm);
79
80         LYXERR(Debug::LOCALE, "Resetting LC_ALL and LC_TIME");
81         if(!setEnv("LC_TIME", oldLC_TIME))
82                 LYXERR(Debug::LOCALE, "\t... LC_TIME failed!");
83         if (!setEnv("LC_ALL", oldLC_ALL))
84                 LYXERR(Debug::LOCALE, "\t... LC_ALL failed!");
85
86         if (!remainder) {
87                 LYXERR(Debug::LOCALE, "Could not parse `" << t
88                                 << "´ (invalid format)");
89                 return static_cast<time_t>(-1);
90         }
91         if (*remainder != '\0') {
92                 LYXERR(Debug::LOCALE, "Could not parse `" << t
93                                 << "´ (excess characters)");
94                 return static_cast<time_t>(-1);
95         }
96         return mktime(&loc_tm);
97 #endif
98 }
99
100 } // namespace support
101 } // namespace lyx