From: Michael Vrable Date: Thu, 2 May 2013 03:25:03 +0000 (-0700) Subject: Factor out time formatting functions. X-Git-Url: http://git.vrable.net/?p=cumulus.git;a=commitdiff_plain;h=5d386960f207f991833f1204748bc4323934f761 Factor out time formatting functions. --- diff --git a/main.cc b/main.cc index cd31189..ef8f2d7 100644 --- a/main.cc +++ b/main.cc @@ -814,24 +814,21 @@ int main(int argc, char *argv[]) /* Store the time when the backup started, so it can be included in the * snapshot name. */ time_t now; - struct tm time_buf_local, time_buf_utc; - char desc_buf[256]; time(&now); - localtime_r(&now, &time_buf_local); - gmtime_r(&now, &time_buf_utc); - strftime(desc_buf, sizeof(desc_buf), "%Y%m%dT%H%M%S", &time_buf_utc); + string timestamp + = TimeFormat::format(now, TimeFormat::FORMAT_FILENAME, true); /* Open the local database which tracks all objects that are stored * remotely, for efficient incrementals. Provide it with the name of this * snapshot. */ string database_path = localdb_dir + "/localdb.sqlite"; db = new LocalDb; - db->Open(database_path.c_str(), desc_buf, backup_scheme.c_str()); + db->Open(database_path.c_str(), timestamp.c_str(), backup_scheme.c_str()); tss = new TarSegmentStore(remote, db); /* Initialize the stat cache, for skipping over unchanged files. */ - metawriter = new MetadataWriter(tss, localdb_dir.c_str(), desc_buf, + metawriter = new MetadataWriter(tss, localdb_dir.c_str(), timestamp.c_str(), backup_scheme.c_str()); for (int i = optind; i < argc; i++) { @@ -854,7 +851,8 @@ int main(int argc, char *argv[]) string checksum_filename = "snapshot-"; if (backup_scheme.size() > 0) checksum_filename += backup_scheme + "-"; - checksum_filename = checksum_filename + desc_buf + "." + csum_type + "sums"; + checksum_filename + = checksum_filename + timestamp + "." + csum_type + "sums"; RemoteFile *checksum_file = remote->alloc_file(checksum_filename, "meta"); FILE *checksums = fdopen(checksum_file->get_fd(), "w"); @@ -905,7 +903,7 @@ int main(int argc, char *argv[]) string desc_filename = "snapshot-"; if (backup_scheme.size() > 0) desc_filename += backup_scheme + "-"; - desc_filename = desc_filename + desc_buf + ".cumulus"; + desc_filename = desc_filename + timestamp + ".cumulus"; RemoteFile *descriptor_file = remote->alloc_file(desc_filename, "snapshots"); @@ -925,9 +923,9 @@ int main(int argc, char *argv[]) fprintf(descriptor, "Format: Cumulus Snapshot v0.11\n"); fprintf(descriptor, "Producer: Cumulus %s\n", cumulus_version); - strftime(desc_buf, sizeof(desc_buf), "%Y-%m-%d %H:%M:%S %z", - &time_buf_local); - fprintf(descriptor, "Date: %s\n", desc_buf); + string timestamp_local + = TimeFormat::format(now, TimeFormat::FORMAT_LOCALTIME, false); + fprintf(descriptor, "Date: %s\n", timestamp_local.c_str()); if (backup_scheme.size() > 0) fprintf(descriptor, "Scheme: %s\n", backup_scheme.c_str()); fprintf(descriptor, "Root: %s\n", backup_root.c_str()); diff --git a/util.cc b/util.cc index a7479fc..76efe0f 100644 --- a/util.cc +++ b/util.cc @@ -31,6 +31,8 @@ #include #include +#include "util.h" + using std::map; using std::ostream; using std::string; @@ -134,3 +136,24 @@ void fatal(string msg) fprintf(stderr, "FATAL: %s\n", msg.c_str()); exit(1); } + +/* Available time formats. */ +const char TimeFormat::FORMAT_FILENAME[] = "%Y%m%dT%H%M%S"; +const char TimeFormat::FORMAT_ISO8601[] = "%Y-%m-%d %H:%M:%S"; +const char TimeFormat::FORMAT_LOCALTIME[] = "%Y-%m-%d %H:%M:%S %z"; + +static size_t MAX_TIMESTAMP_LENGTH = 1024; + +std::string TimeFormat::format(time_t timestamp, const char *format, bool utc) +{ + struct tm time_buf; + + if (utc) + gmtime_r(×tamp, &time_buf); + else + localtime_r(×tamp, &time_buf); + + char buffer[MAX_TIMESTAMP_LENGTH]; + strftime(buffer, MAX_TIMESTAMP_LENGTH, format, &time_buf); + return string(buffer); +} diff --git a/util.h b/util.h index c6c6f9e..0d1b45d 100644 --- a/util.h +++ b/util.h @@ -36,4 +36,25 @@ void cloexec(int fd); void fatal(std::string msg) __attribute__((noreturn)); +/* Date/time string formatting and parsing utility functions. All data and + * methods are static, so this class should not be instantiated. */ +class TimeFormat { +public: + // Abbreviated time format encoded in snapshot file names. + static const char FORMAT_FILENAME[]; + // A timestamp, in UTC, written out in an ISO 8601 format (compatible with + // the SQLite datetime function). + static const char FORMAT_ISO8601[]; + // Similar to the above, but including a timezone offset. + static const char FORMAT_LOCALTIME[]; + + static std::string format(time_t timestamp, const char *format, bool utc); + + static std::string isoformat(time_t timestamp) + { return format(timestamp, FORMAT_ISO8601, true); } + +private: + TimeFormat() { } +}; + #endif // _LBS_TARSTORE_H