Shaka Packager SDK
test_clock.cc
1 // Copyright 2023 Google LLC. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include <packager/utils/test_clock.h>
8 
9 #include <ctime>
10 #include <iostream>
11 #include <string>
12 
13 #include <absl/strings/str_split.h>
14 #include <absl/strings/string_view.h>
15 
16 std::tm parseISO8601(const std::string& date_string) {
17  std::tm tm = {};
18  std::vector<absl::string_view> date_time_parts =
19  absl::StrSplit(date_string, 'T');
20  if (date_time_parts.size() == 2) {
21  std::vector<absl::string_view> date_parts =
22  absl::StrSplit(date_time_parts[0], '-');
23  std::vector<absl::string_view> time_parts =
24  absl::StrSplit(date_time_parts[1], ':');
25  if (date_parts.size() == 3 && time_parts.size() == 3) {
26  tm.tm_year = std::stoi(std::string(date_parts[0])) - 1900;
27  tm.tm_mon = std::stoi(std::string(date_parts[1])) - 1;
28  tm.tm_mday = std::stoi(std::string(date_parts[2]));
29  tm.tm_hour = std::stoi(std::string(time_parts[0]));
30  tm.tm_min = std::stoi(std::string(time_parts[1]));
31  tm.tm_sec = std::stoi(std::string(time_parts[2]));
32  }
33  }
34  return tm;
35 }
36 
37 shaka::TestClock::TestClock(std::string utc_time_8601) {
38  std::tm tm = parseISO8601(utc_time_8601);
39  std::time_t utc_time_t = std::mktime(&tm);
40  std::time_t offset = utc_time_t - std::mktime(std::gmtime(&utc_time_t));
41  mock_time_ = std::chrono::system_clock::from_time_t(utc_time_t + offset);
42 }