この特定の種類のものは何度も答えられていますが、ここでの質問はctime()
または日付/時刻変換よりも一般的なC++のものと関連していると思います。私はちょうどこれでこれを試してみた。だから、ここのコードは次のとおりです。ctime()と日付/時刻変換
#include <iostream>
#include <ctime>
using std::cout;
using std::endl;
using std::string;
void strTime(int iM, int iD, int iY)
{
time_t rwTime;
time(&rwTime); // current epoch time
// 1st set:: using tmInfo_11
struct tm *tmInfo_11;
tmInfo_11 = localtime(&rwTime);
tmInfo_11->tm_mon = iM - 1;
tmInfo_11->tm_mday = iD;
tmInfo_11->tm_year = iY - 1900;
mktime(tmInfo_11);
cout << "tmInfo_11 RESULt: " << tmInfo_11->tm_wday << endl;
// 2nd set:: using tmInfo_22 //
struct tm tmInfo_22;
tmInfo_22 = *localtime(&rwTime);
tmInfo_22.tm_mon = iM - 1;
tmInfo_22.tm_mday = iD;
tmInfo_22.tm_year = iY - 1900;
mktime(&tmInfo_22);
cout << "tmInfo_22 RESULt: " << tmInfo_22.tm_wday << endl;
}
int main()
{
int iMM=12, iDD=9, iYY=2009;
strTime(iMM, iDD, iYY);
}
と、私の質問は:コードのこれら2セット間の違いは何ですか?いずれにせよ、私は同じことを達成することができます。注目すべき違いは、各セットの最初の2行であり、私はそれをすべて理解できなかったことを認めなければならない。だから誰も親切に私にそれを説明できますか?また、投与量1は、他の投与量よりも利点/不利な点がありますか?乾杯!!
私は最後にのコードを使用しましたが、このコードは私に望ましい結果をもたらしました。だから、基本的には、将来の参考のためです:
strftime()
で
#include <iostream>
#include <fstream>
#include <ctime>
using std::cout;
using std::endl;
using std::string;
tm testTime(int iM, int iD, int iY);
int main()
{
char tmBuff[20];
int iMM=12, iDD=9, iYY=2009;
tm myDate = testTime(iMM, iDD, iYY);
strftime(tmBuff, sizeof(tmBuff), "%a, %b %d, %Y", &myDate);
cout << "TESt PRINt TIMe: " << tmBuff << endl;
}
tm testTime(int iM, int iD, int iY)
{
time_t rwTime;
struct tm tmTime;
tmTime = *localtime(&rwTime);
tmTime.tm_mon = iM - 1;
tmTime.tm_mday = iD;
tmTime.tm_year = iY - 1900;
mktime(&tmTime);
return tmTime;
}
それが指定する*localtime(&rwTime)
を必要としませんNOTE(tmTimeは後で上書きなっているにもかかわらず)それ以外の年(%Y)がありません作業。助けてくれてありがとう。乾杯!!
最初のチャンクは、localtimeによって割り当てられた構造体で機能します。 2番目のチャンクは、localtimeによって割り当てられた構造体の値が割り当てられたローカル構造体で動作します。しかし、これはとても基本的なように私はあなたの質問を誤解している必要があります。 – HonkyTonk
@HonkyTonk:ありがとう!私はまだいくつかの基本を理解しようとしています。乾杯!! – MacUsers