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 string const formatted_time(time_t t, string const & fmt)
36 struct tm * loc_tm = localtime(&t);
38 strftime(date, sizeof(date), fmt.c_str(), loc_tm);
43 time_t from_ctime(string t)
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 // toDateTime() is too stupid to recognize variable amounts of
49 // whitespace (needed because ctime() outputs double spaces before
50 // single digit day numbers and hours)
51 t = subst(t, " ", " ");
52 QString const format("ddd MMM d H:mm:ss yyyy");
54 QDateTime loc_dt = loc.toDateTime(toqstr(t), format);
55 if (!loc_dt.isValid()) {
56 LYXERR(Debug::LOCALE, "Could not parse `" << t
57 << "ยด (invalid format)");
58 return static_cast<time_t>(-1);
60 return loc_dt.toTime_t();
63 } // namespace support