1
我不得不寫一個函數返回我當前位置的UTC偏移量。以我的最大surprize,下面的代碼返回0:如何找出我在Qt 5.1中當前位置的UTC偏移量?
const QDateTime now = QDateTime::currentDateTime();
return now.toUTC().secsTo(now)/60;
我不得不寫一個函數返回我當前位置的UTC偏移量。以我的最大surprize,下面的代碼返回0:如何找出我在Qt 5.1中當前位置的UTC偏移量?
const QDateTime now = QDateTime::currentDateTime();
return now.toUTC().secsTo(now)/60;
這並不像看起來那麼容易,因爲QDateTime :: secsTo計算轉換爲UTC後偏移。我找到了一個答案here,但我並不是很喜歡將字符串轉換回來。所以我的解決方案是:
const QDateTime dateTime1 = QDateTime::currentDateTime();
const QDateTime dateTime2 = QDateTime(dateTime1.date(), dateTime1.time(), Qt::UTC);
return dateTime1.secsTo(dateTime2)/60;