直接设置结构 tm 属性的值不起作用
Directly setting values of struct tm's attributes not working
为什么asctime(ptr)
什么也没有返回?结构体的所有变量都有值。有人能解释一下为什么会这样吗?
我也尝试使用strftime
,但结果是相同的。
#include <iostream>
#include <ctime>
#include <new>
//#include <cstdio>
using namespace std;
int main(int argc,char *argv[])
{
struct tm *ptr=new struct tm;
//char buf[50];
ptr->tm_hour=0;
ptr->tm_mon=0;
ptr->tm_year=0;
ptr->tm_mday=0;
ptr->tm_sec=0;
ptr->tm_yday=0;
ptr->tm_isdst=0;
ptr->tm_min=0;
ptr->tm_wday=0;
cout << asctime(ptr);
//strftime(buf,sizeof(char)*50,"%D",ptr);
//printf("%s",buf);
return 0;
}
下面的程序可以工作。用1去掉0就可以了。
struct tm *ptr = new struct tm();
char buf[50];
ptr->tm_hour = 1;
ptr->tm_mon = 1;
ptr->tm_year = 1;
ptr->tm_mday = 1;
ptr->tm_sec = 1;
ptr->tm_yday = 1;
ptr->tm_isdst = 1;
ptr->tm_min = 1;
ptr->tm_wday = 1;
cout << asctime(ptr)
ptr->tm_hour = 0;
ptr->tm_mon = 0;
ptr->tm_year = 0;
ptr->tm_mday = 1;
ptr->tm_sec = 0;
ptr->tm_yday = 0;
ptr->tm_isdst = 0;
ptr->tm_min = 0;
ptr->tm_wday = 0;
cout << asctime(ptr);
如果struct tm
的任何成员超出其正常范围,则asctime
的行为是未定义的。
特别是当日历日小于0时,该行为是未定义的(某些实现将tm_mday==0
处理为前一个月的最后一天)。
请查看http://en.cppreference.com/w/cpp/chrono/c/asctime和http://en.cppreference.com/w/cpp/chrono/c/tm了解更多详细信息。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 组合"%"和可选后缀时,自动属性传播有时不起作用
- [[maybe_unused]] 属性不起作用
- 当应用于静态方法时,为什么constexpr属性不起作用
- 当变量是属性时,流不起作用
- 直接设置结构 tm 属性的值不起作用