|
//time.h をインクルードする必要があります。#include <time.h>
//string.hをインクルードする必要があります。#include <string.h>
//stdlib.hをインクルードする必要があります。#include <stdlib.h>
char strTarget[] = "2001/05/05 21:31:30";
time_t dteResult;
// 文字列の長さをチェック
if (strlen(strTarget) >= 19)
{
char strTemp[5];
// 年取得
strcpy(strTemp, strTarget , 4);
int intYear = atoi(strTemp);
// 月取得
strcpy(strTemp, strTarget + 5, 2);
int intMonth = atoi(strTemp);
// 日取得
strcpy(strTemp, strTarget + 8, 2);
int intDay = atoi(strTemp);
// 時取得
strcpy(strTemp, strTarget + 11, 2);
int intHour = atoi(strTemp);
// 分取得
strcpy(strTemp, strTarget + 14, 2);
int intMinute = atoi(strTemp);
// 秒取得
strcpy(strTemp, strTarget + 17, 2);
int intSecond = atoi(strTemp);
// tm構造体に値を格納
tm tmTemp;
tmTemp.tm_year = intYear - 1900;
tmTemp.tm_mon = intMonth - 1;
tmTemp.tm_mday = intDay;
tmTemp.tm_hour = intHour;
tmTemp.tm_min = intMinute;
tmTemp.tm_sec = intSecond;
tmTemp.tm_isdst = -1;
// tm構造体→time_t型に変換(現地時刻をカレンダー値に)
dteResult = mktime(&tmTemp);
}
else
{
// エラー
dteResult = -1;
}
|