记录了一下项目制作过程中 所涉及的不熟悉的部分源码可至GitHub查看
相关知识
1. ctime 头文件
1.1 time_t time(time_t *timer)
: 返回自纪元以来经过的时间,以秒为单位
1 2 time_t currentTime;time (& currentTime);
1.2 struct tm *localtime(const time_t *timer)
: 将 time_t
格式的时间转换为本地时间,返回一个指向 tm
结构体的指针
1 2 3 time_t currTime;time (&currTime);struct tm * timeTmp = locatime (&currTime);
1.3 char *asctime(const struct tm *timeptr)
: 将 tm
结构体表示的时间转换为字符串形式的时间,并返回指向静态字符串的指针
1 2 3 4 time_t currTime;time (&currTime);struct tm * timeTmp = locatime (&currTime);char *timeString = asctime (&timeTmp);
1.4 tm结构体(包含在ctime头文件中)
1 2 3 4 5 6 7 8 9 10 11 12 struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; };
1.5 使用
先使用time()函数获取自1970年1月1日午夜(格林威治时间)起算的秒数,然后使用localtime()函数将其转化为本地时间 (使用tm结构体存储),最后根据需要进行打印
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <ctime> using namespace std;int main () { time_t now; time (&now); struct tm * lt = localtime (&now); cout << "Local time is: " << lt->tm_year + 1900 << "-" << lt->tm_mon + 1 << "-" << lt->tm_mday << " " << lt->tm_hour << ":" << lt->tm_min << ":" << lt->tm_sec << endl; char * timeStr = asctime (lt); cout << "Formatted time is: " << timeStr << endl; char buffer[255 ]; strftime (buffer, 255 , "%Y-%m-%d %H:%M:%S" , lt); cout << "Formatted time is: " << buffer << endl; return 0 ; } ----------输出结果----------------- Local time is: 2024 -4 -28 17 :15 :42 Formatted time is: Sun Apr 28 17 :15 :42 2024 Formatted time is: 2024 -04 -28 17 :15 :42
2. CMake简单使用
2.1 创建一个CMakeLists.txt文件
2.2 创建一个名为build的文件夹
避免编译产物与代码文件混在一起
2.3 在CMakeLists.txt中写入内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 cmake_minimum_required (VERSION 3.20 )project (dinnerSystem)add_executable (dinnerSystem)file (GLOB sources CONFIGURE_DEPENDS *.cpp *.h)target_sources (dinnerSystem PUBLIC ${sources} )
2.4 在build文件夹中打开cmd
输入cmake ..
cmake --build .
3. 解决中文乱码
使用UTF-8 无BOM 可以在VS2022下载插件Force UTF-8(No BOM)
4.Qt的基础使用
见另一篇笔记