用C转换日期时间到不同的时区++ /升压 [英] converting datetime to different timezone in C++/boost

查看:161
本文介绍了用C转换日期时间到不同的时区++ /升压的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对印度股市的价格数据,但它的时间和日期戳GMT,这样我就可以用数据重新presenting正确的日期和时间工作。

我需要为某些记录的日期和时间转换成我的数据库为从GMT IST,我目前的时区。

  MySQL的>说明price_hist;
+ --------------- + ------------ + ------ + - --- + --------- + ---------------- +
|现场|类型|空|钥匙|默认|额外|
+ --------------- + ------------ + ------ + - --- + --------- + ---------------- +
| trade_id | INT(11)| NO | PRI | NULL | AUTO_INCREMENT |
| CONTRACT_NAME | VARCHAR(14)| NO | MUL | NULL | |
| trade_date |日期| NO | | NULL | |
| trade_time | VARCHAR(6)| NO | | NULL | |
| trade_price |十进制(10,4)| NO | | NULL | |
| trade_volume | BIGINT(20)| NO | | NULL | |
+ --------------- + ------------ + ------ + - --- + --------- + ---------------- +
8行中集(0.02秒)

我试图通过运行更改数据库本身的时区,但没有工作:

 选择CONVERT_TZ(2010-06-30 19:00:00,GMT,IST);
+ ----------------------------------------------- +
| CONVERT_TZ(2011-06-30 09:00:00,GMT,IST)|
+ ----------------------------------------------- +
| NULL |
+ ----------------------------------------------- +
1行中集(0.01秒)

我是新来提振,但我一直在建议使用升压迄今在$ C $来处理这个C​​本身。

我搜索了一些职位日期时间转换,但没有找到一个职位,会回答我的具体问题。

如果有特定的链接或者更好的是,例如升压code有人可以共享,这将是对像我这样的NUBEE一个很好的开始。 : - )

我以前@Karison提供编写如下code参考:

 的#include助推/ DATE_TIME / LOCAL_TIME / local_time.hpp
  #包括LT&;&iostream的GT;  诠释的main()
  {
    使用空间boost ::了posix_time;
    使用空间boost ::格利高;
    使用空间boost :: LOCAL_TIME;    tz_database tz_db;
    time_zon​​e_ptr chi_tz = tz_db.time_zon​​e_from_region(美国/芝加哥);
    time_zon​​e_ptr jst_tz(新的posix_time_zon​​e(EST + 5:00:00));
    LOCAL_DATE_TIME jpn_time(日期(2012年,月,3),小时(16),jst_tz,LOCAL_DATE_TIME :: NOT_DATE_TIME_ON_ERROR);
    LOCAL_DATE_TIME osaka_time = jpn_time.local_time_in(chi_tz);
    性病::法院LT&;<osaka_time:<< osaka_time<<的std :: ENDL;
返回0;
}


解决方案

首先,让我们改变语法和分离的问题,因为我可以看到有3。

首先 CONVERT_TZ 的语法需要一个字符串调用看起来应该像这:

 选择CONVERT_TZ(2011-06-30 09:00:00,GMT,IST)

单蜱不是双引号。

二。如果您的机器位于同一时区,你是在POSIX兼容的系统运行,就可以得到字符串重新presenting日期和做的事:

 结构TM结果;
strptime(小于时间字符串>中&下;格式字符串>中&放大器;结果);
time_t的epoch_time = mktime(安培;结果);

这将给你的标准UNIX时间,你可以再使用做任何你需要的。

最后但并非最不重要的推动作用。这是一个功能强大的库,以确保,但我不认为这个项目,你需要在这个时候增加它的复杂性。如果你仍然觉得自己想出来或者你是不是在POSIX兼容的系统,您可以看看的 http://www.boost.org/doc/libs/1_48_0/doc/html/date_time.html ,你仍然需要创建分组时间从正在从数据库中返回,然后操纵它,你认为合适的字符串。因为你需要再检查一次你针对它的数据库中获取使用 LOCAL_TIME 部分创建时区对象。你可以看看的http://www.boost.org/doc/libs/1_48_0/doc/html/date_time/examples/general_usage_examples.html

特别是最后一个。因此,对于你的榜样将是:

  time_zon​​e_ptr src_zone(新的posix_time_zon​​e(IST + 05:30:00));
time_zon​​e_ptr dst_zone(新的posix_time_zon​​e(CST));
LOCAL_DATE_TIME trd_time(日期(....),时(...),src_zone,
                         LOCAL_DATE_TIME :: NOT_DATE_TIME_ON_ERROR);
LOCAL_DATE_TIME res_time = trd_time.local_time_in(dst_zone);

不能保证,这将是100%正确的,但你的想法。

例如:

 的#include助推/ DATE_TIME / LOCAL_TIME / local_time.hpp
#包括LT&;&iostream的GT;诠释的main()
{
    使用空间boost ::了posix_time;
    使用空间boost ::格利高;
    使用空间boost :: LOCAL_TIME;    tz_database tz_db;
    time_zon​​e_ptr chi_tz(新的posix_time_zon​​e(CST-06:00:00);
    time_zon​​e_ptr jst_tz(新的posix_time_zon​​e(JST + 09:00:00));    LOCAL_DATE_TIME jpn_time(日期(2012年,月,3),小时(16)
                             chi_tz,LOCAL_DATE_TIME :: NOT_DATE_TIME_ON_ERROR);
    LOCAL_DATE_TIME osaka_time = jpn_time.local_time_in(jst_tz);
    性病::法院LT&;<osaka_time:<< osaka_time<<的std :: ENDL;
    返回0;
}

I have price data for the Indian stock market, but the time and date stamp on it is GMT, so I can work with the data representing the correct date and time.

I need to convert the date and time for some of the records in my DB into IST from GMT, my current time zone.

mysql> desc price_hist;
+---------------+-----------------------+------+-----+---------+----------------+
| Field         | Type                  | Null | Key | Default | Extra          |
+---------------+-----------------------+------+-----+---------+----------------+
| trade_id      | int(11)               | NO   | PRI | NULL    | auto_increment |
| contract_name | varchar(14)           | NO   | MUL | NULL    |                |
| trade_date    | date                  | NO   |     | NULL    |                |
| trade_time    | varchar(6)            | NO   |     | NULL    |                |
| trade_price   | decimal(10,4)         | NO   |     | NULL    |                |
| trade_volume  | bigint(20)            | NO   |     | NULL    |                |
+---------------+-----------------------+------+-----+---------+----------------+
8 rows in set (0.02 sec)

I tried to change the timezone in the DB itself by running, but that did not work:

select convert_tz("2010-06-30 19:00:00",'GMT','IST');
+-----------------------------------------------+
| convert_tz("2011-06-30 09:00:00",'GMT','IST') |
+-----------------------------------------------+
| NULL |
+-----------------------------------------------+
1 row in set (0.01 sec) 

I am new to Boost, but I have been suggested to use Boost date to handle this in the code itself.

I searched some of the posts for datetime conversion but did not find a post that would answer my specific question.

If there is a specific link or better still, example boost code someone could share, that would be a great start for a nubee like me. :-)

I used the reference @Karison provided to write the following code:

#include "boost/date_time/local_time/local_time.hpp"
  #include <iostream>

  int  main() 
  {
    using namespace boost::posix_time;
    using namespace boost::gregorian;
    using namespace boost::local_time;

    tz_database tz_db;
    time_zone_ptr chi_tz=tz_db.time_zone_from_region("America/Chicago");
    time_zone_ptr jst_tz(new posix_time_zone("EST+5:00:00"));


    local_date_time jpn_time(date(2012,Jan,3), hours(16), jst_tz,local_date_time::NOT_DATE_TIME_ON_ERROR);
    local_date_time osaka_time = jpn_time.local_time_in(chi_tz);
    std::cout<<"osaka_time: "<<osaka_time<<std::endl;
return 0;
}

解决方案

First off let's change the syntax and separate the questions as I can see there are 3.

First syntax of convert_tz takes a string which the call should look like:

select convert_tz('2011-06-30 09:00:00','GMT','IST')

Single ticks not double quotes.

Second. If your machine is located in the same timezone and you are running on POSIX compliant system you can get the string representing the date and do:

struct tm result;
strptime(<time string>, <format string>, &result);
time_t epoch_time = mktime(&result);

which will give you the standard UNIX time which you can then use to do whatever you need.

Last but not least Boost. It's a powerful library to be sure but I don't think that for the project you will need to add complexity of it at this time. If you still feel like trying it out or you are not on POSIX compliant system you can take a look at http://www.boost.org/doc/libs/1_48_0/doc/html/date_time.html, which you will still need to create ptime from the string you are returning from the database and then manipulate it as you see fit. Using local_time portion create the timezone objects as you need and then check time you get from the database against it. You can look at the examples in http://www.boost.org/doc/libs/1_48_0/doc/html/date_time/examples/general_usage_examples.html

Especially the last one. So for your example it will be:

time_zone_ptr src_zone(new posix_time_zone("IST+05:30:00"));
time_zone_ptr dst_zone(new posix_time_zone("CST"));
local_date_time trd_time(date(....), hours(...), src_zone, 
                         local_date_time::NOT_DATE_TIME_ON_ERROR);
local_date_time res_time = trd_time.local_time_in(dst_zone);

Can't guarantee that it will be 100% correct but you get the idea.

Example:

#include "boost/date_time/local_time/local_time.hpp"
#include <iostream>

int  main() 
{
    using namespace boost::posix_time;
    using namespace boost::gregorian;
    using namespace boost::local_time;

    tz_database tz_db;
    time_zone_ptr chi_tz(new posix_time_zone("CST-06:00:00");
    time_zone_ptr jst_tz(new posix_time_zone("JST+09:00:00"));

    local_date_time jpn_time(date(2012,Jan,3), hours(16),
                             chi_tz,local_date_time::NOT_DATE_TIME_ON_ERROR);
    local_date_time osaka_time = jpn_time.local_time_in(jst_tz);
    std::cout<<"osaka_time: "<<osaka_time<<std::endl;
    return 0;
}

这篇关于用C转换日期时间到不同的时区++ /升压的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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