From 392cbb817eb440eebae6f94af236337d60126eca Mon Sep 17 00:00:00 2001 From: mpc <mpc> Date: Sat, 17 Jul 2004 03:11:20 +0000 Subject: [PATCH] cleaned up time class --- apps/enclave/libsockthread/src/platform.hpp | 13 +++++- apps/enclave/libsockthread/src/time.cpp | 47 ++++++++------------- apps/enclave/libsockthread/src/time.hpp | 16 ++++--- 3 files changed, 41 insertions(+), 35 deletions(-) diff --git a/apps/enclave/libsockthread/src/platform.hpp b/apps/enclave/libsockthread/src/platform.hpp index 328d8e0c00..6b5c847988 100644 --- a/apps/enclave/libsockthread/src/platform.hpp +++ b/apps/enclave/libsockthread/src/platform.hpp @@ -28,13 +28,19 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +/* + * Global includes and platform configuration. This is used to compile the + * library, but is not intended for use by users of the library in their + * programs. + */ + #ifndef LIBSOCKTHREAD_PLATFORM_HPP #define LIBSOCKTHREAD_PLATFORM_HPP /* * Operating system */ -#define FREEBSD 0 // FreeBSD (untested) +#define FREEBSD 0 // FreeBSD #define MINGW 1 // Windows native (Mingw) #define LINUX 2 // Linux #define CYGWIN 3 // Cygwin @@ -60,4 +66,9 @@ #define NO_INET_PTON #endif +#include <ctime> +#include <iostream> +#include <string> +using namespace std; + #endif // LIBSOCKTHREAD_PLATFORM_HPP diff --git a/apps/enclave/libsockthread/src/time.cpp b/apps/enclave/libsockthread/src/time.cpp index e9c89f52ed..21a255c7a6 100644 --- a/apps/enclave/libsockthread/src/time.cpp +++ b/apps/enclave/libsockthread/src/time.cpp @@ -28,66 +28,55 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include <ctime> -#include <string> -using namespace std; +#include "platform.hpp" #include "time.hpp" using namespace Libsockthread; /* - * Converts the time to an ISO 8601 standard time and date and puts it in a - * string + * Converts the time to an ISO 8601 standard date and time * Example: 2004-07-01T19:03:47Z */ -string& Time::utc(string &s) const +string& Time::utc() { - struct tm* tm; - - tm = gmtime(&unixtime); + struct tm* tm = gmtime(&unixtime); char t[21]; strftime(t, sizeof t, "%Y-%m-%dT%H:%M:%SZ", tm); - return s = t; + return formatted = t; } /* - * Converts the time to an ISO 8601 standard date and puts it in a string + * Converts the time to an ISO 8601 standard date * Example: 2004-07-01Z */ -string& Time::utc_date(string &s) const +string& Time::utc_date() { - struct tm* tm; - - tm = gmtime(&unixtime); + struct tm* tm = gmtime(&unixtime); char t[12]; strftime(t, sizeof t, "%Y-%m-%dZ", tm); - return s = t; + return formatted = t; } /* - * Converts the time to an ISO 8601 standard time and puts it in a string + * Converts the time to an ISO 8601 standard time * Example: 19:03:47Z */ -string& Time::utc_time(string &s) const +string& Time::utc_time() { - struct tm* tm; - - tm = gmtime(&unixtime); + struct tm* tm = gmtime(&unixtime); char t[10]; strftime(t, sizeof t, "%H:%M:%SZ", tm); - return s = t; + return formatted = t; } #ifdef UNIT_TEST // g++ -Wall -DUNIT_TEST time.cpp -o time -#include <iostream> - -int main(void) +int main() { Time t; - string s; - cout << "Current date and time is " << t.utc(s) << '\n'; - cout << "Current date is " << t.utc_date(s) << '\n'; - cout << "Current time is " << t.utc_time(s) << '\n'; + cout << "Current date and time is " << t.utc() << '\n'; + cout << "Current date is " << t.utc_date() << '\n'; + cout << "Current time is " << t.utc_time() << '\n'; + cout << "Formatted time is " << t.get_formatted() << " (should be the same)\n"; return 0; } diff --git a/apps/enclave/libsockthread/src/time.hpp b/apps/enclave/libsockthread/src/time.hpp index 6a439b507c..89e81891b0 100644 --- a/apps/enclave/libsockthread/src/time.hpp +++ b/apps/enclave/libsockthread/src/time.hpp @@ -34,13 +34,19 @@ namespace Libsockthread { class Time { public: - Time(void) { now(); } + Time() + { now(); } + + string& get_formatted() + { return formatted; } + void now() + { unixtime = time(0); } + string& utc(); + string& utc_date(); + string& utc_time(); - void now(void) { unixtime = time(0); } - string& utc(string &s) const; - string& utc_date(string &s) const; - string& utc_time(string &s) const; private: + string formatted; time_t unixtime; }; } -- GitLab