


Linux has a function to get the time. Commonly used time functions in Linux: 1. time() function to obtain the current time; 2. "localtime_r"() and localtime() functions to obtain the current local time and date; 3. gettimeofday() function to obtain the current time time.
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
Linux function to obtain time
Introduction to commonly used time functions:
- ##time() function obtains the current time
#include <time.h> time_t time(time_t *t); /* 此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。 * 如果t 并非空指针的话,此函数也会将返回值存到t指针所指的内存。 */</time.h>
Example code:#include <stdio.h> #include <string.h> #include <time.h> int main() { time_t sec; sec = time((time_t *)NULL); printf("%d\n", (int)sec); return 0; }</time.h></string.h></stdio.h>
Running results: - ## localtime_r() localtime() obtains the current local time and Date
#include <time.h> struct tm *localtime(const time_t *timep); struct tm *localtime_r(const time_t *timep, struct tm *result); /*该函数将有time函数获取的值timep转换真实世界所使用的时间日期表示方法,然后将结果由结构tm返回*/ /**需要注意的是localtime函数可以将时间转换本地时间,但是localtime函数不是线程安全的。 多线程应用里面,应该用localtime_r函数替代localtime函数,因为localtime_r是线程安全的**/</time.h>
Instance code:#include <stdio.h> #include <string.h> #include <time.h> int main() { time_t tmp; struct tm *timp; time(&tmp); timp = localtime(&tmp); printf("%d-%d-%d %d:%d:%d\n", (1900 + timp->tm_year), ( 1 + timp->tm_mon), timp->tm_mday, (timp->tm_hour), timp->tm_min, timp->tm_sec); return 0; }</time.h></string.h></stdio.h>
Running result:
## asctime( ) asctime_r() returns the time and date in string format -
#include <time.h> struct tm *gmtime(const time_t *timep); struct tm *gmtime_r(const time_t *timep, struct tm *result); char *asctime(const struct tm *tm); char *asctime_r(const struct tm *tm, char *buf); /* *gmtime是把日期和时间转换为格林威治(GMT)时间的函数。 *将参数time 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回 *asctime 将时间以换为字符串字符串格式返回 */</time.h>
Example code:#include <stdio.h> #include <string.h> #include <time.h> int main() { time_t timp; time(&timp); printf("%s\n", asctime(gmtime(&timp))); return 0; }</time.h></string.h></stdio.h>
Running results: (The string obtained by asctime itself has a newline character
)
## ctime(), ctime_r() represents the time and date in string format -
#include <time.h> char *ctime(const time_t *timep); char *ctime_r(const time_t *timep, char *buf); /* *ctime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法, *然后将结果以字符串形态返回 */</time.h>
Example code:
ctimeThe obtained string itself has a newline character#include <stdio.h> #include <string.h> #include <time.h> int main(void) { time_t tmp; time(&tmp); printf("%s\n", ctime(&tmp)); return 0; }</time.h></string.h></stdio.h>
Run result: ()
mktime() Converts the value of the time structure struct tm into elapsed seconds -
#include <time.h> time_t mktime(struct tm *tm); /* *将时间结构体struct tm的值转化为经过的秒数 */</time.h>
Example code:
#include <stdio.h> #include <string.h> #include <time.h> int main() { time_t tmp; struct tm *timp; time(&tmp); timp = localtime(&tmp); tmp = mktime(timp); printf("%d\n", (int)tmp); return 0; }</time.h></string.h></stdio.h>
Run result:## gettimeofday() Gets the current time
- Example code:
#include <stdio.h> #include <string.h> #include <sys> int main() { struct timeval tv; gettimeofday(&tv, NULL); printf("tv_sec = %d\n", (int)tv.tv_sec); printf("tv_usec = %d\n", (int)tv.tv_usec); return 0; }</sys></string.h></stdio.h>
Running results:Recommended learning:
Linux video tutorial
#include <sys> int gettimeofday(struct timeval *tv, struct timezone *tz); struct timeval { time_t tv_sec; /* seconds (秒)*/ suseconds_t tv_usec; /* microseconds(微秒) */ }; struct timezone { int tz_minuteswest; /* minutes west of Greenwich */ int tz_dsttime; /* type of DST correction */ }; /* *gettimeofday函数获取当前时间存于tv结构体中,相应的时区信息则存于tz结构体中 *需要注意的是tz是依赖于系统,不同的系统可能存在获取不到的可能,因此通常设置为NULL */</sys>
The above is the detailed content of Is there a function to get time in linux?. For more information, please follow other related articles on the PHP Chinese website!

linux设备节点是应用程序和设备驱动程序沟通的一个桥梁;设备节点被创建在“/dev”,是连接内核与用户层的枢纽,相当于硬盘的inode一样的东西,记录了硬件设备的位置和信息。设备节点使用户可以与内核进行硬件的沟通,读写设备以及其他的操作。

区别:1、open是UNIX系统调用函数,而fopen是ANSIC标准中的C语言库函数;2、open的移植性没fopen好;3、fopen只能操纵普通正规文件,而open可以操作普通文件、网络套接字等;4、open无缓冲,fopen有缓冲。

端口映射又称端口转发,是指将外部主机的IP地址的端口映射到Intranet中的一台计算机,当用户访问外网IP的这个端口时,服务器自动将请求映射到对应局域网内部的机器上;可以通过使用动态或固定的公共网络IP路由ADSL宽带路由器来实现。

在linux中,eof是自定义终止符,是“END Of File”的缩写;因为是自定义的终止符,所以eof就不是固定的,可以随意的设置别名,linux中按“ctrl+d”就代表eof,eof一般会配合cat命令用于多行文本输出,指文件末尾。

在linux中,可以利用“rpm -qa pcre”命令判断pcre是否安装;rpm命令专门用于管理各项套件,使用该命令后,若结果中出现pcre的版本信息,则表示pcre已经安装,若没有出现版本信息,则表示没有安装pcre。

linux查询mac地址的方法:1、打开系统,在桌面中点击鼠标右键,选择“打开终端”;2、在终端中,执行“ifconfig”命令,查看输出结果,在输出信息第四行中紧跟“ether”单词后的字符串就是mac地址。

手机远程linux工具有:1、JuiceSSH,是一款功能强大的安卓SSH客户端应用,可直接对linux服务进行管理;2、Termius,可以利用手机来连接Linux服务器;3、Termux,一个强大的远程终端工具;4、向日葵远程控制等等。

在linux中,rpc是远程过程调用的意思,是Reomote Procedure Call的缩写,特指一种隐藏了过程调用时实际通信细节的IPC方法;linux中通过RPC可以充分利用非共享内存的多处理器环境,提高系统资源的利用率。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools