Is it safe to convert dates back and forth using UTC to ignore DST but still use the current language for the current user?

I have a date (no time, assuming time 00:00:00) that I convert between time_tand a struct tm.

I get the date in plain YYYYMMDD format and convert it to struct tm:

struct tm my_tm;
memset(&my_tm, 0, sizeof(my_tm));
my_tm.tm_year = str.mid(0, 4).toInt() - 1900;
my_tm.tm_mon = str.mid(4, 2).toInt() - 1;
my_tm.tm_mday = str.mid(6, 2).toInt();
  • PS: for those who are wondering, I have QString(Qt), therefore the members mid()and are used toInt().

Then I convert this date to time_twith mktime():

time_t my_time(mktime(&my_tm));

At this point, the date changes a day earlier (more precisely, -1h) if the date is March 6, 2016 ("20160306" becomes 2016/03/05 at struct tm). This is due to DST (tm_isdst is set accordingly).

mktime(), mkgmtime(), , : , DST :

time_t my_time(mkgmtime(&my_tm));

struct tm gmtime_r(). , , :

struct tm other_tm;
gmtime_r(&my_time, &other_tm);

- , . ICU. f_current_timezone UTC format_date(), 6, 2016 ( , 5 , 2016).

QString locale::format_date(time_t d)
{
    QUnicodeString const timezone_id(f_current_timezone);
    LocalPointer<TimeZone> tz(TimeZone::createTimeZone(timezone_id));
    Locale const l(f_current_locale.toUtf8().data());
    LocalPointer<DateFormat> dt(DateFormat::createDateInstance(DateFormat::kDefault, l));
    dt->setTimeZone(*tz);
    UDate const udate(d * 1000LL);
    QUnicodeString u;
    dt->format(udate, u);
    return u;
}

( ) "UTC" , format_date()?

+4
1

mktime() tm_hour 12 (12pm). , , , mktime().

0

All Articles