- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个 boost::posix_time::ptime
实例并希望使用给定的 boost::local_time::time_zone_ptr
将其转换(“格式化”)为字符串实例。下面是一个显示我目前拥有的测试程序。它转换 ptime
到 local_date_time
据我了解,除了时间信息外,它还表示时区。
在 2011-08-18 12:00:00 UTC 运行这个程序时,我期望输出 2011-08-18 14.00.00 UTC+02:00
.相反,它打印 2011-08-18 12:00:00 UTC+00:00
.即相对于打印的时区,打印的时间是正确的,但它不在我用来创建 boost::local_time::local_date_time
的时区内实例。
我目前使用建议的技术in this question使用自定义格式字符串。
#include <iostream>
#include <ctime>
#include <boost/date_time.hpp>
int main(int argc, char ** argv) {
using namespace std;
// Get current time, as an example
boost::posix_time::ptime dt = boost::posix_time::microsec_clock::universal_time();
// Create a time_zone_ptr for the desired time zone and use it to create a local_date_time
boost::local_time::time_zone_ptr zone(new boost::local_time::posix_time_zone("EST"));
boost::local_time::local_date_time dt_with_zone(dt, zone);
std::stringstream strm;
// Set the formatting facet on the stringstream and print the local_date_time to it.
// Ownership of the boost::local_time::local_time_facet object goes to the created std::locale object.
strm.imbue(std::locale(std::cout.getloc(), new boost::local_time::local_time_facet("%Y-%m-%d %H:%M:%S UTC%Q")));
strm << dt_with_zone;
// Print the stream's content to the console
cout << strm.str() << endl;
return 0;
}
我应该如何转换 local_date_time
实例到字符串,因此字符串中的日期使用 time_zone_ptr
指定的时区表示实例?
最佳答案
我认为 boost 不知道时区说明符。替换
new boost::local_time::posix_time_zone("EST")
在你的代码中
new boost::local_time::posix_time_zone("EST-05:00:00")
一切正常。如果您想使用通用标准名称,则必须按照 boost 文档中的描述创建一个时区数据库。
关于c++ - 使用自定义时区将 boost::posix_time::ptime 转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7108059/
我正在尝试将 boost::posix_time::ptime 转换为特定的字符串格式(扩展 ISO),然后再转换回 boost::posix_time::ptime. 令人惊讶的是,它看起来像对于
我在尝试编译这段代码时遇到了这些奇怪的编译错误。 OutputHandler.h: #pragma once #include #include #include "FileNotAccessib
当我尝试使用以下行编译代码时:(我正在使用 boost 1.51,在 UBuntu 12.04 LTS 64 中使用 gcc 4.6.3位) #include dotFile *, std::v
我想将分钟数舍入到给定的步骤(15分钟)。像这样 "2002-01-20 23:35:59.000" -> "2002-01-20 23:30:00.000" "2002-01-20 23:00:59
我需要纳秒分辨率,我在文档中读到: http://www.boost.org/doc/libs/1_53_0/doc/html/date_time/details.html#date_time.bui
我使用以下方法通过 boost::posix_time 检索包含当前时间的字符串: wstring TimeField::getActualTime() const { // Defined el
最好的方法是什么,而不使用 MS COM 库将 OLE 日期时间格式转换为 boost 使用的 posix_datetime? OLE 日期时间表示为 float 。 最佳答案 您必须手动执行...我
有人能解释一下为什么我们需要从 boost::posix_time::ptime 中减去 boost::gregorian::date(1970,1,1) 以获得 POSIX 时间符号(即自 1970
我有一组函数,允许我根据特定格式将 ptime 转换为字符串并将字符串转换为 ptime。 这很好用,直到我需要使用毫秒修饰符(%f)。 转换为 String 工作正常: std::string st
boost::posix_time::ptime 是固定大小的吗? 我想用它来替换一些自定义的时间类型垃圾,但它需要进入的类是预分配内存池的一部分,并且必须是固定大小。 如果是这样,我在哪里可以找到保
我试图在整个互联网上找到答案。我需要以微秒为单位的秒级时间戳。 boost::posix_time::ptime now = boost::posix_time::microsec_clock::lo
奇怪的是, std::cout << boost::posix_time::from_iso_string("20120927T062299");// 99 seconds! yields 2012-
我有以下代码从 posix_time 获取 UNIX 时间 boost::posix_time::ptime time1(boost::gregorian::date(9999,12,31)); bo
当我在我的 C++ 代码中包含 boost/date_time/posix_time/posix_time.hpp 时出现以下编译错误 /usr/include/boost/date_time/dat
我正在从 PostgreSQL 数据库中读取时间戳字段。时间戳列定义为: my_timestamp TIMESTAMP WITH TIME ZONE DEFAULT NOW() 从数据库中读取时,我将
这不是问题,只是出于好奇。我想知道当使用其他语言的 dateTime 实现的类似操作只有 3 或 4 行时,我必须编写多少代码才能实现此目的。 所以我得到的是作为字符串的时间戳,即:06-Feb-20
我正在尝试从文本文件中解析日期时间。时间戳具有微秒精度,但由于我无法控制的历史原因,它们是使用冒号而不是点来分隔小数秒部分创建的,例如: 2015/05/05 03:10:43:537408 代替 2
我正在尝试对两个起初看起来并不难的问题给出答案。Q1:如何获取 UTC.Now() 和给定日期之间经过的秒数?A1:就像下面的代码一样!Q2:我如何确定自上次“完整”秒以来已经过去了多少小数秒?我想打
我有一个像“2012-03-28T08:00:00”这样的格式化字符串。我想获取年、月(字符串格式)、日期、小时、分钟、秒和日(字符串格式)。谁能建议我在 boost 中使用最简单的方法。 谢谢 最佳
我正在使用 boost::posix_time::ptime 来测量我的模拟运行时间和其他东西。 假设 boost::posix_time::ptime start, stop; boost::pos
我是一名优秀的程序员,十分优秀!