Steefmaester

[classe] Timeplus (chrono wrap)

Dec 13th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. //by steef
  2.  
  3. //Timeplus.h
  4.  
  5. #ifndef _timeplus_
  6. #define _timeplus_
  7. #define _CRT_SECURE_NO_WARNINGS
  8.  
  9. #include <string>
  10. #include <chrono>
  11.  
  12.  
  13. using namespace std;
  14. using namespace std::chrono;
  15.  
  16. class Timeplus {
  17.  
  18. private:
  19.  
  20.     time_point<system_clock> Date;
  21.  
  22.     string Hours = "%H";
  23.     string Minutes = "%M";
  24.     string Seconds = "%S";
  25.  
  26.     string Day = "%d";
  27.     string Month = "%m";
  28.     string Years = "%y";
  29.  
  30.     string insert(string Time, int start, int end, string str);
  31.  
  32. public:
  33.  
  34.     Timeplus();
  35.     ~Timeplus();
  36.  
  37.     void save_system_time();
  38.  
  39.     string getTime(string Time);
  40.  
  41. };
  42.  
  43. #endif
  44.  
  45. //Timeplus.cpp
  46.  
  47. #include "Timeplus.h"
  48.  
  49. Timeplus::Timeplus(){
  50.  
  51.     save_system_time();
  52.  
  53. }
  54.  
  55. void Timeplus::save_system_time()
  56. {
  57.     Date = system_clock::now();
  58. }
  59.  
  60. string Timeplus::insert(string Time, int start, int end, string str)
  61. {
  62.     string debut = Time.substr(0, start);
  63.     string fin = Time.substr(end, Time.size());
  64.  
  65.     Time = debut + str + fin;
  66.  
  67.     return Time;
  68. }
  69.  
  70. string Timeplus::getTime(string Time)
  71. {
  72.  
  73.     time_t Date_t = system_clock::to_time_t(Date);
  74.     tm local_tm = *localtime(&Date_t);
  75.  
  76.     int val;
  77.     val = Time.find(Hours);
  78.     if (val > 0)
  79.         Time = insert(Time, val, val + 2,to_string(local_tm.tm_hour));
  80.  
  81.     val = Time.find(Minutes);
  82.     if (val > 0)
  83.         Time = insert(Time, val, val + 2, to_string(local_tm.tm_min));
  84.  
  85.     val = Time.find(Seconds);
  86.     if (val > 0)
  87.         Time = insert(Time, val, val + 2, to_string(local_tm.tm_sec));
  88.  
  89.     val = Time.find(Day);
  90.     if (val > 0)
  91.         Time = insert(Time, val, val + 2, to_string(local_tm.tm_mday));
  92.  
  93.     val = Time.find(Month);
  94.     if (val > 0)
  95.         Time = insert(Time, val, val + 2, to_string(local_tm.tm_mon+1));
  96.  
  97.     val = Time.find(Years);
  98.     if (val > 0)
  99.         Time = insert(Time, val, val + 2, to_string(local_tm.tm_year+1900));
  100.  
  101.     return Time;
  102. }
  103.  
  104. Timeplus::~Timeplus()
  105. {
  106.  
  107. }
  108.  
  109. //main.cpp
  110.  
  111. #include <iostream>
  112. #include <string>
  113. #include "Timeplus.h"
  114.  
  115.  
  116.  
  117.  
  118. using namespace std;
  119. using namespace std::chrono;
  120.  
  121.  
  122. void main() {
  123.  
  124.     Timeplus test;
  125.  
  126.     test.save_system_time();//récupère l'heure system (appelé automatiquement lors de la création de la classe)
  127.  
  128.     cout << test.getTime("Il est %H:%M:%S et nous sommes le %d/%m/%y") << endl;//affiche la date au format voulu
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment