- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在一个类中,我有一个属性 boost::posix_time::ptime,它指的是这样的日期和时间:
boost::posix_time::ptime p_;
在构造函数中,我可以毫无问题地传递值和设置它们。
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));
我想为这个 ptime 的所有字段(年、月、日、小时......如果可能的话)创 build 置方法(加减)值。
如果我使用 ptime_.date(),它返回一个日期的 cons 引用,我不能直接设置它。
我想做这样的事情:
void my_class::set_year(qint64 y) {
// p_.date().year = y;
}
这可能吗?
我正在考虑创建一个 reset(...) 方法,并设置我需要的东西,但这样做听起来很奇怪(复制所有值并在代码中重复它们)。
研发。
最佳答案
在boost ptime的描述中我们可以阅读:
The class boost::posix_time::ptime is the primary interface for time point manipulation. In general, the ptime class is immutable once constructed although it does allow assignment. (...) Functions for converting posix_time objects to, and from, tm structs are provided as well as conversion from time_t and FILETIME.
因此可以使用到时间 struct tm
的转换。此代码段演示了这一点。
#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);
}
输出:
2002 年 2 月 10 日 05:04:02
2001 年 2 月 10 日 05:04:02
关于c++ - 在 boost::posix_time 中设置值(年、月、日...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23093430/
我正在使用 C#。 我知道我可以用 ToLongDateString() 显示如下内容: Friday, February 27, 2009 我喜欢做的是展示类似的东西: Febru
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
我需要检查是否 DateTime.Now是在每个月的前 3 个工作日(周一至周五)。我还需要提供一个 List有国定假日,应相应处理。 如果 DateTime.Now 是星期六并且是该月的 1 号,则
我想以 2012 年 1 月 1 日 - 2012 年 1 月 31 日的格式显示日期 并获取由 dateranges['jan 1,2012 - jan 31,2012','December 1,2
我有这两个函数可以以正确的格式 (mm-dd-yyyy) 创建一个新字符串,但现在它似乎工作得不太好......当我输入日期 31-03-2013 这是一个有效的日期,它出现在 04-01-2013
我只想在 Fullcalendar 中加载 December 月份,其中 2 个日期启用 2012 年 12 月 14 日、15 日。 var date = new Date(2012, 11); v
我创建了一个convertDate类,用于转换我的时间戳,即Date().sinceReferenceDate(即自2001年1月1日凌晨12:00起)。我有一个问题:当时间戳的日期超过 3 周前时,
我有来自两个来源的数据,我试图在日期/时间上进行匹配。问题是我没有得到匹配。所以我将日期/时间转换为 Julian 并遇到了一些奇怪的事情。 8/11/2015 6:50:22.000 42227
我正在尝试将毫秒转换为如下所示的日期:Oct 04, 2013 .我将毫秒转换为日期对象: var d1 = new Date(milliseconds); 然后输出如下内容: Fri Oct 04
我正在尝试使用 javascript 进行倒计时。但是,我的倒计时只能计算天数、小时数、分钟数和秒数。我还想显示年份和月份。 以下是我的代码: today = new Date();
我有下面的查询不适合我, 这个函数 from_tz(to_timestamp(START_TIME, 'YYYYMMDDHH24MISS'), substr(UTC_TIME_CODE_OFFSET,
例如,我有一列数据如下: 17.14.11 17.15.10 18.21.06 现在是 2017 年 11 月 14 日,我想将其更改为 DateTime 对象,例如: 2017-11-14 2017
$date1 = "2000-01-01"; $date2 = "2011-03-14"; $diff = abs(strtotime($date2) - strtotime($date1)); $y
我有以下代码来验证给定日期格式的日期: val df = new SimpleDateFormat("MM/dd/yyyy"); df.setLenient(false); try { val
我有一个包含两个 DateTime 成员的类。 但是,当我尝试将表示日期的“人类可读”值分配给 DateTime 成员时,它拒绝它并显示消息“字符串未被识别为有效的 DateTime”。 失败的代码行
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
这个问题已经有答案了: Adding days to a date in Java [duplicate] (6 个回答) 已关闭 6 年前。 如何在 java 中将给定的日期转换为日历格式。 示例初
这个问题已经有答案了: Illegal pattern character 'T' when parsing a date string to java.util.Date (4 个回答) 已关闭 6
我现在在香港。如何获取美国的年、月、日? 我已经尝试过: Calendar calendar = Calendar.getInstance(); SimpleDateFormat sdf = new
我正在尝试使用 php 和 Mysql 开发一个搜索系统,但是您如何搜索:例如当数据库中的日期格式为 2012-03-26(年/月/日)时的三月份 最佳答案 根据列数据的格式,这样的事情可能会起作用:
我是一名优秀的程序员,十分优秀!