jtentor icon

Consulta localtime y asctime

jtentor | PRO | 05/16/20 01:59:17 PM UTC | 0 ⭐ | 403 👁️ | Never ⏰ | []
C++ |

1.46 KB

|

None

|

0 👍

/

0 👎

#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;
 
    // from http://www.cplusplus.com/reference/ctime/asctime/
    time_t rawtime;
    struct tm * timeinfo;
 
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    // printf ( "The current date/time is: %s", asctime(timeinfo));
 
    // need a string object to convert char* sequence
    std::string str;
    std::cout << "The current date/time is: " << str.assign(asctime(timeinfo)) << std::endl;
 
    // 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