jtentor

Consulta localtime y asctime

May 16th, 2020
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. //#include <stdio.h>
  5. #include <time.h>
  6.  
  7. std::string myasctime(const struct tm *timeptr);
  8.  
  9. int main() {
  10.     std::cout << "Consulta sobre localtime y asctime" << std::endl;
  11.  
  12.     // from http://www.cplusplus.com/reference/ctime/asctime/
  13.     time_t rawtime;
  14.     struct tm * timeinfo;
  15.  
  16.     time(&rawtime);
  17.     timeinfo = localtime(&rawtime);
  18.     // printf ( "The current date/time is: %s", asctime(timeinfo));
  19.  
  20.     // need a string object to convert char* sequence
  21.     std::string str;
  22.     std::cout << "The current date/time is: " << str.assign(asctime(timeinfo)) << std::endl;
  23.  
  24.     // using special function
  25.     std::cout << "The current date/time is: " << myasctime(timeinfo) << std::endl;
  26.  
  27.     return 0;
  28. }
  29.  
  30. std::string myasctime(const struct tm *timeptr)
  31. {
  32.     static const std::string wday_name[] = {
  33.             "Dom", "Lun", "Mar", "Mie", "Jue", "Vie", "Sab"
  34.     };
  35.     static const std::string mon_name[] = {
  36.             "Ene", "Feb", "Mar", "Abr", "May", "Jun",
  37.             "Jul", "Ago", "Sep", "Oct", "Nov", "Dic"
  38.     };
  39.  
  40.     std::string out;
  41.     out = wday_name[timeptr->tm_wday] + " " \
  42.         + mon_name[timeptr->tm_mon] + " " \
  43.         + std::to_string(timeptr->tm_mday) + " " \
  44.         + std::to_string(timeptr->tm_hour) + ":" \
  45.         + std::to_string(timeptr->tm_min) + ":" \
  46.         + std::to_string(timeptr->tm_sec) + ":" \
  47.         + std::to_string(1900 + timeptr->tm_year);
  48.  
  49.     return out;
  50. }
Add Comment
Please, Sign In to add comment