年齢・月齢

/**
* [年齢]の文字列を作成 (年"%03dY" / 月"%02dM")
* 1歳に満たない場合は[月齢]を返す
*
* @param[in] birth "20070601"
* @param[out] buf
*/
void CreateAge( const CString& birth, char* buf )
{
if( birth.GetLength() == 0 ){
buf[0] = '\0';
return;
}

COleDateTime current = COleDateTime::GetCurrentTime();

int nYear  = current.GetYear()  - atoi( birth.Mid( 0, 4 ) );
int nMonth = current.GetMonth() - atoi( birth.Mid( 4, 2 ) );
int nDay   = current.GetDay()   - atoi( birth.Mid( 6, 2 ) );

if( nYear < 0 ){
buf[0] = '\0';
return;// エラー
}else if( nYear == 0 ){// 0歳の場合
if( nMonth < 0 ){// まだ誕生月でない
buf[0] = '\0';
return;// エラー
}else if( nMonth == 0 ){// 0ヶ月の場合
if( nDay < 0 ){// まだ誕生日でない
buf[0] = '\0';
return;// エラー
}
}else{
if( nDay < 0 ){
nMonth--;
}
}

// 月齢を返す
sprintf( buf, "%02dM", nMonth );
}else{
if( nMonth < 0 ){// まだ誕生月でない
nYear--;
nMonth += 12;
}else if( nMonth == 0 ){// 同じ月の場合
if( nDay < 0 ){// まだ誕生日でない
nYear--;
nMonth += 11;
}
}

if( nYear <= 0 ){
// 月齢を返す
sprintf( buf, "%02dM", nMonth );
}else{
// 年齢を返す
sprintf( buf, "%03dY", nYear );
}
}
}
最終更新:2009年07月16日 18:38
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。