標準c取得時間

1086 ワード

// gettime.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <time.h>
#include <iostream>
using namespace std;

bool GetCurrTime(char* pszCurTime, int nIntervalTime);

void main () 
{ 
	char szTime[100] = {0};

    GetCurrTime(szTime, 300);

	cout << "current time: " << szTime << endl;

	return;
}

bool GetCurrTime(char* pszCurTime, int nIntervalTime)
{
	time_t	rt		= {0};;
	tm*		ptmInfo	= NULL;;
	int		nMin	= 0;

	try
	{
		time(&rt);
		ptmInfo = localtime(&rt);

		nMin = ptmInfo->tm_min - (ptmInfo->tm_min % (nIntervalTime / 60));

		sprintf(pszCurTime, "%d-%d-%d %d:%d:0", ptmInfo->tm_year + 1900, ptmInfo->tm_mon, ptmInfo->tm_mday, ptmInfo->tm_hour, nMin);
	}
	catch (...)
	{
		return false;
	}
	
	return true;
}