显示一年中新的每一天的新信息
Displaying new information for every new day of the year
我是C++的新手,目前正试图将当前日期/时间存储在单独的变量中,然后每次日期更改时,都应该显示新的信息。我的代码应该运行如下:
- 存储日期
- 检查日期
- 如果日期与存储的日期不同,则显示信息
- 存储新日期
- 重复过程
如何创建商店旧日期和检查新日期功能?
这是我到目前为止的代码,任何帮助都将不胜感激。
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
time_t t = time(NULL);
tm* timePtr = localtime(&t);
cout << "seconds= " << timePtr->tm_sec << endl;
cout << "minutes = " << timePtr->tm_min << endl;
cout << "hours = " << timePtr->tm_hour << endl;
cout << "day of month = " << timePtr->tm_mday << endl;
cout << "month of year = " << timePtr->tm_mon << endl;
cout << "year = " << timePtr->tm_year+1900 << endl;
cout << "weekday = " << timePtr->tm_wday << endl;
cout << "day of year = " << timePtr->tm_yday << endl;
cout << "daylight savings = " << timePtr->tm_isdst << endl;
return 0;
}
这里有一个简单的例子:
#include <fstream>
#include <time.h>
#include <iostream>
int main(int argc, char* argv [])
{
// first argument is program name, second is timefile
if (argc == 2)
{
// extract time from file (if it exists)
time_t last_raw;
std::ifstream ifs;
ifs.open(argv[1],std::ifstream::in);
if (ifs.good())
ifs >> last_raw;
else
time(&last_raw); // it does not exist, so create it
ifs.close();
// get current time
time_t now_raw;
time(&now_raw);
// compare new to old
struct tm * now = localtime(&now_raw);
struct tm * last = localtime(&last_raw);
if (now->tm_mday != last->tm_day || now->tm_mon != last->tm_mon || now->tm_year != last->tm_year)
{
// print whatever here
}
// save new time out to file
std::ofstream ofs;
ofs.open (argv[1], std::ofstream::out | std::ofstream::trunc); // truncate to overwrite old time
ofs << now_raw;
ofs.close();
}
return 0;
}
相关文章:
- 维护unordered_map但同时每一步都需要最低的映射值
- 查找矩阵C++中每一列和每一行的最小和最大元素
- C++ Chrono 确定一天是否是周末?
- 编写了一个C++代码来检查表达式是否具有平衡括号并且我的代码未运行.我已经卡了一天了
- 我正在尝试制作一个程序,在添加 n 天(整数)后告诉一个人什么是一天(例如星期一等)
- C++ 尝试优化每一帧将数据打印到二进制文件
- 如何允许用户选择何时停止三列中的每一列的旋转
- 以不同的方式替换每一项,regex c++
- 当我在每一帧中多次使用正则表达式时,FPS 下降了
- 使用 OpenCV C++ 每 1 分钟创建新的视频文件
- std::get_time 没有解析一天
- 以秒为单位将时间从纪元转换为一年中的某一天?
- 在二维向量的每一列中查找最大值
- 为什么除了最后一帧之外,每一帧都没有调用命令
- 解决一年中的一天 c++
- 每隔一行或每一列提取一个特征矩阵作为一个新矩阵
- 显示一年中新的每一天的新信息
- 二维数组:如何获得价值的每一天,而不是一周
- 每一天至少有一个观察者失败
- 是否有可能使用log4cXX为每一天创建一个新文件