c++ - localtime c++ 无法正常工作
问题描述
我在 C++ 中遇到本地时间问题。我使用 vs 代码和 localtime_s 不工作。当我使用本地时间时,它会为今天的小时、分钟和秒提供非常大的数字(如“6417576:16480216:6422248”)。例如,如何将其更改为“16:30:30”之类的内容?我找不到任何对我有帮助的信息,所以也许你们会的。这是我的代码:
void write(string team_name)
{
//current time
time_t current;
current=time(0);
struct tm now;
localtime(¤t);
stringstream time;
time<<now.tm_hour<<":"<<now.tm_min<<":"<<now.tm_sec;
//writing filenames and times
stringstream filename;
filename<<"Filename"<<"_"<<team_name<<time.str()<<".txt";
ofstream file;
file.open(filename.str());
file<<show();
void print_time();
file.close();
}
};
解决方案
没有任何东西以任何方式连接这两个语句:
struct tm now;
localtime(¤t);
第二条语句解决了所有问题,但随后丢弃了结果,因此其中的所有这些字段now
仍设置为创建变量时它们具有的任意值。
该localtime
函数返回一个指向结构的指针,因此您应该使用它:
struct tm *now = localtime(¤t);
// use now->something
推荐阅读
- vert.x - Vertx 事件总线未在单个 JVM 中通信
- unit-testing - 如何根据 app.config 键值运行特定的 xunit 测试用例
- jquery - 如何使用jquery从数组结果中删除空值
- java - Unity:从 Unity 调用 Companion 对象内的 Android Kotlin 函数
- c# - 用十六进制设置材质的颜色
- spring-boot - Spring Boot 无法开始使用 Atomikos 并且 JMS MQ 连接失败
- r - 如何映射数据框,这是一个 tidyeval 错误吗?
- sql - 将自定义格式字符串转换为日期
- java - JPA 嵌套映射到没有基表的视图
- swift - tableView 中的步进器,发送了无法识别的选择器