C/C++时间函数的使用
一、获取日历时间
time_t是定义在time.h中的一个类型,表示一个日历时间,也就是从1970年1月1日0时0分0秒到此时的秒数,原型是:
typedef long time_t; /* time value */
可以看出time_t其实是一个长整型,由于长整型能表示的数值有限,因此它能表示的最迟时间是2038年1月18日19时14分07秒。
函数time可以获取当前日历时间时间,time的定义:
time_t time(time_t *)
#include <iostream>
#include <time.h>
using namespace std;
int main(void)
{
time_t nowtime;
nowtime = time(NULL); //获取当前时间
cout << nowtime << endl;
return 0;
}
输出结果:1268575163
二、获取本地时间
time_t只是一个长整型,不符合我们的使用习惯,需要转换成本地时间,就要用到tm结构,time.h中结构tm的原型是:
struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
可以看出,这个机构定义了年、月、日、时、分、秒、星期、当年中的某一天、夏令时。可以用这个结构很方便的显示时间。
用localtime获取当前系统时间,该函数将一个time_t时间转换成tm结构表示的时间
相关文档:
1、 一些头文件的作用:
<assert.h>:ANSI C。提供断言,assert(表达式)
<glib.h>:GCC。GTK,GNOME的基础库,提供很多有用的函数,如有数据结构操作函数。使用glib只需要包含<glib.h>
<dirent.h>:GCC。文件夹操作函数。struct dirent,struct DIR,opendir(),closedir(),readdir(),readdi ......
在测试
ConsoleRunner
的过程中发现一个有意思的现象,一段很简单的
C
程序:
#include <stdio.h>
#include <windows.h>
int main() {
int i = 0;
for (;;) {
printf("%d\n", i++); ......
根据ACM官方网站3月9日发布的消息,2009年图灵奖授予了现代PC之父Charles Thacker,以表彰他对第一台现代个人计算机Xerox PARC Alto的先驱性设计与实现,还有在局域网(包括以太网)、多处理器工作站、窥探高速缓存一致性协议和平板PC等方面的重大发明和贡献(For the pioneering design andРrealization of the fi ......
这两天学习C++学累了,看了很多的网站论坛,突然感觉迷茫了,c/c++到底能做什么呢?现在JAVA很热,也很好找工作,而且学起来还听说很容易入门。不用学计算机基础类的知识,可C/C++就不同了,只学编程还不行,还得学什么数据结构,算法,计算机原理,操作系统,汇编语言,编程用具等等,需要好多,感觉一 ......
为了用vc写一个最简单的 socket 程序,花费了一个下午的时间,过程中出现的错误有:
'SOCKET' : illegal use of this type as an expression
syntax error : missing ';' before 'type'
syntax error : identifier 'InitWinsock' --> bool InitWinsock( ......