Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //by steef
- //Timeplus.h
- #ifndef _timeplus_
- #define _timeplus_
- #define _CRT_SECURE_NO_WARNINGS
- #include <string>
- #include <chrono>
- using namespace std;
- using namespace std::chrono;
- class Timeplus {
- private:
- time_point<system_clock> Date;
- string Hours = "%H";
- string Minutes = "%M";
- string Seconds = "%S";
- string Day = "%d";
- string Month = "%m";
- string Years = "%y";
- string insert(string Time, int start, int end, string str);
- public:
- Timeplus();
- ~Timeplus();
- void save_system_time();
- string getTime(string Time);
- };
- #endif
- //Timeplus.cpp
- #include "Timeplus.h"
- Timeplus::Timeplus(){
- save_system_time();
- }
- void Timeplus::save_system_time()
- {
- Date = system_clock::now();
- }
- string Timeplus::insert(string Time, int start, int end, string str)
- {
- string debut = Time.substr(0, start);
- string fin = Time.substr(end, Time.size());
- Time = debut + str + fin;
- return Time;
- }
- string Timeplus::getTime(string Time)
- {
- time_t Date_t = system_clock::to_time_t(Date);
- tm local_tm = *localtime(&Date_t);
- int val;
- val = Time.find(Hours);
- if (val > 0)
- Time = insert(Time, val, val + 2,to_string(local_tm.tm_hour));
- val = Time.find(Minutes);
- if (val > 0)
- Time = insert(Time, val, val + 2, to_string(local_tm.tm_min));
- val = Time.find(Seconds);
- if (val > 0)
- Time = insert(Time, val, val + 2, to_string(local_tm.tm_sec));
- val = Time.find(Day);
- if (val > 0)
- Time = insert(Time, val, val + 2, to_string(local_tm.tm_mday));
- val = Time.find(Month);
- if (val > 0)
- Time = insert(Time, val, val + 2, to_string(local_tm.tm_mon+1));
- val = Time.find(Years);
- if (val > 0)
- Time = insert(Time, val, val + 2, to_string(local_tm.tm_year+1900));
- return Time;
- }
- Timeplus::~Timeplus()
- {
- }
- //main.cpp
- #include <iostream>
- #include <string>
- #include "Timeplus.h"
- using namespace std;
- using namespace std::chrono;
- void main() {
- Timeplus test;
- test.save_system_time();//récupère l'heure system (appelé automatiquement lors de la création de la classe)
- cout << test.getTime("Il est %H:%M:%S et nous sommes le %d/%m/%y") << endl;//affiche la date au format voulu
- }
Advertisement
Add Comment
Please, Sign In to add comment