有趣的事实是,我确信我们大多数人都能在时间领域发挥作用 - 有些日期/时间可能看似有效,但实际上并不存在,例如夏令时切换时间凌晨2:30.
在C++(标准或Windows)中是否有办法确定给定的时区规范中给定的日期/时间是否有效?
我有一个小数纪元时间戳,表示为double,我想转换为适当的std::chrono::time_point.这个时代是自1970年1月1日以来的常见UNIX时代.我知道存在std::chrono::system_clock::from_time_t,但是time_t没有一个小部分.使用C++ 11的最佳方法是什么?
这个问题与boost :: posix_time :: ptime的unix时间戳有关,除了它要求的是C++ 11而不是Boost版本.
我试图将转换std::string为boost::gregorian::date这样的:
using namespace boost::gregorian;
std::string str = "1 Mar 2012";
std::stringstream ss(str);
date_input_facet *df = new date_input_facet("%e %b %Y");
ss.imbue(std::locale(ss.getloc(), df));
date d;
ss >> d; //conversion fails to not-a-date-time
std::cout << "'" << d << "'" << std::endl; //'not-a-date-time'
Run Code Online (Sandbox Code Playgroud)
但如果字符串包含"2012年3月1日",则转换成功.
如何将"2012年3月1日"等字符串转换为等效字符串boost::gregorian::date?
const boost::posix_time::ptime now= boost::posix_time::second_clock::local_time();
year_ = now.date().year();
month_ = now.date().month();
day_ = now.date().day();
Run Code Online (Sandbox Code Playgroud)
这就是我如何获得boost :: posix_time :: ptime的年,月和日,但我无法弄清楚如何获得小时,分钟和秒.你能帮帮我吗?
我有以下代码:
#include <iostream>
#include <string>
#include <iomanip>
#include <locale>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/gregorian/parsers.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
using namespace boost::posix_time;
using namespace boost::gregorian;
int main(int argc, char *argv[])
{
std::string ds("2011-01-02");
date dt(from_string(ds));
date_facet *f=new date_facet("%Y-%m-%d");
std::locale loc=std::locale(std::locale::classic(),f);
std::cout.imbue(loc);
std::cout<<dt<<std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我编译它时,我收到以下错误:
/tmp/ccBWTFcx.o: In function `unsigned short boost::date_time::month_str_to_ushort<boost::gregorian::greg_month>(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
b.cpp:(.text._ZN5boost9date_time19month_str_to_ushortINS_9gregorian10greg_monthEEEtRKSs[unsigned short boost::date_time::month_str_to_ushort<boost::gregorian::greg_month>(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)]+0x97): undefined reference to `boost::gregorian::greg_month::get_month_map_ptr()'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
这是图书馆链接问题吗?
我有一个字符串"2011-10-20T09:30:10-05:00"
有人知道如何使用boost :: date_time库解析它吗?
下面的代码给出了g ++ - 4.7.0的编译错误,但是用g ++ - 4.6编译得很好.
#include <iostream>
#include <boost/date_time/local_time/local_time.hpp>
using namespace std;
int main(){
boost::posix_time::ptime time_t_epoch(boost::gregorian::date(1970,1,1));
cout << time_t_epoch << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
以下是重复出现的错误消息(编译器输出大量消息)
/usr/include/boost/date_time/local_time/local_date_time.hpp:433:84: error: use of deleted function boost::shared_ptr<boost::date_time::time_zone_base<boost::posix_time::ptime, char> >::shared_ptr(const boost::shared_ptr<boost::date_time::time_zone_base<boost::posix_time::ptime, char> >&)
Run Code Online (Sandbox Code Playgroud)
我使用的是Ubuntu 12.04和libboost-date-time1.46.1.
有什么建议?
我正在将现有程序转换为C++,这里需要操作Sybase时间戳.这些时间戳包含日期和时间信息,据我所知,最好由boost::posix_time::ptime变量处理.在代码中的一些地方,我需要从变量中获取年份.
我的问题是:如何从boost ptime变量中最有效地提取年份?下面是其中所花费的示例程序3行代码,用一个额外的开销ostringstream变量和boost::gregorian::date变量.
根据boost文档:
类ptime取决于
gregorian::date时间点的日期部分的接口
但gregorian::date似乎不是一个基类ptime.不知怎的,我在这里遗漏了一些东西.
难道没有一种更简单的方法从中提取年份ptime?
样品:
#include <boost/date_time/local_time/local_time.hpp>
#include <iostream>
int main()
{
boost::posix_time::ptime t(boost::posix_time::second_clock::local_time());
boost::gregorian::date d = t.date();
std::ostringstream os; os << d.year();
std::cout << os.str() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个32位的Linux系统,我必须在其中记录带有时间戳的数据,该时间戳是从1901-01-01 00:00:00的纪元开始的UINT32秒偏移.
计算时间戳对我来说没关系,因为我可以使用64位ticks()计数器和ticks_per_second()函数来生成自纪元以来的秒数,如下所示(我只需要二级分辨率)
const ptime ptime_origin(time_from_string("1901-01-01 00:00:00"));
time_duration my_utc = microsec_clock::universal_time() - ptime_origin;
boost::int64_t tick_per_sec = my_utc.ticks_per_second();
boost::int64_t tick_count = my_utc.ticks();
boost::int64_t sec_since_epoch = tick_count/tick_per_sec;
Run Code Online (Sandbox Code Playgroud)
这对我有用,因为我知道作为无符号整数,秒数不会超过最大UINT32值(不管多少年都没有).
我的问题是我的应用程序可以接收包含UINT32值的modbus消息,我必须ioctl使用调用来设置硬件和系统时钟RTC_SET_TIME.此UINT32再次是自我的纪元1901-01-01 00:00:00以来的秒数偏移量.
我现在的问题是我无法使用64位整数创建一个ptime对象 - 对象的ticks一部分time_duration是私有的,我被限制使用long哪个在我的32位系统上只是一个4字节的有符号整数,不足以存储从我的纪元偏离的秒数.
我无法控制时代的价值,所以我真的很难过如何boost::posix_time::ptime从我拥有的数据中创建我所需的对象.我可以通过计算特定时间间隔的硬第二次计数并使用额外的纪元来建立一个允许这种情况的桥来获得一个肮脏的解决方案,但我想知道boost代码中是否有某些内容可以让我完全使用提升日期时间库.我已经阅读了所有可以找到的文档,但是我看不到任何明显的方法.
编辑:我发现这个相关的问题将int64_t转换为time_duration但是接受的答案对我的纪元不起作用
我想计算两个boost :: gregorian日期之间的时间.目前我只是将这两个日期区分开来,给我一个date_duration来处理:
boost::gregorian::date rhs(2000, 1, 1);
boost::gregorian::date lhs(2005, 6, 15);
boost::gregorian::date_duration dd = lhs - rhs;
boost::gregorian::days daysOld(dd);
boost::gregorian::months monthsOld(...) // This is where I'm stumped
boost::gregorian::years yearsOld(...)
Run Code Online (Sandbox Code Playgroud)
boost :: gregorian :: days ctor可以使用date_duration,但我无法找到一种方法来创建date_duration中几个月和几年的类似表示.从我所看到和看到的情况来看,似乎date_duration只包含两个日期之间的天数.我所希望的是它会给我非正常化的成员多年,几个月和几天.例如,与上面的日期,我希望date_duration有这给了一些领域,如years = 5,months = 65和days = 1992其中每个字段表示使用该特定时间粒度的时间总量.就目前而言,似乎只有一种方法可以在日期之间(通过调用dd.days())访问date_duration .我错过了什么吗?有没有办法使用boost :: gregorian或其他日期/时间库之一来完成这个?
谢谢