Home | History | Annotate | Download | only in cloud
      1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #include "tensorflow/core/platform/cloud/time_util.h"
     17 #include <time.h>
     18 #include <cmath>
     19 #include <cstdio>
     20 #include <ctime>
     21 #ifdef _WIN32
     22 #define timegm _mkgmtime
     23 #endif
     24 #include "tensorflow/core/lib/core/errors.h"
     25 
     26 namespace tensorflow {
     27 
     28 namespace {
     29 constexpr int64 kNanosecondsPerSecond = 1000 * 1000 * 1000;
     30 
     31 }  // namespace
     32 
     33 // Only implements one special case of RFC 3339 which is returned by
     34 // GCS API, e.g 2016-04-29T23:15:24.896Z.
     35 Status ParseRfc3339Time(const string& time, int64* mtime_nsec) {
     36   tm parsed{0};
     37   float seconds;
     38   if (sscanf(time.c_str(), "%4d-%2d-%2dT%2d:%2d:%fZ", &(parsed.tm_year),
     39              &(parsed.tm_mon), &(parsed.tm_mday), &(parsed.tm_hour),
     40              &(parsed.tm_min), &seconds) != 6) {
     41     return errors::Internal(
     42         strings::StrCat("Unrecognized RFC 3339 time format: ", time));
     43   }
     44   const int int_seconds = floor(seconds);
     45   parsed.tm_year -= 1900;  // tm_year expects years since 1900.
     46   parsed.tm_mon -= 1;      // month is zero-based.
     47   parsed.tm_sec = int_seconds;
     48 
     49   *mtime_nsec = timegm(&parsed) * kNanosecondsPerSecond +
     50                 static_cast<int64>(
     51                     floor((seconds - int_seconds) * kNanosecondsPerSecond));
     52 
     53   return Status::OK();
     54 }
     55 
     56 }  // namespace tensorflow
     57