2011-08-16 36 views

回答

2

我想了一些更多的調試時間相關的結構後。

它更好地使用struct tm * gmtime (const time_t * timer)它將給UTC時間。

並且可以使用tzset()tzname[0]來獲取時區信息。

1

Boost有Boost.Date_Time,包含時區數據庫。你可以使用相同的。

2

您可以使用在Unix上執行以下操作:

std::string LocalTimeZone() 
{ 
    time_t now = time(NULL); 
    struct tm tnow = *localtime(&now); 
    std::string tz = tnow.tm_zone; 
    std::cout << "Local timezone: " << tz << std::endl; 

    char buff[100]; 
    strftime(buff, sizeof buff, "%a %b %d %Y %T %Z%z", &tnow); 

    std::vector<std::string> vec; 
    const std::string s(buff); 
    boost::split(vec, s, boost::is_any_of(" ")); 

    std::vector<std::string>::iterator i = vec.end(); 
    return *--i; 
} 

這將給當前時區爲每個主機的系統設置。