Advertisement
jtentor

Consulta localtime_s y asctime_s

May 19th, 2020
1,226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. // Consultalocaltime.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include <iostream>
  5. #include <string>
  6.  
  7. #include <stdio.h>
  8. #include <time.h>
  9.  
  10. std::string myasctime(const struct tm *timeptr);
  11.  
  12. int main()
  13. {
  14.     std::cout << "Consulta sobre localtime y asctime" << std::endl;
  15.  
  16.     time_t rawtime;
  17.     time(&rawtime);
  18.  
  19.     struct tm timeinfo;
  20.  
  21.     // from https://en.cppreference.com/w/c/chrono/localtime
  22.     // from https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/localtime-s-localtime32-s-localtime64-s?view=vs-2019
  23.     localtime_s(&timeinfo, &rawtime);
  24.  
  25.     // from https://en.cppreference.com/w/c/chrono/asctime
  26.     // from https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/asctime-s-wasctime-s?view=vs-2019
  27.     char str[26];
  28.     asctime_s(str, sizeof str, &timeinfo);
  29.  
  30.     printf ( "The current date/time is: %s", str);
  31.  
  32.     // using special function
  33.     std::cout << "The current date/time is: " << myasctime(&timeinfo) << std::endl;
  34.  
  35.     return 0;
  36. }
  37.  
  38. std::string myasctime(const struct tm *timeptr)
  39. {
  40.     static const std::string wday_name[] = {
  41.             "Dom", "Lun", "Mar", "Mie", "Jue", "Vie", "Sab"
  42.     };
  43.     static const std::string mon_name[] = {
  44.             "Ene", "Feb", "Mar", "Abr", "May", "Jun",
  45.             "Jul", "Ago", "Sep", "Oct", "Nov", "Dic"
  46.     };
  47.  
  48.     std::string out;
  49.     out = wday_name[timeptr->tm_wday] + " " \
  50.         + mon_name[timeptr->tm_mon] + " " \
  51.         + std::to_string(timeptr->tm_mday) + " " \
  52.         + std::to_string(timeptr->tm_hour) + ":" \
  53.         + std::to_string(timeptr->tm_min) + ":" \
  54.         + std::to_string(timeptr->tm_sec) + ":" \
  55.         + std::to_string(1900 + timeptr->tm_year);
  56.  
  57.     return out;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement