jtentor icon

Consulta localtime_s y asctime_s

jtentor | PRO | 05/19/20 09:06:05 PM UTC | 0 ⭐ | 1491 👁️ | Never ⏰ | []
C++ |

1.63 KB

|

None

|

0 👍

/

0 👎

// Consultalocaltime.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
 
#include <iostream>
#include <string>
 
#include <stdio.h>
#include <time.h>
 
std::string myasctime(const struct tm *timeptr);
 
int main()
{
    std::cout << "Consulta sobre localtime y asctime" << std::endl;
 
    time_t rawtime;
    time(&rawtime);
 
    struct tm timeinfo;
 
    // from https://en.cppreference.com/w/c/chrono/localtime
    // from https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/localtime-s-localtime32-s-localtime64-s?view=vs-2019
    localtime_s(&timeinfo, &rawtime);
 
    // from https://en.cppreference.com/w/c/chrono/asctime
    // from https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/asctime-s-wasctime-s?view=vs-2019
    char str[26];
    asctime_s(str, sizeof str, &timeinfo);
 
    printf ( "The current date/time is: %s", str);
 
    // using special function
    std::cout << "The current date/time is: " << myasctime(&timeinfo) << std::endl;
 
    return 0;
}
 
std::string myasctime(const struct tm *timeptr)
{
    static const std::string wday_name[] = {
            "Dom", "Lun", "Mar", "Mie", "Jue", "Vie", "Sab"
    };
    static const std::string mon_name[] = {
            "Ene", "Feb", "Mar", "Abr", "May", "Jun",
            "Jul", "Ago", "Sep", "Oct", "Nov", "Dic"
    };
 
    std::string out;
    out = wday_name[timeptr->tm_wday] + " " \
        + mon_name[timeptr->tm_mon] + " " \
        + std::to_string(timeptr->tm_mday) + " " \
        + std::to_string(timeptr->tm_hour) + ":" \
        + std::to_string(timeptr->tm_min) + ":" \
        + std::to_string(timeptr->tm_sec) + ":" \
        + std::to_string(1900 + timeptr->tm_year);
 
    return out;
}

Comments