ctime 使用

Functions

Time manipulation
Conversion:

gmtime

function
<ctime>
struct tm * gmtime ( const time_t * timer );

Convert time_t to tm as UTC time

Uses the value pointed by timer to fill a tm structure with the values that represent the corresponding time, expressed as UTC (or GMT timezone).

Parameters

timer
Pointer to a time_t value representing a calendar time (see time_t).

 

Return Value

A pointer to a tm structure with the time information filled in.

This structure is statically allocated and shared by the functions gmtime and localtime. Each time either one of these functions is called the contents of this structure is overwritten.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* gmtime example */
#include <stdio.h>
#include <time.h>

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

int main ()
{
  time_t rawtime;
  tm * ptm;

  time ( &rawtime );

  ptm = gmtime ( &rawtime );

  puts ("Current time around the World:");
  printf ("Phoenix, AZ (U.S.) :  %2d:%02d/n", (ptm->tm_hour+MST)%24, ptm->tm_min);
  printf ("Reykjavik (Iceland) : %2d:%02d/n", (ptm->tm_hour+UTC)%24, ptm->tm_min);
  printf ("Beijing (China) :     %2d:%02d/n", (ptm->tm_hour+CCT)%24, ptm->tm_min);
  
  return 0;
}

 

mktime

function
<ctime>
time_t mktime ( struct tm * timeptr );

Convert tm structure to time_t

Interprets the contents of the tm structure pointed by timeptr as a calendar time expressed in local time. This calendar time is used to adjust the values of the members of timeptr accordingly and returned as an object of type time_t.

The original values of the members tm_wday and tm_yday of timeptr are ignored, and the ranges of values for the rest of its members are not restricted to their normal values (like tm_mday being between 1 and 31).

The object pointed by timeptr is modified, setting the tm_wday and tm_yday to their appropiate values, and modifying the other members as necessary to values within the normal range representing the specified time.

Parameters

timeptr
Pointer to a tm structure that contains a calendar time broken down into its components (see tm).

 

Return Value

A time_t value corresponding to the calendar time passed as argument.
On error, a -1 value is returned.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* mktime example: weekday calculator */
#include <stdio.h>
#include <time.h>

int main ()
{
  time_t rawtime;
  struct tm * timeinfo;
  int year, month ,day;
  char * weekday[] = { "Sunday", "Monday",
                       "Tuesday", "Wednesday",
                       "Thursday", "Friday", "Saturday"};

  /* prompt user for date */
  printf ("Enter year: "); scanf ("%d",&year);
  printf ("Enter month: "); scanf ("%d",&month);
  printf ("Enter day: "); scanf ("%d",&day);

  /* get current timeinfo and modify it to the user's choice */
  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  timeinfo->tm_year = year - 1900;
  timeinfo->tm_mon = month - 1;
  timeinfo->tm_mday = day;

  /* call mktime: timeinfo->tm_wday will be set */
  mktime ( timeinfo );

  printf ("That day is a %s./n", weekday[timeinfo->tm_wday]);
  
  return 0;
}

time_t

type
<ctime>

Time type

Type capable of representing times and support arithmetical operations.

This type is returned by the time function and is used as parameter by some other functions of the <ctime> header.

It is almost universally expected to be an integral value representing the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC. This is due to historical reasons, since it corresponds to a unix timestamp, but is widely implemented in C libraries across all platforms.

 

struct tm

type
<ctime>

Time structure

Structure containing a calendar date and time broken down into its components.

The structure contains nine members of type int, which are (in any order):

1
2
3
4
5
6
7
8
9
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;



The meaning of each is:

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

localtime

function
<ctime>
struct tm * localtime ( const time_t * timer );

Convert time_t to tm as local time

Uses the time pointed by timer to fill a tm structure with the values that represent the corresponding local time.

Parameters

timer
Pointer to a time_t value representing a calendar time (see time_t).

 

Return Value

A pointer to a tm structure with the time information filled in.

This structure is statically allocated and shared by the functions gmtime and localtime. Each time either one of these functions is called the content of this structure is overwritten.

Example

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

int main ()
{
  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  printf ( "Current local time and date: %s", asctime (timeinfo) );
  
  return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值