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;
Member Meaning Range
tm_sec seconds after the minute 0-61*
tm_min minutes after the hour 0-59
tm_hour hours since midnight 0-23
tm_mday day of the month 1-31
tm_mon months since January 0-11
tm_year years since 1900
tm_wday days since Sunday 0-6
tm_yday days since January 1 0-365
tm_isdst Daylight 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:

struct tm,time,localtime,mktime,ctime,gmtime,difftime,asctime,strftime_第1张图片

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:
struct tm,time,localtime,mktime,ctime,gmtime,difftime,asctime,strftime_第2张图片

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:
struct tm,time,localtime,mktime,ctime,gmtime,difftime,asctime,strftime_第3张图片

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:
struct tm,time,localtime,mktime,ctime,gmtime,difftime,asctime,strftime_第4张图片

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:
struct tm,time,localtime,mktime,ctime,gmtime,difftime,asctime,strftime_第5张图片

9. strftime

<ctime>
size_t strftime ( char * ptr, size_t maxsize, const char * format,
                  const struct tm * timeptr );
Format time as string
specifier Replaced by Example
%a Abbreviated weekday name * Thu
%A Full weekday name * Thursday
%b Abbreviated month name * Aug
%B Full month name * August
%c Date and time representation * Thu Aug 23 14:55:02 2001
%d Day of the month (01-31) 23
%H Hour in 24h format (00-23) 14
%I Hour in 12h format (01-12) 02
%j Day of the year (001-366) 235
%m Month as a decimal number (01-12) 08
%M Minute (00-59) 55
%p AM or PM designation PM
%S Second (00-61) 02
%U Week number with the first Sunday as the first day of week one (00-53) 33
%w Weekday as a decimal number with Sunday as 0 (0-6) 4
%W Week number with the first Monday as the first day of week one (00-53) 34
%x Date representation * 08/23/01
%X Time representation * 14:55:02
%y Year, last two digits (00-99) 01
%Y Year 2001
%Z Timezone name or abbreviation CDT
%% A % 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.

你可能感兴趣的:(struct tm,time,localtime,mktime,ctime,gmtime,difftime,asctime,strftime)