C/C++中三种方法显示当前系统时间 localtime time.h

#include<ctime>
#include<iostream>
using namespace std;
void Get_Time_V1()
{
    int t=time(NULL);
    int h=(t/3600)%24+8;
    int m=(t/60)%60;
    int s=t%60;
    cout << h << ":" << m << ":" << s << endl;
}
void Get_Time_V2()
{
    time_t t;
    tm *lt;
    t=time(NULL);
    lt=localtime(&t);
    cout << lt->tm_hour << ":" << lt->tm_min << ":" << lt->tm_sec << endl;
}
void Get_Time_V3()
{
  time_t rawtime;
  struct tm * timeinfo;

  time (&rawtime);
  timeinfo = localtime (&rawtime);
  cout<<"Current local time and date: "<<asctime(timeinfo)<<endl;

}
int main()
{
    cout<<"-------version1--------\n";
    Get_Time_V1();
    cout<<"-------version2--------\n";
    Get_Time_V2();
    cout<<"-------version3--------\n";
    Get_Time_V3();
}
/****************************
程序运行结果:
-------version1--------
18:58:23
-------version2--------
18:58:23
-------version3--------
Current local time and date: Mon Jun 03 18:58:23 2013


Process returned 0 (0x0)   execution time : 2.683 s
Press any key to continue.

*****************************/


function
<ctime>

localtime

struct tm * localtime (const time_t * timer);
Convert time_t to tm as local time
Uses the value pointed by timer to fill atm structure with the values that represent the corresponding time, expressed for the local timezone.

Parameters

timer
Pointer to an object of type time_t that contains a time value.
time_t is an alias of a fundamentalarithmetic type capable of representing times as returned by functiontime.

Return Value

A pointer to a tm structure with its members filled with the values that correspond to the local time representation oftimer.

The returned value points to an internal object whose validity or value may be altered by any subsequent call togmtime or localtime.



The ctime(), gmtime() and localtime() functions all take an argument of data typetime_t which represents calendar time. When interpreted as an absolute time value, it represents the number of seconds elapsed since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).

The asctime() and mktime() functions both take an argument representing broken-down time which is a representation separated into year, month,day, and so on.

Broken-down time is stored in the structure tm which is defined in <time.h> as follows:

struct tm {
    int tm_sec;         /* seconds */
    int tm_min;         /* minutes */
    int tm_hour;        /* hours */
    int tm_mday;        /* day of the month */
    int tm_mon;         /* month */
    int tm_year;        /* year */
    int tm_wday;        /* day of the week */
    int tm_yday;        /* day in the year */
    int tm_isdst;       /* daylight saving time */
};
The members of the tm structure are: tm_sec

The number of seconds after the minute, normally in the range 0 to 59, but can be up to 60 to allow for leap seconds.

tm_min

The number of minutes after the hour, in the range 0 to 59.

tm_hour

The number of hours past midnight, in the range 0 to 23.

tm_mday

The day of the month, in the range 1 to 31.

tm_mon

The number of months since January, in the range 0 to 11.

tm_year

The number of years since 1900.

tm_wday

The number of days since Sunday, in the range 0 to 6.

tm_yday

The number of days since January 1, in the range 0 to 365.

tm_isdst

函数名: localtime

简介

功 能: 把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间,而gmtimes函数转换后的时间没有经过时区变换,是UTC时间 。
说明:此函数获得的tm结构体的时间是日历时间。
用 法: struct tm *localtime(const time_t *clock);
返回值:返回指向tm 结构体指针.tm结构体是time.h中定义的用于分别存储时间的各个量(年月日等)的结构体.


localtime(取得当地目前时间和日期)
相关函数 time, asctime, ctime, gmtime
表头文件 #include<time.h>
定义函数 struct tm *localtime(const time_t * timep);
函数说明 localtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。 此函数返回的时间日期已经转换成当地时区。
返回值 返回结构tm代表目前的当地时间。


  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值