gpt4 book ai didi

c++ - 如何从 C++ 定义中获取编译日期?

转载 作者:太空宇宙 更新时间:2023-11-04 07:26:30 25 4
gpt4 key购买 nike

所以我玩arduino时钟。这是它的 wiki .它需要类似的设置:

clock.fillByYMD(2013,1,19);//Jan 19,2013
clock.fillByHMS(15,28,30);//15:28 30"
clock.fillDayOfWeek(SAT);//Saturday

所以我尝试解析:

char compileTime[] = __TIME__;

到目前为止我得到了:

  byte hour = getInt(compileTime, 0);
byte minute = getInt(compileTime, 3);
byte second = getInt(compileTime, 6);
unsigned int hash = hour * 60 * 60 + minute * 60 + second;
clock.fillByHMS(hour, minute, second);
clock.setTime();

哪里:

char getInt(const char* string, const int & startIndex) {
return int(string[startIndex] - '0') * 10 + int(string[startIndex+1]) - '0';
}

我想知道如何通过编译器定义解析来设置fillByYMDfillDayOfWeek

最佳答案

由于(数字)月份和工作日不在编译时数据中,因此您需要进行一些转换;这假定一个 get4DigitInt 和对 getInt 的轻微更改以允许在第一个位置有一个空格。

char compileDate[] = __DATE__;

int year = get4DigitInt(compileDate, 7);
int day = getInt(compileDate, 4); // First character may be space
int month;
switch(compileDate[0]+compileDate[1]+compileDate[2]) {
case 'J'+'a'+'n': month=1; break;
case 'F'+'e'+'b': month=2; break;
case 'M'+'a'+'r': month=3; break;
case 'A'+'p'+'r': month=4; break;
case 'M'+'a'+'y': month=5; break;
case 'J'+'u'+'n': month=6; break;
case 'J'+'u'+'l': month=7; break;
case 'A'+'u'+'g': month=8; break;
case 'S'+'e'+'p': month=9; break;
case 'O'+'c'+'t': month=10; break;
case 'N'+'o'+'v': month=11; break;
case 'D'+'e'+'c': month=12; break;
}
std::tm time = { 0, 0, 0, day, month - 1, year - 1900 };
std::mktime(&time);
int day_of_week = time.tm_wday; // 0=Sun, 1=Mon, ...

std::cout << "Time: " << hour << ":" << minute << ":" << second << std::endl;
std::cout << "Date: " << year << "-" << month << "-" << day << std::endl;
std::cout << "Day: " << day_of_week << std::endl;

关于c++ - 如何从 C++ 定义中获取编译日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17907783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com