Create the portability header for time.h
authorChristopher Dykes <cdykes@fb.com>
Wed, 2 Mar 2016 00:10:58 +0000 (16:10 -0800)
committerFacebook Github Bot 9 <facebook-github-bot-9-bot@fb.com>
Wed, 2 Mar 2016 00:20:36 +0000 (16:20 -0800)
Summary: Although MSVC does have time.h, and does have equivelents of these functions, those have slightly different semantics so we have to wrap them.

Reviewed By: yfeldblum

Differential Revision: D2983613

fb-gh-sync-id: 676cd524ffa834a4250a2acc76aa1200eefe62cc
shipit-source-id: 676cd524ffa834a4250a2acc76aa1200eefe62cc

folly/Makefile.am
folly/portability/Time.cpp [new file with mode: 0755]
folly/portability/Time.h [new file with mode: 0755]

index e910a2cb3ceb84618529efe7e580290f8390a345..0dc1d29e44bb0f6511f75d9ad49abf4fe4bf681a 100644 (file)
@@ -271,6 +271,7 @@ nobase_follyinclude_HEADERS = \
        portability/Syscall.h \
        portability/SysTime.h \
        portability/SysUio.h \
+       portability/Time.h \
        Preprocessor.h \
        ProducerConsumerQueue.h \
        Random.h \
@@ -397,6 +398,7 @@ libfolly_la_SOURCES = \
        MemoryMapping.cpp \
        portability/Environment.cpp \
        portability/SysTime.cpp \
+       portability/Time.cpp \
        Random.cpp \
        SafeAssert.cpp \
        SharedMutex.cpp \
diff --git a/folly/portability/Time.cpp b/folly/portability/Time.cpp
new file mode 100755 (executable)
index 0000000..0a06abd
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2016 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <folly/portability/Time.h>
+
+#ifdef _WIN32
+#include <iomanip>
+#include <sstream>
+
+#include <Windows.h>
+
+extern "C" {
+char* asctime_r(const tm* tm, char* buf) {
+  char tmpBuf[64];
+  if (asctime_s(tmpBuf, tm)) {
+    return nullptr;
+  }
+  // Nothing we can do if the buff is to small :(
+  return strcpy(buf, tmpBuf);
+}
+
+char* ctime_r(const time_t* t, char* buf) {
+  char tmpBuf[64];
+  if (ctime_s(tmpBuf, t)) {
+    return nullptr;
+  }
+  // Nothing we can do if the buff is to small :(
+  return strcpy(buf, tmpBuf);
+}
+
+tm* gmtime_r(const time_t* t, tm* res) {
+  if (!gmtime_s(res, t)) {
+    return res;
+  }
+  return nullptr;
+}
+
+tm* localtime_r(const time_t* t, tm* o) {
+  if (!localtime_s(o, t)) {
+    return o;
+  }
+  return nullptr;
+}
+
+int nanosleep(const struct timespec* request, struct timespec* remain) {
+  Sleep((DWORD)((request->tv_sec * 1000) + (request->tv_nsec / 1000000));
+  remain->tv_nsec = 0;
+  remain->tv_sec = 0;
+  return 0;
+}
+
+char* strptime(const char* __restrict s,
+               const char* __restrict f,
+               struct tm* __restrict tm) {
+  // Isn't the C++ standard lib nice? std::get_time is defined such that its
+  // format parameters are the exact same as strptime. Of course, we have to
+  // create a string stream first, and imbue it with the current C locale, and
+  // we also have to make sure we return the right things if it fails, or
+  // if it succeeds, but this is still far simpler an implementation than any
+  // of the versions in any of the C standard libraries.
+  std::istringstream input(s);
+  input.imbue(std::locale(setlocale(LC_ALL, nullptr)));
+  input >> std::get_time(tm, f);
+  if (input.fail()) {
+    return nullptr;
+  }
+  return const_cast<char*>(s + input.tellg());
+}
+}
+#endif
diff --git a/folly/portability/Time.h b/folly/portability/Time.h
new file mode 100755 (executable)
index 0000000..0a35b23
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2016 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <time.h>
+
+#ifdef _WIN32
+#define TM_YEAR_BASE (1900)
+
+extern "C" {
+char* asctime_r(const tm* tm, char* buf);
+char* ctime_r(const time_t* t, char* buf);
+tm* gmtime_r(const time_t* t, tm* res);
+tm* localtime_r(const time_t* t, tm* o);
+int nanosleep(const struct timespec* request, struct timespec* remain);
+char* strptime(const char* __restrict buf,
+               const char* __restrict fmt,
+               struct tm* __restrict tm);
+}
+#endif