Add time_util.

Time string utility functions.

Change-Id: I0ffdc54f386a58f697e18601eaeadebaf041c3f7
diff --git a/encoder/CMakeLists.txt b/encoder/CMakeLists.txt
index 46e202f..38b03d0 100644
--- a/encoder/CMakeLists.txt
+++ b/encoder/CMakeLists.txt
@@ -125,6 +125,8 @@
                file_writer.h
                http_uploader.cc
                http_uploader.h
+               time_util.cc
+               time_util.h
                video_encoder.cc
                video_encoder.h
                vorbis_encoder.cc
diff --git a/encoder/time_util.cc b/encoder/time_util.cc
new file mode 100644
index 0000000..ffce401
--- /dev/null
+++ b/encoder/time_util.cc
@@ -0,0 +1,67 @@
+// Copyright (c) 2015 The WebM project authors. All Rights Reserved.
+//
+// Use of this source code is governed by a BSD-style license
+// that can be found in the LICENSE file in the root of the source
+// tree. An additional intellectual property rights grant can be found
+// in the file PATENTS.  All contributing project authors may
+// be found in the AUTHORS file in the root of the source tree.
+#include "encoder/time_util.h"
+
+#include "glog/logging.h"
+
+namespace webmlive {
+
+const size_t kTimeUtilBufferSize = 256;
+
+std::string LocalDateString() {
+  char date_buffer[kTimeUtilBufferSize] = {0};
+  // %Y - year
+  // %m - month, zero padded (01-12)
+  // %d - day of month, zero padded (01-31).
+  const char format_string[] = "%Y%m%d";
+  const time_t raw_time_now = time(NULL);
+  const struct tm* time_now = localtime(&raw_time_now);
+
+  if (strftime(&date_buffer[0], sizeof(date_buffer), format_string,
+               time_now) == 0) {
+    LOG(ERROR) << "DateString failed..";
+    return "";
+  }
+
+  const std::string date_string = date_buffer;
+  return date_string;
+}
+
+std::string LocalTimeString() {
+  char time_buffer[kTimeUtilBufferSize] = {0};
+  // %H - hour, zero padded, 24 hour clock (00-23)
+  // %M - minute, zero padded (00-59)
+  // %S - second, zero padded (00-61)
+  const char format_string[] = "%H%M%S";
+  const time_t raw_time_now = time(NULL);
+  const struct tm* time_now = localtime(&raw_time_now);
+
+  if (strftime(&time_buffer[0], sizeof(time_buffer), format_string,
+               time_now) == 0) {
+    LOG(ERROR) << "TimeString failed.";
+    return "";
+  }
+
+  const std::string time_string = time_buffer;
+  return time_string;
+}
+
+std::string StrFTime(const struct tm* time_value,
+                     const std::string& format_string) {
+  char strftime_buffer[kTimeUtilBufferSize] = {0};
+  if (strftime(&strftime_buffer[0], sizeof(strftime_buffer),
+               format_string.c_str(), time_value) == 0) {
+    LOG(ERROR) << "StrFTime failed.";
+    return "";
+  }
+
+  const std::string out_string = strftime_buffer;
+  return out_string;
+}
+
+}  // namespace webmlive
diff --git a/encoder/time_util.h b/encoder/time_util.h
new file mode 100644
index 0000000..6b3efaa
--- /dev/null
+++ b/encoder/time_util.h
@@ -0,0 +1,31 @@
+// Copyright (c) 2015 The WebM project authors. All Rights Reserved.
+//
+// Use of this source code is governed by a BSD-style license
+// that can be found in the LICENSE file in the root of the source
+// tree. An additional intellectual property rights grant can be found
+// in the file PATENTS.  All contributing project authors may
+// be found in the AUTHORS file in the root of the source tree.
+#ifndef WEBMLIVE_ENCODER_TIME_UTIL_H_
+#define WEBMLIVE_ENCODER_TIME_UTIL_H_
+
+#include <ctime>
+#include <string>
+
+namespace webmlive {
+
+// Time string utility functions. All return the string noted or an empty string
+// upon failure.
+
+// Returns current date in the format: YYYYMMDD.
+std::string LocalDateString();
+
+// Returns current time in the format: HHMMSS.
+std::string LocalTimeString();
+
+// Returns user requested time string.
+std::string StrFTime(const struct tm* time_value,
+                     const std::string& format_string);
+
+}  // namespace webmlive
+
+#endif  // WEBMLIVE_ENCODER_TIME_UTIL_H_
\ No newline at end of file