我正在從用戶那里獲取輸入日期并將其轉換為 tm 結構(使用本地時間設定 tm 結構的 is_dst、時區和 gmtoff 引數),但是當我使用 mkTime 獲取紀元值時,它會更改 tm 的時區和 gmtOffset 屬性結構并回傳錯誤的偏移值。
tm tmStartDateTime = {};
string dateString = inputDate;
if (myclass::tryParseString(inputDate, "%Y/%m/%d %H:%M:%S", tmStartDateTime))// converting string date to tm
{
struct timespec now = {0};
clock_gettime(CLOCK_REALTIME, &now);
tm *nowStruct = localtime(&now.tv_sec);
// setting isdst, gmtoff and timezone using localtime
tmStartDateTime.tm_isdst = nowStruct->tm_isdst;
tmStartDateTime.tm_gmtoff = nowStruct->tm_gmtoff;
tmStartDateTime.tm_zone = nowStruct->tm_zone;
tm bmStartDate = {};
bmStartDate = tmStartDateTime;
StartDateTimeEpoch = mktime(&bmStartDate);
}
例如,如果用戶給出日期 01/01/1970 00:00:00 并且機器時區設定為歐洲/倫敦,則 tm struct gmtoffset 值更改為 3600 并且時區更改為 BST,而機器時區為 GMT,gmtoffset 為 0夏令時于 2022 年 10 月 30 日結束。mktime 為什么以及如何更改時區和 tm 結構的 gmtoffset 值。(注意:機器的 TZ 變數設定為空字串,我也將其更改為 Europe/London 但沒有成功)
uj5u.com熱心網友回復:
localtime()正在使用您傳遞給它的時間的 DST 設定,而不是呼叫函式時的當前時間。DST 于 1970 年 1 月 1 日在英國生效,因此無論 DST 今天是否生效,它都會回傳夏令時。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/537894.html
下一篇:Android網路請求(2)
