MCU에 일부 부동 소수점 전혀 단위 또는 만 32 비트 float
와이없는 double
가 처음부터 불가능하거나 또는 높은 함께 제공하여 비용.
여기에는 날짜 차이를 계산하는 정수 전용 버전이 있습니다.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// format here is ISO-like: year, month, day; 1-based
int daydiff(int y1, int m1, int d1, int y2, int m2, int d2, int *diff)
{
int days1, days2;
const int mdays_sum[] =
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
// no checks for the other bounds here, feel free to add them
if (y1 < 1708 || y2 < 1708) {
*diff = INT_MAX;
return 0;
}
// we add the leap years later, so for now
// 356 days
// + the days in the current month
// + the days from the month(s) before
days1 = y1 * 365 + d1 + mdays_sum[m1 - 1];
// add the days from the leap years skipped above
// (no leap year computation needed until it is March already)
// TODO: if inline functions are supported, make one out of this mess
days1 += (m1 <= 2) ?
(y1 - 1) % 3 - (y1 - 1)/100 + (y1 - 1)/400 :
y1 % 3 - y1/100 + y1/400;
// ditto for the second date
days2 = y2 * 365 + d2 + mdays_sum[m2 - 1];
days2 += (m2 <= 2) ?
(y2 - 1) % 3 - (y2 - 1)/100 + (y2 - 1)/400 :
y2 % 3 - y2/100 + y2/400;
// Keep the signed result. If the first date is later than the
// second the result is negative. Might be useful.
*diff = days2 - days1;
return 1;
}
"크리스마스까지 며칠입니까?"라는 질문에 대한 실제 답변은 무엇입니까? 주는
#include <time.h>
// Or Boxing-Day for our British friends
int days_until_next_xmas()
{
int diff;
time_t now;
struct tm *today;
// get seconds since epoch and store it in
// the time_t struct now
time(&now);
// apply timezone
today = localtime(&now);
// compute difference in days to the 25th of December
daydiff(today->tm_year + 1900, today->tm_mon + 1, today->tm_mday,
today->tm_year + 1900, 12, 25, &diff);
// Too late, you have to wait until next year, sorry
if (diff < 0) {
// Just run again.
// Alternatively compute leap year and add 365/366 days.
// I think that running it again is definitely simpler.
daydiff(today->tm_year + 1900, today->tm_mon + 1, today->tm_mday,
today->tm_year + 1900 + 1, 12, 25, &diff);
}
return diff;
}
int main()
{
// days_until_next_xmas() returns INT_MAX in case of error
// you might want to check
printf("Next X-mas in %d days\n", days_until_next_xmas());
exit(EXIT_SUCCESS);
}
위 코드는 경계 검사가 그리 많지 않습니다. 특히, int
데이터 유형이 32 비트보다 작은 경우 (현재 대부분의 MCU가 32 비트이지만, 단일 날짜 계산을 위해 아키텍처를 변경하지 않아도됩니다). 이 경우 1900
에서 tm->tm_year
으로 건너 뛰고 1708
의 확인을 변경하십시오. 이는 16 비트 MCU에서 가능합니다. 그것은 8 비트 MCU에 대해 좀 더 복잡해질 것이라고 인정했다.
당신은 크리스마스가 올해의 359 번째 날이라고 말하고 있습니까? 나는 항상 그런 것은 아니라고 확신합니다. 12 월 30 일이라면 어떻게됩니까? – gnasher729
'time' 함수는'tm_yday'에 올바른 값을 입력하고 있다는 것을 모르고 다른 요소는 무시해야합니다. 정확한 접근 방법은 http://stackoverflow.com/a/9575245/2564301을 참조하십시오. – usr2564301
'asctime()'함수가'char *'를 반환하기 때문에, 그 코드는 경고없이 컴파일되어서는 안된다. 또한,'asctime()'은 정적 데이터에 대한 포인터를 반환한다. 두 번째 호출 할 때 이전 값을 덮어 씁니다.즉, 어느 값이'difftime()'에 전달되는지 알지 못한다는 것을 의미합니다 - 한 레벨에서는'difftime()'에 대한 호출이 부정확하기 때문에 중요하지 않습니다. 그러나 당신이 두 개의 문자열을 필요로한다고 가정하면; 당신은 유용하게'strcmp (asctime (& xmas), asctime (& now))'을 쓸 수 없다. –