- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我编写了一个表示日期的 C++ 类,我使用 strptime/strftime 从字符串编写和实例化日期。
当我在我的 Linux 上使用“示例输出”选项卡中的 bash 多次运行它时,有时我会创建相同的日期并解析回来,有时我会得到一个小时偏移的日期(我的时区是 UTC +1).
所以,这里发生了什么,我不知道!
#ifndef DOLIPRANE_TIMEUNIT_HPP
#define DOLIPRANE_TIMEUNIT_HPP
enum TimeUnit {
DAY,
HOUR,
MINUTE,
SECOND
};
#endif
#ifndef DOLIPRANE_DATE_HPP
#define DOLIPRANE_DATE_HPP
#include <ctime>
#include <string>
class Date
{
public:
Date();
Date(time_t epoch);
/**
* Expected format: dd/MM/YYYY HH:mm:[ss]
*/
Date(const std::string &date);
~Date();
void
add(long val, TimeUnit u = SECOND);
bool
operator==(const Date &other) const;
bool
operator!=(const Date &other) const;
bool
operator<(const Date &other) const;
bool
operator<=(const Date &other) const;
bool
operator>(const Date &other) const;
bool
operator>=(const Date &other) const;
friend std::ostream&
operator<<(std::ostream &, const Date&);
friend std::istream&
operator>>(std::istream &, Date&);
private:
static const std::string FORMAT;
time_t m_time;
};
#endif
#include <iostream>
#include <stdexcept>
#include <ctime>
const char SEPARATOR=';';
const std::string Date::FORMAT="%d/%m/%Y %H:%M:%S";
Date::Date()
{
m_time = time(NULL);
}
Date::Date(time_t epoch)
: m_time(epoch)
{}
Date::Date(const std::string &date)
{
struct tm t;
const char* ptr = strptime(date.c_str(), FORMAT.c_str(), &t);
if (!ptr) {
std::string cause = "Cannot parse date ";
cause += date;
throw std::invalid_argument(cause);
}
m_time = mktime(&t);
if (m_time == -1) {
std::string cause = "Cannot compute epoch from " + date;
throw std::range_error(cause);
}
}
Date::~Date()
{
}
void
Date::add(long val, TimeUnit u) {
switch(u){
case DAY:
m_time += 86400*val;
break;
case HOUR:
m_time += 3600*val;
break;
case MINUTE:
m_time += 60*val;
break;
case SECOND:
m_time += val;
break;
default:
throw std::invalid_argument("Unknown TimeUnit specified");
}
}
bool
Date::operator==(const Date& o) const
{
return m_time == o.m_time;
}
bool
Date::operator!=(const Date& o) const
{
return ! (*this==o);
}
bool
Date::operator<(const Date &other) const
{
return m_time < other.m_time;
}
bool
Date::operator<=(const Date &other) const
{
return m_time <= other.m_time;
}
bool
Date::operator>(const Date &other) const
{
return m_time > other.m_time;
}
bool
Date::operator>=(const Date &other) const
{
return m_time >= other.m_time;
}
std::ostream&
operator<<(std::ostream& out, const Date &d)
{
struct tm* tm = localtime(&d.m_time);
char buffer[20];
strftime(buffer, 20, Date::FORMAT.c_str(), tm);
out << buffer << SEPARATOR;
return out;
}
std::istream&
operator>>(std::istream &in, Date &d)
{
std::string buf;
std::getline(in, buf, SEPARATOR);
Date o(buf);
d = o;
return in;
}
#include <iostream>
#include <fstream>
int
main(void)
{
Date d;
std::cout << d << std::endl;
std::ofstream out("tmp.txt");
out << d;
out.close();
std::ifstream in("tmp.txt");
Date d2;
in >> d2;
in.close();
std::cout << d2 << std::endl;
}
最后,我是如何测试它的:
$ for i in `seq 1 10`; do echo "test $i:"; ./test; rm tmp.txt; done
test 1:
26/03/2016 00:30:31;
26/03/2016 00:30:31;
test 2:
26/03/2016 00:30:31;
26/03/2016 00:30:31;
test 3:
26/03/2016 00:30:31;
25/03/2016 23:30:31;
test 4:
26/03/2016 00:30:31;
26/03/2016 00:30:31;
test 5:
26/03/2016 00:30:31;
25/03/2016 23:30:31;
test 6:
26/03/2016 00:30:31;
26/03/2016 00:30:31;
test 7:
26/03/2016 00:30:31;
25/03/2016 23:30:31;
test 8:
26/03/2016 00:30:31;
25/03/2016 23:30:31;
test 9:
26/03/2016 00:30:31;
25/03/2016 23:30:31;
test 10:
26/03/2016 00:30:31;
26/03/2016 00:30:31;
最佳答案
问题是struct tm t;
在采用字符串的 Date 构造函数中,它从不初始化对象,因此它的 tm_isdst 字段具有未指定的值。正确设置 tm_isdst 字段,您将获得一致多次运行的结果。
关于c++ - 使用 strptime/strftime 的结果不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36229908/
“strftime”中的“strf”是什么意思?我在谷歌上搜索了很多次这个问题,但没有找到答案。知道这些字母代表什么会让我更容易记住这个函数的名称。 最佳答案 来自 man 3 strftime :
我制作了这个脚本来跟踪我当前的时间和当前的电池百分比(然后将其附加到剪贴板,以便我可以将其粘贴到某个地方): from tkinter import * from urllib import pars
我想从我的 Sqlite 数据库中的日期时间列中获取一个月中的某一天。我可以简单地使用 select strftime('%d',myColumn) from myTable 但是这些值以 UTC 格
我编写了一个 perl 脚本,我在其中调用一个子例程将字段插入到数据库表中。该子例程在除主 perl 文件 Test.pl 之外的另一个文件 Test.pm 中调用。在 Test.pm 中,我将以下字
我们有一个 C++/MFC 应用程序,它允许用户通过配置文件自定义日期格式。不想重新发明轮子,我将格式字符串传递给 CTime::Format("") 以进行实际格式化。在幕后,Format 调用了标
下面列出了两行代码。两者对日期和时间的期望相同,但只有一个有效。我正在使用 R 3.1。 以下不起作用: DateTime2=strftime("08/13/2010 05:26:24.350", f
我使用strftime获得ISO 8601格式(%V)的星期数,并希望转换为前景中显示的星期数(猜测是格里高利历法)。 Outlook日历设置为: 一周的第一天(星期日) 一年的第一周1月1日开始 在
为什么此页面的代码不起作用?:http://strftime.org/ 。我想输出不带前导零的日期和月份,例如 'm/d/yyyy' 例如:“1992 年 4 月 5 日”是 '04/05/1992'
谁能告诉我为什么时区返回为“山区夏令时间”而不是 MST ? import time print time.strftime( "%H:%M %A %Z %d %b %y") 最佳答案 根据您提出的另
我正在尝试更新一些 csv 文件,稍后再更新我的数据库。看来每个月的 strftime 都会混淆一天。我在德国,我们写的是日期 DD/MM/YYYY 而不是 MM/DD/YYYY。 如何从 germa
我当前的计算机显示 CET 时间: $ date Mon Dec 1 21:31:41 CET 2014 但是我的区域设置是 en_US: $ locale | grep -i time LC_TI
strftime('%s', 'now', 'start of month') AND strftime('%s', 'now', 'start of month', '+1 month', '-1
我正在使用以下日期格式: d.strftime("%A, %d %B %Y %H:%m") 并且由于工作日的长度 (%A) 发生了变化,我想总是打印工作日10个字符,左边补空格,右对齐。 有点像 d.
Python3 strftime 产生的结果因平台而异。例如: OS X 10.10.5 >>> sys.version '3.5.2 (v3.5.2:4def2a2901a5, Jun 26 201
大家好,我想知道是否有人可以向我指出这一点。我目前正在尝试在 cmd 提示符的右上角显示日期和时间,但我得到的字符比我要求的要多。这是我目前所拥有的 time_t rawtime; struct tm
#include #include using namespace std; int main() { char dateStringBuffer[256] = ""; tm da
我试过 strftime( ) 来获取格式化的时间戳。 char ft[ 256 ]; struct tm *tmp; strftime( ft, 256, "%D - %T", tmp ); 我的问
char buffer[800]; struct tm str_time; str_time.tm_mday = Cur_Day; str_time.t
例如,时间戳是1403667010.574,使用Time.at(1403667010.574).strftime('%Y-%m-%d %H:%M:%S.%L %z'),则结果为 "2014-06-25
我试图将 YYYY-mm-dd 23:59:59 格式的时间保存到带有日期时间的 mysql 数据库列中。我不明白为什么总是忽略分钟和秒 00 ?非常感谢您的帮助。 PHP: $time = strf
我是一名优秀的程序员,十分优秀!