1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C > stdint.hを使うことで処理が変わってしまう > ミス: unsigned intをuint8_tで定義した

Last updated at Posted at 2017-08-02

#include <stdio.h>
#include <time.h> // use for test. not available on IDE

time_t getElapsedSecond(int yyyy, int mm, int dd, int hh, int nn, int ss){
    struct tm workdt;
    workdt.tm_isdst = -1; // summer time flag
    workdt.tm_year = yyyy - 1900;
    workdt.tm_mon = mm - 1;
    workdt.tm_mday = dd;
    workdt.tm_hour = hh;
    workdt.tm_min = nn;
    workdt.tm_sec = ss;
    return mktime(&workdt);
}

void getDateTimeString(time_t srcdt, char *dstPtr)
{
    if (dstPtr == NULL) {
        return; // error
    }
    struct tm *workdt;
    workdt = localtime(&srcdt); 
    workdt->tm_year = workdt->tm_year + 1900;
    sprintf(dstPtr, "%04d/%02d/%02d %02d:%02d:%02d", 
        workdt->tm_year, workdt->tm_mon + 1, workdt->tm_mday, 
        workdt->tm_hour, workdt->tm_min, workdt->tm_sec
    );
}

typedef struct {
	unsigned int OI; // [s]
	unsigned int MI; // [s]
	unsigned int W; // [s]
	unsigned int R; // [s]
	unsigned int N; // [s]
} SETTING_t;

time_t getNextWtime(time_t torigin, time_t tnow, SETTING_t *setPtr)
{
	if (setPtr == NULL) {
		return 0; // error // TOOD: 0z > use error value for time_t
	}
	// case 1: (now - origin) % OI == 0
	unsigned long A = (tnow - torigin) / setPtr->OI;
	if ((tnow - torigin) % setPtr->OI == 0) {
		return torigin + (A + 1) * setPtr->OI;
	}

	// case 2: (now - origin) % OI != 0

	//   case 2-a: (now - A*OI) % MI == 0
	
	//   case 2-b: (now - A*OI) % MI != 0
	
	return 0; // TODO: 0z > remove after debug
}

void AssertCheck(time_t torigin, time_t tnow, time_t tnextWans, SETTING_t *setPtr)
{
	char szbuf[30];
	if (setPtr == NULL) {
		return; // error
	}
	time_t tnextWres = getNextWtime(torigin, tnow, setPtr);

    getDateTimeString(tnow, szbuf);
	printf("%s - ", szbuf);
    getDateTimeString(tnextWres, szbuf);
	printf("%s:", szbuf);
	printf("%c", (tnextWres == tnextWans) ? 'T' : 'F');
	printf("\r\n");
}

void Test_group_run()
{
	time_t torigin = getElapsedSecond(2017, 8, 2, 13, 48, 55);
	SETTING_t setting = { 3600, 600, 60, 5, 3 }; // OI, MI, W, R, N

	// case (2)
	AssertCheck(torigin,
		        getElapsedSecond(2017, 8, 2, 13, 48, 55),
           	    getElapsedSecond(2017, 8, 2, 14, 48, 55),
	            &setting);
	
}

int main(void) {
	Test_group_run();

	return 0;
}
run
2017/08/02 13:48:55 - 2017/08/02 14:48:55:T

SETTING_tの定義に関してunsigned intをuint8_tに変更することで結果がTからFに変わってしまった。

優先作業が終わってから時間に余裕があれば見直すかもしれない。

教えていただきました

@mt08 さんのコメント
にて型の間違いを教えていただきました。

情報感謝です。

1
0
8

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?