Advertisement
DasShelmer

9.s.3

Dec 10th, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <ctime>
  5. #include <sstream>
  6. #include <iomanip>
  7. #include <algorithm>
  8. using namespace std;
  9.  
  10. bool correctLine(const string& l, const string& pattern) {
  11.     static const char digit = 'x';// символ для цифр(циферный символ для шаблона)
  12.     int lenght = pattern.length();
  13.     if (l.length() != lenght)// если длина не совпадает с длиной шаблона, то строка не подходит
  14.         return false;
  15.     for (int i = 0; i < lenght; i++)
  16.         // если символ в шаблоне циферный, то проверяем на "циферность" символа в строке,
  17.         // иначе просто сопоставляем символы строки и шаблона
  18.         if (pattern[i] == digit ? !isdigit(l.at(i)) : pattern[i] != l[i])
  19.             return false;
  20.     return true;
  21. }
  22. // Converts UTC time string to a time_t value.
  23. time_t getTime(string dateTime) {
  24.     static const string dateTimeFormat = "%dT-%m-%y";
  25.     istringstream ss(dateTime);
  26.     tm dt = { 0, 0, 0, 0, 0, 0 , 0 , 0 , 0};
  27.     dateTime.replace(dateTime.begin(), dateTime.end(), '.', '-');
  28.     // Записываем данные в структуру согласно шаблону
  29.     ss >> get_time(&dt, dateTimeFormat.c_str());
  30.     // Возвращаем как кол-во секунд
  31.     return mktime(&dt);
  32. }
  33. int main() {
  34.     static const time_t oneday = 60 * 60 * 24;
  35.     ifstream in ("f1.txt");
  36.     ofstream out("f2.txt");
  37.     const string pattern = "xx.xx.xx";
  38.     string temp;
  39.     time_t current = 0, before = 0;
  40.     tm cur, bef;
  41.     char buffer[8];
  42.  
  43.     while (in)// пока файл не кончился
  44.         if (in >> temp && correctLine(temp, pattern)) { // пока он содержит слова, записываем в temp слово
  45.             current = getTime(temp);
  46.             before = current - oneday;
  47.             gmtime_s(&cur, &current);
  48.             gmtime_s(&bef, &before);
  49.             strftime(buffer, 8, "%dT.%m.%Y", &cur);
  50.             out << buffer << ' ';
  51.             strftime(buffer, 8, "%dT.%m.%Y", &bef);
  52.             out << buffer << endl;
  53.         }
  54.  
  55.     in.close();
  56.     out.close();
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement