2019-09-11 10:13:59 +00:00
|
|
|
#ifndef slic3r_Utils_Time_hpp_
|
|
|
|
#define slic3r_Utils_Time_hpp_
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <ctime>
|
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
namespace Utils {
|
|
|
|
|
2019-09-24 08:48:24 +00:00
|
|
|
// Should be thread safe.
|
2019-09-11 10:13:59 +00:00
|
|
|
time_t get_current_time_utc();
|
|
|
|
|
|
|
|
enum class TimeZone { local, utc };
|
2019-09-24 08:48:24 +00:00
|
|
|
enum class TimeFormat { gcode, iso8601Z };
|
2019-09-11 10:13:59 +00:00
|
|
|
|
2019-09-24 08:48:24 +00:00
|
|
|
// time_t to string functions...
|
2019-09-11 10:13:59 +00:00
|
|
|
|
2019-09-24 08:48:24 +00:00
|
|
|
std::string time2str(const time_t &t, TimeZone zone, TimeFormat fmt);
|
|
|
|
|
|
|
|
inline std::string time2str(TimeZone zone, TimeFormat fmt)
|
2019-09-11 10:13:59 +00:00
|
|
|
{
|
|
|
|
return time2str(get_current_time_utc(), zone, fmt);
|
|
|
|
}
|
|
|
|
|
2019-09-24 08:48:24 +00:00
|
|
|
inline std::string utc_timestamp(time_t t)
|
|
|
|
{
|
|
|
|
return time2str(t, TimeZone::utc, TimeFormat::gcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string utc_timestamp()
|
|
|
|
{
|
|
|
|
return utc_timestamp(get_current_time_utc());
|
|
|
|
}
|
|
|
|
|
|
|
|
// String to time_t function. Returns time_t(-1) if fails to parse the input.
|
|
|
|
time_t str2time(const std::string &str, TimeZone zone, TimeFormat fmt);
|
|
|
|
|
|
|
|
|
|
|
|
// /////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Utilities to convert an UTC time_t to/from an ISO8601 time format,
|
|
|
|
// useful for putting timestamps into file and directory names.
|
|
|
|
// Returns (time_t)-1 on error.
|
|
|
|
|
|
|
|
// Use these functions to convert safely to and from the ISO8601 format on
|
|
|
|
// all platforms
|
|
|
|
|
|
|
|
inline std::string iso_utc_timestamp(time_t t)
|
|
|
|
{
|
|
|
|
return time2str(t, TimeZone::utc, TimeFormat::gcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string iso_utc_timestamp()
|
2019-09-11 10:13:59 +00:00
|
|
|
{
|
2019-09-24 08:48:24 +00:00
|
|
|
return iso_utc_timestamp(get_current_time_utc());
|
2019-09-11 10:13:59 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 08:48:24 +00:00
|
|
|
inline time_t parse_iso_utc_timestamp(const std::string &str)
|
2019-09-11 10:13:59 +00:00
|
|
|
{
|
2019-09-24 08:48:24 +00:00
|
|
|
return str2time(str, TimeZone::utc, TimeFormat::iso8601Z);
|
2019-09-11 10:13:59 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 08:48:24 +00:00
|
|
|
// /////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
} // namespace Utils
|
|
|
|
} // namespace Slic3r
|
2019-09-11 10:13:59 +00:00
|
|
|
|
|
|
|
#endif /* slic3r_Utils_Time_hpp_ */
|