Преобразуйте строку из __ ДАТА __ в time_t

Легкий режим в C#, только с помощью Библиотек базовых классов

Редактирование: просто видел, что кто-то действительно Выстраивал. Инвертируйте также

public bool IsPalindrome(string s)
            {
                if (String.IsNullOrEmpty(s))
                {
                    return false;
                }

                else
                {
                    char[] t = s.ToCharArray();
                    Array.Reverse(t);
                    string u = new string(t);
                    if (s.ToLower() == u.ToLower())
                    {
                        return true;
                    }
                }

                return false;
            }
11
задан Brian Tompsett - 汤莱恩 9 June 2016 в 18:03
поделиться

3 ответа

Изменить: исправленная функция должна выглядеть примерно так:

time_t cvt_TIME(char const *time) { 
    char s_month[5];
    int month, day, year;
    struct tm t = {0};
    static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";

    sscanf(time, "%s %d %d", s_month, &day, &year);

    month = (strstr(month_names, s_month)-month_names)/3;

    t.tm_mon = month;
    t.tm_mday = day;
    t.tm_year = year - 1900;
    t.tm_isdst = -1;

    return mktime(&t);
}
17
ответ дан 3 December 2019 в 02:30
поделиться

Функция Джерри выглядит великолепно. Вот моя попытка ... Я также добавил __TIME__.

#include <iostream>
#include <sstream>

using namespace std;

time_t time_when_compiled()
{
    string datestr = __DATE__;
    string timestr = __TIME__;

    istringstream iss_date( datestr );
    string str_month;
    int day;
    int year;
    iss_date >> str_month >> day >> year;

    int month;
    if     ( str_month == "Jan" ) month = 1;
    else if( str_month == "Feb" ) month = 2;
    else if( str_month == "Mar" ) month = 3;
    else if( str_month == "Apr" ) month = 4;
    else if( str_month == "May" ) month = 5;
    else if( str_month == "Jun" ) month = 6;
    else if( str_month == "Jul" ) month = 7;
    else if( str_month == "Aug" ) month = 8;
    else if( str_month == "Sep" ) month = 9;
    else if( str_month == "Oct" ) month = 10;
    else if( str_month == "Nov" ) month = 11;
    else if( str_month == "Dec" ) month = 12;
    else exit(-1);

    for( string::size_type pos = timestr.find( ':' ); pos != string::npos; pos = timestr.find( ':', pos ) )
        timestr[ pos ] = ' ';
    istringstream iss_time( timestr );
    int hour, min, sec;
    iss_time >> hour >> min >> sec;

    tm t = {0};
    t.tm_mon = month-1;
    t.tm_mday = day;
    t.tm_year = year - 1900;
    t.tm_hour = hour - 1;
    t.tm_min = min;
    t.tm_sec = sec;
    return mktime(&t);
}

int main( int, char** )
{
    cout << "Time_t when compiled: " << time_when_compiled() << endl;
    cout << "Time_t now: " << time(0) << endl;

    return 0;
}
7
ответ дан 3 December 2019 в 02:30
поделиться

Ответ здесь .

Спецификация формата ДАТА - здесь .

-1
ответ дан 3 December 2019 в 02:30
поделиться
Другие вопросы по тегам:

Похожие вопросы: