2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/portability/Time.h>
26 char* asctime_r(const tm* tm, char* buf) {
28 if (asctime_s(tmpBuf, tm)) {
31 // Nothing we can do if the buff is to small :(
32 return strcpy(buf, tmpBuf);
35 char* ctime_r(const time_t* t, char* buf) {
37 if (ctime_s(tmpBuf, t)) {
40 // Nothing we can do if the buff is to small :(
41 return strcpy(buf, tmpBuf);
44 tm* gmtime_r(const time_t* t, tm* res) {
45 if (!gmtime_s(res, t)) {
51 tm* localtime_r(const time_t* t, tm* o) {
52 if (!localtime_s(o, t)) {
58 int nanosleep(const struct timespec* request, struct timespec* remain) {
59 Sleep((DWORD)((request->tv_sec * 1000) + (request->tv_nsec / 1000000));
65 char* strptime(const char* __restrict s,
66 const char* __restrict f,
67 struct tm* __restrict tm) {
68 // Isn't the C++ standard lib nice? std::get_time is defined such that its
69 // format parameters are the exact same as strptime. Of course, we have to
70 // create a string stream first, and imbue it with the current C locale, and
71 // we also have to make sure we return the right things if it fails, or
72 // if it succeeds, but this is still far simpler an implementation than any
73 // of the versions in any of the C standard libraries.
74 std::istringstream input(s);
75 input.imbue(std::locale(setlocale(LC_ALL, nullptr)));
76 input >> std::get_time(tm, f);
80 return const_cast<char*>(s + input.tellg());