Пример #1
0
bool
string_to_timespec_secs(const gchar *str, Timespec *ts)
{

    struct tm parsed_time;
    const gchar *strpos;
    time64 parsed_secs;
    long int gmtoff;

    if (!str || !ts) return FALSE;

    memset(&parsed_time, 0, sizeof(struct tm));

    /* If you change this, make sure you also change the output code, if
       necessary. */
    /*fprintf(stderr, "parsing (%s)\n", str);*/
    strpos = strptime(str, TIMESPEC_PARSE_TIME_FORMAT, &parsed_time);

    g_return_val_if_fail(strpos, FALSE);

    {
        char sign;
        int h1;
        int h2;
        int m1;
        int m2;
        int num_read;

        /* must use "<" here because %n's effects aren't well defined */
        if (sscanf(strpos, " %c%1d%1d%1d%1d%n",
                   &sign,
                   &h1,
                   &h2,
                   &m1,
                   &m2,
                   &num_read) < 5)
        {
            return(FALSE);
        }

        if ((sign != '+') && (sign != '-')) return(FALSE);
        if (!isspace_str(strpos + num_read, -1)) return(FALSE);

        gmtoff = (h1 * 10 + h2) * 60 * 60;
        gmtoff += (m1 * 10 + m2) * 60;
        if (sign == '-') gmtoff = - gmtoff;

        parsed_time.tm_isdst = -1;
    }

    parsed_secs = gnc_timegm(&parsed_time);

    parsed_secs -= gmtoff;

    ts->tv_sec = parsed_secs;

    return(TRUE);
}
Пример #2
0
Timespec
gnc_dmy2timespec_neutral (int day, int month, int year)
{
    struct tm date;
    memset (&date, 0, sizeof(struct tm));
    date.tm_year = year - 1900;
    date.tm_mon = month - 1;
    date.tm_mday = day;
    date.tm_hour = 10;
    date.tm_min = 59;
    date.tm_sec = 0;

    GncDateTime gncdt(date);
    auto offset = gncdt.offset() / 3600;
    if (offset < -11)
        date.tm_hour = -offset;
    if (offset > 13)
        date.tm_hour = 23 - offset;

    return {gnc_timegm(&date), 0};
}