从系统时钟获取时间方式

time函数介绍:
  1. 1、函数名称: localtime
  2. 2、函数名称: asctime
  3. 3、函数名称: ctime
  4. 4、函数名称: difftime
  5. 5、函数名称: gmtime
  6. 6、函数名称: time
  7. 7、函数名称: tzset

time.h是C/C++中的日期和时间头文件。

代码示例

# include <stdio.h>

#include <time.h>

int main(void)

{

time_t timer =time(NULL);

printf("ctime is %s\n",ctime(&timer)); //得到日历时间

return 0;

}

从系统时钟获取时间方式

time_t time(time_t* timer)

得到从标准计时点(一般是1970年1月1日午夜)到当前时间的秒数。

clock_t clock(void)

得到从程序启动到此次函数调用时累计的毫秒数。

time函数介绍

1、函数名称: localtime

函数原型: struct tm *localtime(const time_t *timer)

函数功能: 返回一个以tm结构表达的机器时间信息

函数返回: 以tm结构表达的时间,结构tm定义如下:

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;

};

参数说明: timer-使用time()函数获得的机器时间

所属文件: <time.h>

#include <time.h>

#include <stdio.h>

#include <dos.h>

int main()

{

time_t timer;

struct tm *tblock;

timer=time(NULL);

tblock=localtime(&timer);

printf("Local time is: %s",asctime(tblock));

return 0;

}

2、函数名称: asctime

函数原型: char* asctime(struct tm * ptr)

函数功能: 得到机器时间(日期时间转换为ASCII码)

函数返回: 返回的时间字符串格式为:星期,月,日,小时:分:秒,年

参数说明: 结构指针ptr应通过函数localtime()和gmtime()得到

3、函数名称: ctime

函数原型: char *ctime(long time)

函数功能: 得到日历时间

函数返回: 返回字符串格式:星期,月,日,小时:分:秒,年

参数说明: time-该参数应由函数time获得

所属文件: <time.h>

#include <stdio.h>

#include <time.h>

int main()

{

time_t t;

time(&t);

printf("Today's date and time: %s",ctime(&t));

return 0;

}

4、函数名称: difftime

函数原型: double difftime(time_t time2, time_t time1)

函数功能: 得到两次机器时间差,单位为秒

函数返回: 时间差,单位为秒

参数说明: time1-机器时间一,time2-机器时间二.该参数应使用time函数获得

所属文件: <time.h>

#include <time.h>

#include <stdio.h>

#include <dos.h>

#include <conio.h>

int main()

{

time_t first, second;

clrscr();

first=time(NULL);

delay(2000);

second=time(NULL);

printf("The difference is: %fseconds",difftime(second,first));

getch();

return 0;

}

5、函数名称: gmtime

函数原型: struct tm *gmtime(time_t *time)

函数功能: 得到以结构tm表示的时间信息

函数返回: 以结构tm表示的时间信息指针

参数说明: time-用函数time()得到的时间信息

所属文件: <time.h>

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <dos.h>

char *tzstr="TZ=PST8PDT";

int main()

{

time_t t;

struct tm *gmt, *area;

putenv(tzstr);

tzset();

t=time(NULL);

area=localtime(&t);

printf("Local time is:%s", asctime(area));

gmt=gmtime(&t);

printf("GMT is:%s", asctime(gmt));

return 0;

}

6、函数名称: time

函数原型: time_t time(time_t *timer)

函数功能: 得到机器的日历时间或者设置日历时间

函数返回: 机器日历时间

参数说明: timer=NULL时得到机器日历时间,timer=时间数值时,用于设置日历时间,time_t是一个long类型

所属文件: <time.h>

#include <time.h>

#include <stdio.h>

#include <dos.h>

int main()

{

time_t t;

t=time();

printf("The number of seconds since January 1,1970 is %ld",t);

return 0;

}

7、函数名称: tzset

函数原型: void tzset(void)

函数功能: UNIX兼容函数,用于得到时区,在DOS环境下无用途

函数返回:

参数说明:

所属文件: <time.h>

#include <time.h>

#include <stdlib.h>

#include <stdio.h>

int main()

{

time_t td;

putenv("TZ=PST8PDT");

tzset();

time(&td);

printf("Current time=%s",asctime(localtime(&td)));

return 0;

}

linux系统编程之文件与IO(七):时间函数小结的更多相关文章

  1. linux系统编程之文件与io(一)

    经过了漫长的学习,C语言相关的的基础知识算是告一段落了,这也是尝试用写博客的形式来学习c语言,回过头来看,虽说可能写的内容有些比较简单,但是个人感觉是有史起来学习最踏实的一次,因为里面的每个实验都是自 ...

  2. linux系统编程之文件与io(五)

    上一节中已经学习了文件描述符的复制,复制方法有三种,其中最后一种fcntl还并未使用到,关于这个函数,不光只有复制文件描述符的功能,还有其它一些用法,本节就对其进行一一剖析: fcntl常用操作: 这 ...

  3. linux系统编程之文件与io(四)

    今天继续学习文件与io,主要是学习文件共享及文件.复制文件描述符,有点抽象,主要是概念上的理解,但是很重要,下面一一来分解: 文件共享: 回顾一下,在linux系统调用中,是通过文件描述符来访问文件的 ...

  4. linux系统编程之文件与io(二)

    今天继续学习文件与io,话不多说,开始进入正题: 文件的read和write系统调用: 说明:函数中出现在size_t和ssize_t是针对系统定制的数据类型:     下面以一个实现文件简单拷贝的示 ...

  5. linux系统编程之文件与IO(一):文件描述符、open,close

    什么是IO? 输入/输出是主存和外部设备之间拷贝数据的过程 设备->内存(输入操作) 内存->设备(输出操作) 高级I/O ANSI C提供的标准I/O库称为高级I/O,通常也称为带缓冲的 ...

  6. linux系统编程之文件与IO(四):目录访问相关系统调用

    1. 目录操作相关的系统调用     1.1 mkdir和rmdir系统调用     1.1.1 实例     1.2 chdir, getcwd系统调用     1.2.1 实例     1.3 o ...

  7. linux系统编程之文件与IO(三):利用lseek()创建空洞文件

    一.lseek()系统调用 功能说明: 通过指定相对于开始位置.当前位置或末尾位置的字节数来重定位 curp,这取决于 lseek() 函数中指定的位置 函数原型: #include <sys/ ...

  8. linux系统编程之文件与io(三)

    上次我们利用文件的read和write来实现了简易的cp命令,其中将源文件拷贝到目标文件时,我们给目标文件的权限是写死的,而非根据源文件的权限生成的,如下: 今天就来解决这个问题,来学习获取文件权限相 ...

  9. linux系统编程之文件与IO(八):文件描述符相关操作-dup,dup2,fcntl

    本节目标: 1,文件共享 打开文件内核数据结构 一个进程两次打开同一个文件 两个进程打开同一个文件 2,复制文件描述符(dup.dup2.fcntl) 一,文件共享 1,一个进程打开两个文件内核数据结 ...

随机推荐

  1. IIS中的MIME类型设置

    https://www.cnblogs.com/David-Young/p/5323949.html

  2. Python3 tuple 函数

    Python3 tuple 函数  Python3 内置函数 描述 tuple 函数将列表转换为元组.. 语法 以下是 tuple 的语法: tuple( seq ) 参数 seq -- 要转换为元组 ...

  3. python 的时间与日期

    显示当前日期: import time print time.strftime('%Y-%m-%d %A %X %Z',time.localtime(time.time())) 或者 你也可以用: p ...

  4. 215. Kth Largest Element in an Array(QuickSort)

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  5. 关于weblogic报UnsatisfiedLinkError Native Library xxx.so already loaded

    一.场景 最近写的一个系统,在Tomcat测试完后说要改使用weblogic,于是在服务器上安装了weblogic,捣鼓了半天,一个个问题冒了出来,其中就有个比较麻烦的报错:UnsatisfiedLi ...

  6. [udemy]WebDevelopment_History of The Web

    WWW vs Internet For the begining, Internet was there. it was for the academics among universities Th ...

  7. OJ_单词倒排

    题目描述:对字符串中的所有单词进行倒排. 说明: 1.每个单词是以26个大写或小写英文字母构成,可以用一个“-”中连接线连接单词两部分表示一个单词,但是仅限一个“-”,出现两个“--”则为非构成单词的 ...

  8. visio2003 数据表模型中显示字段类型和注释

    1.在visio菜单上选择 数据库->选项->文档. 2.在常规中找到 [在图表中可见的名称] 选中 两者. 3.在表中找到 [数据类型] 选中 显示物理. 4.在数据表模型中创建字段,并 ...

  9. Spring框架之演示JDBC的模板类

    1. 步骤一:创建数据库的表结构 create database spring_day03; use spring_day03; create table t_account( id int prim ...

  10. CountVectorizer()类解析

      主要可以参考下面几个链接: 1.sklearn文本特征提取 2.使用scikit-learn tfidf计算词语权重 3.sklearn官方中文文档 4.sklearn.feature_extra ...