简体   繁体   English

在boost :: posix_time中设置值(年,月,日......)

[英]set values (year, month, day…) in a boost::posix_time

In a class, I have an attribute boost::posix_time::ptime that refers to a date and time like this: 在类中,我有一个属性boost :: posix_time :: ptime,它引用了这样的日期和时间:

boost::posix_time::ptime p_; boost :: posix_time :: ptime p_;

In the constructor, I can pass the values and set them without problems. 在构造函数中,我可以传递值并设置它们没有问题。

my_class::my_class( ... )
  : p_( boost::posix_time::ptime( boost::gregorian::date(y,M,d),
                                  hours(h) + minutes(m) + seconds(s) +
                                  milliseconds(ms) + microseconds(us) +
                                  nanosec(ns));

I would like to create set methods (add and subtract) the values for all fields of this ptime (year, month, day, hours... if possible). 我想创建set方法(加减)这个ptime的所有字段的值(年,月,日,小时......如果可能的话)。

If I use the ptime_.date(), it returns a cons reference of date, and I can't set it directly. 如果我使用ptime_.date(),它返回日期的cons引用,我不能直接设置它。

I'd like to do something like this: 我想做这样的事情:

void my_class::set_year(qint64 y) {
  // p_.date().year = y;
}

Is this possible? 这可能吗?

I was thinking on creating a reset(...) method, and set what I need, but it sounds weird for this purpose (copy all values and repeat them in the code). 我正在考虑创建一个重置(...)方法,并设置我需要的东西,但这听起来很奇怪(复制所有值并在代码中重复它们)。

Rgds. RGDS。

In description of boost ptime we can read: boost ptime的描述中,我们可以阅读:

The class boost::posix_time::ptime is the primary interface for time point manipulation. boost :: posix_time :: ptime类是时间点操作的主要接口。 In general, the ptime class is immutable once constructed although it does allow assignment. 通常,虽然ptime类允许赋值,但它一旦构造就是不可变的。 (...) Functions for converting posix_time objects to, and from, tm structs are provided as well as conversion from time_t and FILETIME. (...)提供了将posix_time对象转换为和tm结构的函数以及从time_t和FILETIME的转换。

So conversion to time struct tm and from it can be used. 因此可以使用转换为时间struct tm This snippet demonstrates this. 这个片段演示了这一点。

#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/date.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
/*
 * 
 */

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

void set_year( uint64_t y, ptime& p) {
    tm pt_tm = to_tm( p);
    pt_tm.tm_year = y - 1900;
    p = ptime_from_tm( pt_tm);
}

int main(int argc, char** argv) {
   ptime t1( date(2002,Feb,10), hours(5)+minutes(4)+seconds(2)); 
   std::cout << to_simple_string( t1) << std::endl;
   set_year( 2001, t1);
   std::cout << to_simple_string( t1);
}

output: 输出:

2002-Feb-10 05:04:02 2002年2月10日05:04:02

2001-Feb-10 05:04:02 2001年2月10日05:04:02

What you describe is what you should do. 你描述的是你应该做的。

Posix Time models a posix timestamp, and it is pretty minimalist with respect to the API: Posix Time对posix时间戳进行建模,对于API来说它非常简约:

Introduction 介绍

The class boost::posix_time::ptime is the primary interface for time point manipulation. boost::posix_time::ptime是时间点操作的主要接口。 In general, the ptime class is immutable once constructed although it does allow assignment . 通常,虽然ptime允许赋值,但它一旦构造就是不可变的

So there you have it: you're supposed to assign a new ptime if you want to change it. 所以你有它:如果你想改变它,你应该分配一个新的ptime。

I can only imagine a single scenario for this, and that's when you want users to input a time value with full fidelity. 我只能想象一个这样的场景,那就是你希望用户输入一个完全保真的时间值。 In that case I'd likely bind the UI elements to separate fields for each time-part and piece together a string representation from them. 在这种情况下,我可能会将UI元素绑定到每个时间部分的单独字段,并将来自它们的字符串表示拼凑在一起。

Then, parse your string into a ptime so you will have 然后,将您的字符串解析为ptime这样您就可以了

  • no clumsy hacking to beat ptime into submission to support operations it wasn't designed for 没有笨拙的黑客攻击ptime提交支持不适合的操作
  • free input validation 免费输入验证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM