Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <ctime>
- #include <sstream>
- #include <iomanip>
- #include <algorithm>
- using namespace std;
- bool correctLine(const string& l, const string& pattern) {
- static const char digit = 'x';// символ для цифр(циферный символ для шаблона)
- int lenght = pattern.length();
- if (l.length() != lenght)// если длина не совпадает с длиной шаблона, то строка не подходит
- return false;
- for (int i = 0; i < lenght; i++)
- // если символ в шаблоне циферный, то проверяем на "циферность" символа в строке,
- // иначе просто сопоставляем символы строки и шаблона
- if (pattern[i] == digit ? !isdigit(l.at(i)) : pattern[i] != l[i])
- return false;
- return true;
- }
- // Converts UTC time string to a time_t value.
- time_t getTime(string dateTime) {
- static const string dateTimeFormat = "%dT-%m-%y";
- istringstream ss(dateTime);
- tm dt = { 0, 0, 0, 0, 0, 0 , 0 , 0 , 0};
- dateTime.replace(dateTime.begin(), dateTime.end(), '.', '-');
- // Записываем данные в структуру согласно шаблону
- ss >> get_time(&dt, dateTimeFormat.c_str());
- // Возвращаем как кол-во секунд
- return mktime(&dt);
- }
- int main() {
- static const time_t oneday = 60 * 60 * 24;
- ifstream in ("f1.txt");
- ofstream out("f2.txt");
- const string pattern = "xx.xx.xx";
- string temp;
- time_t current = 0, before = 0;
- tm cur, bef;
- char buffer[8];
- while (in)// пока файл не кончился
- if (in >> temp && correctLine(temp, pattern)) { // пока он содержит слова, записываем в temp слово
- current = getTime(temp);
- before = current - oneday;
- gmtime_s(&cur, ¤t);
- gmtime_s(&bef, &before);
- strftime(buffer, 8, "%dT.%m.%Y", &cur);
- out << buffer << ' ';
- strftime(buffer, 8, "%dT.%m.%Y", &bef);
- out << buffer << endl;
- }
- in.close();
- out.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement