Advertisement
OIQ

Untitled

OIQ
Oct 3rd, 2021
937
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. const int64_t dayToSec = 24 * 60 * 60;
  5.  
  6. int64_t FromTimeToSeconds(const std::string& str) {
  7.     int64_t h = 0, m = 0, s = 0, t = 0;
  8.     size_t index = 0;
  9.     std::string hours, minutes, seconds;
  10.     int counter = 0;
  11.     for (char i : str) {
  12.         if (i == ':') {
  13.             counter++;
  14.         }
  15.     }
  16.     if (counter == 2) {
  17.         while (str[index] != ':') {
  18.             hours += str[index];
  19.             index++;
  20.         }
  21.         index++;
  22.         for (char hour : hours) {
  23.             h = h * 10 + static_cast<int>(hour) - '0';
  24.         }
  25.         t += h * 60 * 60;
  26.     }
  27.     if (counter >= 1) {
  28.         while (str[index] != ':') {
  29.             minutes += str[index];
  30.             index++;
  31.         }
  32.         index++;
  33.         for (char minute : minutes) {
  34.             m = m * 10 + static_cast<int>(minute) - '0';
  35.         }
  36.         t += m * 60;
  37.     }
  38.     while (index < str.size()) {
  39.         seconds += str[index];
  40.         index++;
  41.     }
  42.     for (char second : seconds) {
  43.         s = s * 10 + static_cast<int>(second) - '0';
  44.     }
  45.  
  46.     return t + s;
  47. }
  48.  
  49. std::string timerAlarm(int64_t start, int64_t timer) {
  50.     std::string result;
  51.     int64_t days = 0;
  52.     int64_t time = (start + timer) % (dayToSec);
  53.  
  54.     if (time < start) {
  55.         ++days;
  56.     }
  57.  
  58.     days += timer / dayToSec;
  59.  
  60.     int64_t hours = time / (60 * 60);
  61.     int64_t minutes = (time % (60 * 60)) / 60;
  62.     int64_t seconds = time % 60;
  63.     if (hours < 10) {
  64.         result += '0';
  65.     }
  66.     result += (std::to_string(hours) + ':');
  67.     if (minutes < 10) {
  68.         result += '0';
  69.     }
  70.     result += (std::to_string(minutes) + ':');
  71.     if (seconds < 10) {
  72.         result += '0';
  73.     }
  74.     result += std::to_string(seconds);
  75.     if (days >= 1) {
  76.         result += ("+" + std::to_string(days) + " days");
  77.     }
  78.     return result;
  79. }
  80.  
  81.  
  82. int main() {
  83.     std::string start;
  84.     std::string timer;
  85.  
  86.  
  87.     std::cin >> start;
  88.     std::cin >> timer;
  89.  
  90.     int64_t int_start = FromTimeToSeconds(start);
  91.     int64_t int_timer = FromTimeToSeconds(timer);
  92.  
  93.     std::cout << timerAlarm(int_start, int_timer);
  94.  
  95.     return 0;
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement