strftime/strptime问题
我编写了一个函数来将日期时间从一种格式转换为另一种格式 -
int convertDateTime(char* source_fmt,char* dest_fmt,char* source,char* dest)
{
struct tm tmpptr;
if (strptime(source,source_fmt,&tmpptr) == NULL)
{
strcpy(dest,"");
return -1;
}
strftime(dest,100,dest_fmt,&tmpptr);
return 0;
}
它适用于大多数格式,但是当我使用 format = "%y%j" 时,我得到的只是 10001;朱利安日不起作用。
我在 Solaris 10 上使用 gcc。知道我需要更改什么吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查 strptime 的预期行为,在某些实现中它是贪婪的,并且会消耗尽可能多的数字。
这对我来说在 MacOS 上是个问题,随着 UNIX 标准合规性的努力,实现在 10.5 中发生了变化。在此之前,以下调用运行良好。
从 10.5 开始,您必须执行以下操作才能使其正常工作。
不过,您的编译器库和操作系统可能有所不同。
Check the expected behaviour of strptime, in some implementations it is greedy and will consume as many digits as it can.
This has been a problem for me on MacOS, the implementation changed in 10.5 with the UNIX standards compliance efforts. Before this the following call worked fine.
As of 10.5 you have to do the following to get it to work.
Your compiler libraries and OS may differ though.
我可以告诉你问题是什么。当您使用
%j
格式的strptime()
时,它仅填充struct tm
上的tm_yday
字段。顺便说一句,这并不限于 Solaris,CygWin gcc 也在做同样的事情。因为您的
strftime()
很可能使用其他字段(tm_mon
和tm_mday
),并且这些字段仍设置为零,所以 为什么你得到了错误的一年中的某一天。以下代码说明了这一点:
当您这样运行它时:
您会得到:
修复它很棘手,我不知道有任何标准时间函数可以重建
tm_mday
和tm_mon
来自tm_yday
。但是,如果您找不到解决方案,请尝试一下:
它将根据 tm_year 和 tm_yday 设置这两个字段,这有点混乱,但会得到至少你会去(也许你会找到更好的方法)。
我会在您的转换函数中插入对此的调用,并且仅在特定情况下调用它,以免覆盖已经设置的值:
这给出:
并且,像这里的所有代码一样,您应该彻底测试它。我很确定我得到了所有的边缘情况,但是,由于您没有为我的服务支付冷硬现金,您应该假设这只是一般建议,而不是特定的解决方案:-)
I can tell you what the problem is. When you use
strptime()
with the%j
format, it only populates thetm_yday
field on yourstruct tm
. This isn't limited to Solaris by the way, CygWin gcc is doing the same thing.Because your
strftime()
is most likely using other fields (tm_mon
andtm_mday
), and these are still set to zero, that's why you're getting the wrong day of the year.The following code illustrates this:
When you run it thus:
you get:
Fixing it is tricky, I'm not aware of any standard time function that will rebuild
tm_mday
andtm_mon
fromtm_yday
.But, if you're stuck for a solution, try this out:
It will set those two fields based on
tm_year
andtm_yday
and it's a bit of a kludge but will get you going at least (and maybe you'll find a better way).I would insert a call to this in your convert function and only call it under specific circumstances so as not to overwrite values that are already set:
which gives:
And, like all code here, you should test it thoroughly. I'm pretty certain I got all the edge cases but, since you haven't paid me cold hard cash for my services, you should assume this is general advice only, not a specific solution :-)
更新
原始答案(如下)假设“%y%j”格式用于输出(strftime)而不是输入(strptime)。 mktime 函数将从有效信息计算 yday,但反之则不行。
如果你想解码类似的东西,你需要手动完成。下面是一些示例代码。它经过了最少的测试,几乎肯定存在错误。您可能需要更改 ir,具体取决于您想要的输入格式。
=================================================
结束更新
调用 strptime() 后,调用 mktime() 它将填充结构体中任何缺失的成员。另外,您应该在开始之前将结构清零。
Update
The original answer (below) presumed that the "%y%j" format was used on the output (strftime) not the input (strptime). The mktime function will compute yday from valid info, but it doesn't work the other way.
If you want to decode something like that you need to do it manually. Some example code follows. It has had minimal testing and almost certainly has bugs. You may need to alter ir, depending in the input format you want.
================================================
End Update
After you call strptime(), call mktime() which will populate any missing members of the struct. Also, you should zero out the struct before beginning.