如何获取特定日期某个位置的时区(偏移量)? [英] How do you get the timezone (offset) for a location on a particular date?

查看:189
本文介绍了如何获取特定日期某个位置的时区(偏移量)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以UTC储存邮件。因此,如果有人回顾以前的消息,我需要能够将时间戳转换为时间相对于时区的时间。

I am storing messages in UTC. Therefore, if someone looks back to a previous message, I need to be able to convert the timestamp to what the time was relative to what the timezone was then. How do I get what the timezone was then?

例如,在2012年9月3日,时区为PDT。当前时间偏移量为-0700。我在9:06发送消息。 UTC是16:06。

For example, on September 3, 2012 the timezone is PDT. The current time offset is -0700. I send a message at 9:06. The time UTC is 16:06.

我回到这个讯息2012年12月1日。目前的时区是PST。当前时间偏移量为-0800。我看看我在2012年9月3日发送的消息。如果我使用当前时间偏移从UTC转换回来,我得到8:06这是不是当消息发送。它是在9:06发送的。

I come back to this message December 1, 2012. The current timezone is PST. The current time offset is -0800. I look at the message I sent on September 3, 2012. If I were to convert back from UTC using the current time offset I get 8:06 which is NOT when the message was sent. It was sent at 9:06.

因此,我需要一种方法来找出2012年9月3日的时区是PDT,而不是PST。

Therefore, I need a way to find out that on September 3, 2012 the timezone was PDT, not PST.

PS没有库是最好的,谢谢。

P.S. without libraries would be the best, thanks.

推荐答案

Boost Date_time就是这样,这里是简单的例子):

Boost Date_time does that, here is simply example I had hanging around (code below):

edd@max:~/src/progs/C++$ g++ -o boost_posix_time_dst boost_posix_time_dst.cpp
edd@max:~/src/progs/C++$ ./boost_posix_time_dst 
DST ran from 2012-Mar-11 02:00:00 to 2012-Nov-04 02:00:00
DST shortcut PDT
DST name     Pacific Daylight Time
edd@max:~/src/progs/C++$ 

还有一个功能来形成一个日期(你的2012年12月1日),看看它是否在一个给定的时间间隔内(由DST开始和结束形成)。

There is also functionality to form a date (your Dec 1, 2012) and see if it is inside a give interval (as formed here by the DST start and end).

我想你也可以通过形成一个日期并检查isDST()布尔值。

I think you can also get it by forming a date and checking the isDST() boolean.

短程序如下。您需要一个csv文件的本地副本,这是a)在Boost源和b)在许多处理时区的网站(例如谷歌的第一次或第二次击中发现它在CERN):

My short program is below. You need a local copy of the csv file which is a) in the Boost sources and b) on a number of sites dealing with timezones (eg Google's first or second hit finds it at CERN):

#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>  
#include <boost/date_time/local_time/local_time.hpp>     

using namespace boost::posix_time;
using namespace boost::gregorian;

int main(int argc, char **argv) {

  boost::local_time::tz_database tz;
  tz.load_from_file("/tmp/date_time_zonespec.csv");

  boost::local_time::time_zone_ptr tzp =
            tz.time_zone_from_region("America/Los_Angeles");    

  int year = 2012;
  boost::posix_time::ptime t1 = tzp->dst_local_start_time(year);
  boost::posix_time::ptime t2 = tzp->dst_local_end_time(year);
  std::cout << "DST ran from " << t1 << " to " << t2 << std::endl;
  std::cout << "DST shortcut " << tzp->dst_zone_abbrev() << std::endl;
  std::cout << "DST name     " << tzp->dst_zone_name() << std::endl;

}

这篇关于如何获取特定日期某个位置的时区(偏移量)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆