[英]boost: get the current local_date_time with current time zone from the machine
問題是:
代碼:
boost::local_time::local_date_time currentTime(
boost::posix_time::second_clock::local_time(),
boost::local_time::time_zone_ptr());
std::cout << currentTime.local_time() << std::endl;
代碼:
tzset();
// the var tzname will have time zone names
// the var timezone will have the current offset
// the var daylight should show me if there is daylight "on"
但是我仍然無法使用當前的time_zone獲取local_date_time ...有人知道,怎么做?
好的,至於現在我還是不知道答案
但是有代碼可以幫助打印當前時區偏移量
(基於此處相關問題的答案(stackoverflow)和一些內部增強代碼)
我絕對不確定它能在所有機器上正常工作,但現在它總比沒有好:
boost::posix_time::time_duration getUtcOffset(const boost::posix_time::ptime& utcTime)
{
using boost::posix_time::ptime;
const ptime localTime = boost::date_time::c_local_adjustor<ptime>::utc_to_local(utcTime);
return localTime - utcTime;
}
std::wstring getUtcOffsetString(const boost::posix_time::ptime& utcTime)
{
const boost::posix_time::time_duration td = getUtcOffset(utcTime);
const wchar_t fillChar = L'0';
const wchar_t timeSeparator = L':';
std::wostringstream out;
out << (td.is_negative() ? L'-' : L'+');
out << std::setw(2) << std::setfill(fillChar)
<< boost::date_time::absolute_value(td.hours());
out << L':';
out << std::setw(2) << std::setfill(fillChar)
<< boost::date_time::absolute_value(td.minutes());
return out.str();
}
int main()
{
const boost::posix_time::ptime utcNow =
boost::posix_time::second_clock::universal_time();
const std::wstring curTimeOffset = getUtcOffsetString(utcNow);
std::wcout << curTimeOffset.c_str() << std::endl; // prints -05:00 on my comp
}
既然你說你有時區信息,唯一的問題是如何使用boost來格式化你需要的字符串。 以下是示例代碼:
using namespace boost::local_time;
using namespace boost::posix_time;
// Composing this string is the most tricky part. Syntax see in:
// boost\date_time\local_time\posix_time_zone.hpp
string posix_tz_def("PST-5PDT01:00:00,M4.1.0/02:00:00,M10.1.0/02:00:00");
local_date_time ldt(second_clock::local_time(),
time_zone_ptr(new posix_time_zone(posix_tz_def)));
std::stringstream ss;
local_time_facet* output_facet = new local_time_facet();
ss.imbue(std::locale(std::locale::classic(), output_facet));
output_facet->format("%Y-%m-%dT%H:%M:%S %Q");
ss << ldt;
string formatted_datetime = ss.str(); // 2012-01-05T18:14:06 -05:00
在這種方法中,最有問題的部分是posix時區字符串。 我認為每個時區都應該有包含這些字符串的數據庫,而boost提供了一個用於處理.csv文件的模板。 如果你需要的唯一東西是在字符串中偏移,只需將DTS設置為0:00:00,而不關心其余部分。 例如,使用此字符串: PST-5:30PDT0,0,365
(永遠PDT,移位0)。 用你需要的偏移代替“-5:00”。
但是,C ++ / boost方式將通過派生自date_time::time_zone_base
來實現自己的時區提供程序。
可在此處找到更多樣本和想法。
因此,如果您還需要獲得正確的UTC偏移量,那么應該對我的上一個示例進行修改以產生正確的時區字符串:
static boost::posix_time::time_duration utc_offset(
second_clock::local_time() - second_clock::universal_time());
std::ostringstream ss_posix_tz_def;
// We don't care about real zone name so, just put any three letters.
ss_posix_tz_def << "LOC" << utc_offset;
string posix_tz_def = ss_posix_tz_def.str();
另一個解決方案是編寫一個全新的時區提供程序。 像這個簡單的一個:
// We assume local TZ doesn't have DST, UTC offset is calculated basing on
// local system clocks. Thus, using of this provider for time calculations for
// an arbitrary date is not a good idea.
class machine_time_zone : public boost::local_time::custom_time_zone {
public:
typedef boost::local_time::custom_time_zone base_type;
typedef base_type::time_duration_type time_duration_type;
machine_time_zone()
: boost::local_time::custom_time_zone(
time_zone_names("Local machine TZ", "LOC", "", ""),
GetUTCOffset(),
boost::local_time::dst_adjustment_offsets(
time_duration_type(0, 0, 0),
time_duration_type(0, 0, 0), time_duration_type(0, 0, 0)),
boost::shared_ptr<boost::local_time::dst_calc_rule>()) {
}
// This method is not precise, real offset may be several seconds more or less.
static const boost::posix_time::time_duration& GetUTCOffset() {
using boost::posix_time::second_clock;
static boost::posix_time::time_duration utc_offset(
second_clock::local_time() - second_clock::universal_time());
return utc_offset;
}
};
只需在構造local_date_time
時傳遞它:
local_date_time ldt(second_clock::local_time(),
time_zone_ptr(new machine_time_zone()));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.