struct tm,time,localtime,mktime,ctime,gmtime,difftime,asctime,strftime

1. struct tm

int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
MemberMeaningRange
tm_secseconds after the minute0-61*
tm_minminutes after the hour0-59
tm_hourhours since midnight0-23
tm_mdayday of the month1-31
tm_monmonths since January0-11
tm_yearyears since 1900
tm_wdaydays since Sunday0-6
tm_ydaydays since January 10-365
tm_isdstDaylight Saving Time flag

2. time      

Get current time
time_t time ( time_t * timer );

Example

/ *time example* /
#include <iostream>
#include <ctime>
using namespace std;


int main()
{
time_t seconds;
time(&seconds);
//或者seconds = time(NULL);
cout << seconds / 3600 << " hours since January 1,1970" << endl;

return 0;
}

Possible output:


3. localtime

<ctime>
struct tm * localtime ( const time_t * timer );
Convert time_t to tm as local time

Example

/*localtime example*/
#include <iostream>
#include <ctime>
using namespace std;


int main()
{
	time_t rawtime;
	struct tm* timeinfo;
	time(&rawtime);
	timeinfo = localtime(&rawtime);
	cout << "current local time and date: " << asctime(timeinfo);
	return 0;
}

Output:

4. mktime

time_t mktime ( struct tm * timeptr );
Convert tm structure to time_t

Example

/* mktime example: weekday calculator */
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
	time_t rawtime;
	struct tm* timeinfo;
	int year,month,day;
	char* weekday[] = {
		"Sunday", 
		"Monday",
		"Tuesday", 
		"Wednesday",
		"Thursday",
		"Friday", 
		"Saturday"
	};
	
	cout << "Enter year: "; cin >> year;
	cout << "Enter month: "; cin >> month;
	cout << "Enter day: "; cin >> day;
	//将当前时间赋给rawtime
	time(&rawtime);
	//将rawtime转化为本地时间赋给timeinfo
	timeinfo = localtime(&rawtime);
	timeinfo->tm_year = year - 1900;
	timeinfo->tm_mon = month - 1;
	timeinfo->tm_mday = day;

	mktime(timeinfo);
	cout << "That day is a " << weekday[timeinfo->tm_wday] << endl;

	return 0;
}

Output:

5.ctime

<ctime>
char * ctime ( const time_t * timer );
Convert time_t value to string

Example

/*ctime example*/
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
	time_t rawtime;
	time(&rawtime);
	cout << "Current local time is: " << ctime(&rawtime);

	return 0;
}

Output:

6. gmtime

<ctime>
struct tm * gmtime ( const time_t * timer );
Convert time_t to tm as UTC time

Example

/*gmtime example*/
#include <iostream>
#include <ctime>
using namespace std;

#define MST (-7)
#define UTC (0)
#define CCT (+8)

int main()
{
	time_t rawtime;
	struct tm * ptm;
	//将当前时间赋给rawtime
	time(&rawtime);cout << "time: " << rawtime << endl;
	//Convert time_t to tm
	ptm = gmtime(&rawtime);
	cout << "Current time around the World: ";
	cout << "Phoenix, AZ (U.S.): " << (ptm->tm_hour + MST) % 24
		<< ":" << ptm->tm_min << endl;
	cout << "Reykjavik (Iceland): " << (ptm->tm_hour + UTC) % 24
		<< ":" << ptm->tm_min << endl;
	cout << "Beijing (China): " << (ptm->tm_hour + CCT) % 24
		<< ":" << ptm->tm_min << endl;
	return 0;
}

Output:


7. difftime

<ctime>
double difftime ( time_t time2, time_t time1 );
Return difference between two times

Example

/*diifftime example*/
#include <iostream>
#include <ctime>
#include <string>
using namespace std;

int main()
{
	time_t start,end;
	string str;
	double dif;

	time(&start);
	cout << "Please input your name: ";
	cin >> str;
	time(&end);
	dif = difftime(end,start);
	cout << "Hi, " << str << "! It took you " << dif 
		<< "seconds to type your name. " << endl;

	return 0;
}

Output:

8. asctime

<ctime>
char * asctime ( const struct tm * timeptr );
Convert tm structure to string

Example

/* asctime example */
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
	time_t rawtime;
	struct tm * timeinfo;
	time(&rawtime);
	timeinfo = localtime(&rawtime);
	cout << "Current date/time is: " << asctime(timeinfo);
	return 0;
}

Output:


9. strftime

<ctime>
size_t strftime ( char * ptr, size_t maxsize, const char * format,
                  const struct tm * timeptr );
Format time as string
specifierReplaced byExample
%aAbbreviated weekday name *Thu
%AFull weekday name *Thursday
%bAbbreviated month name *Aug
%BFull month name *August
%cDate and time representation *Thu Aug 23 14:55:02 2001
%dDay of the month (01-31)23
%HHour in 24h format (00-23)14
%IHour in 12h format (01-12)02
%jDay of the year (001-366)235
%mMonth as a decimal number (01-12)08
%MMinute (00-59)55
%pAM or PM designationPM
%SSecond (00-61)02
%UWeek number with the first Sunday as the first day of week one (00-53)33
%wWeekday as a decimal number with Sunday as 0 (0-6)4
%WWeek number with the first Monday as the first day of week one (00-53)34
%xDate representation *08/23/01
%XTime representation *14:55:02
%yYear, last two digits (00-99)01
%YYear2001
%ZTimezone name or abbreviationCDT
%%% sign%

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* strftime example */
#include <stdio.h>
#include <time.h>

int main ()
{
  time_t rawtime;
  struct tm * timeinfo;
  char buffer [80];

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );

  strftime (buffer,80,"Now it's %I:%M%p.",timeinfo);
  puts (buffer);
  
  return 0;
}

Example output:

Now it's 03:21PM.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值