]> Dogcows Code - chaz/yoink/blobdiff - src/stlplus/portability/time.cpp
testing new non-autotools build system
[chaz/yoink] / src / stlplus / portability / time.cpp
diff --git a/src/stlplus/portability/time.cpp b/src/stlplus/portability/time.cpp
new file mode 100644 (file)
index 0000000..250a825
--- /dev/null
@@ -0,0 +1,129 @@
+////////////////////////////////////////////////////////////////////////////////\r
+\r
+//   Author:    Andy Rushton\r
+//   Copyright: (c) Southampton University 1999-2004\r
+//              (c) Andy Rushton           2004-2009\r
+//   License:   BSD License, see ../docs/license.html\r
+\r
+////////////////////////////////////////////////////////////////////////////////\r
+#include "time.hpp"\r
+#include "dprintf.hpp"\r
+#include <ctype.h>\r
+////////////////////////////////////////////////////////////////////////////////\r
+\r
+namespace stlplus\r
+{\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+\r
+  time_t time_now(void)\r
+  {\r
+    return time(0);\r
+  }\r
+\r
+  time_t localtime_create(int year, int month, int day, int hour, int minute, int second)\r
+  {\r
+    tm tm_time;\r
+    tm_time.tm_year = year-1900;  // years are represented as an offset from 1900, for reasons unknown\r
+    tm_time.tm_mon = month-1;     // internal format represents month as 0-11, but it is friendlier to take an input 1-12\r
+    tm_time.tm_mday = day;\r
+    tm_time.tm_hour = hour;\r
+    tm_time.tm_min = minute;\r
+    tm_time.tm_sec = second;\r
+    tm_time.tm_isdst = -1;        // specify that the function should work out daylight savings\r
+    time_t result = mktime(&tm_time);\r
+    return result;\r
+  }\r
+\r
+  int localtime_year(time_t t)\r
+  {\r
+    tm* tm_time = localtime(&t);\r
+    return tm_time->tm_year + 1900;\r
+  }\r
+\r
+  int localtime_month(time_t t)\r
+  {\r
+    tm* tm_time = localtime(&t);\r
+    return tm_time->tm_mon + 1;\r
+  }\r
+\r
+  int localtime_day(time_t t)\r
+  {\r
+    tm* tm_time = localtime(&t);\r
+    return tm_time->tm_mday;\r
+  }\r
+\r
+  int localtime_hour(time_t t)\r
+  {\r
+    tm* tm_time = localtime(&t);\r
+    return tm_time->tm_hour;\r
+  }\r
+\r
+  int localtime_minute(time_t t)\r
+  {\r
+    tm* tm_time = localtime(&t);\r
+    return tm_time->tm_min;\r
+  }\r
+\r
+  int localtime_second(time_t t)\r
+  {\r
+    tm* tm_time = localtime(&t);\r
+    return tm_time->tm_sec;\r
+  }\r
+\r
+  int localtime_weekday(time_t t)\r
+  {\r
+    tm* tm_time = localtime(&t);\r
+    return tm_time->tm_wday;\r
+  }\r
+\r
+  int localtime_yearday(time_t t)\r
+  {\r
+    tm* tm_time = localtime(&t);\r
+    return tm_time->tm_yday;\r
+  }\r
+\r
+  std::string localtime_string(time_t t)\r
+  {\r
+    tm* local = localtime(&t);\r
+    std::string result = local ? asctime(local) : "*time not available*";\r
+    // ctime appends a newline for no apparent reason - clean up\r
+    while (!result.empty() && isspace(result[result.size()-1]))\r
+      result.erase(result.size()-1,1);\r
+    return result;\r
+  }\r
+\r
+  std::string delaytime_string(time_t seconds)\r
+  {\r
+    unsigned minutes = (unsigned)seconds / 60;\r
+    seconds %= 60;\r
+    unsigned hours = minutes / 60;\r
+    minutes %= 60;\r
+    unsigned days = hours / 24;\r
+    hours %= 24;\r
+    unsigned weeks = days / 7;\r
+    days %= 7;\r
+    std::string result;\r
+    if (weeks > 0)\r
+      result += dformat("%dw ",weeks);\r
+    if (!result.empty() || days > 0)\r
+      result += dformat("%dd ", days);\r
+    if (!result.empty() || hours > 0)\r
+      result += dformat("%d:", hours);\r
+    if (!result.empty() || minutes > 0)\r
+    {\r
+      if (!result.empty())\r
+        result += dformat("%02d:", minutes);\r
+      else\r
+        result += dformat("%d:", minutes);\r
+    }\r
+    if (!result.empty())\r
+      result += dformat("%02d:", seconds);\r
+    else\r
+      result += dformat("%ds", seconds);\r
+    return result;\r
+  }\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+\r
+} // end namespace stlplus\r
This page took 0.021925 seconds and 4 git commands to generate.