How to get the current time using time(NULL) function and localtime() in Linux

How to get the current time using time(NULL) function and localtime() in Linux

time(); function

Function prototype: time_t time(time_t *timer)
Function purpose: Get the machine's calendar time or set the calendar time Header file: time.h
Input parameters: When timer=NULL, the machine calendar time is obtained; when time=time value, it is used to set the calendar time;
time_t is a long type

/* time - Get the current calendar time of the computer system
 * Functions that process date and time are calculated based on the return value of this function*
 * Function prototype:
 * #include <time.h>
 * 
 * time_t time(time_t *calptr);
 *
 * Return value:
 * Success: Number of seconds since 1970-1-1, 00:00:00
 *
 * use:
 * time_t now;
 * 
 * time(&now); // == now = time(NULL);
 */

localtime(); function

Function prototype: struct tm *localtime(const time_t *timer)
Function purpose: Returns machine time information expressed in a tm structure Header file: time.h
Input parameters: timer: machine time obtained using the time() function;

/*
 * localtime - converts a time value to local time, taking into account the local time zone and daylight saving time flags*
 * Function declaration:
 * #include <time.h>
 *
 * struct tm * localtime(const time_t *timer);
 *
 */
//The definition of structure tm is: 
 struct tm 
 { 
   int tm_sec; /* Seconds: 0-59 (K&R says 0-61?) */ 
   int tm_min; /* Minutes: 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 Jan. 1: 0-365 */ 
   int tm_isdst; /* +1 Daylight Savings Time, 0 No DST, 
    * -1 don't know */ 
 };

Since time_t is actually a long integer, what should we do if the number of seconds from a time point (usually 00:00:00 on January 1, 1970) to that time (i.e. calendar time) exceeds the range of numbers that can be represented by a long integer? For the value of the time_t data type, the time it represents cannot be later than 19:14:07 on January 18, 2038. In order to represent longer time, some compiler manufacturers introduced 64-bit or even longer integers to save calendar time. For example, Microsoft uses the __time64_t data type to save calendar time in Visual C++, and obtains calendar time through the _time64() function (instead of using the 32-bit word time() function). In this way, the data type can be used to save the time before 00:00:00 on January 1, 3001 (excluding this time point).

/*
* time();
* @author 李政<[email protected]>
*/

#include <time.h> 
#include <stdio.h> 

int main(int argc, char* argv[])
{ 
  struct tm *tp; 
  time_t t = time(NULL); 
  tp = localtime(&t);

  printf("%d/%d/%d\n",tp->tm_mon+1,tp->tm_mday,tp->tm_year+1900); 
  printf("%d:%d:%d\n",tp->tm_hour,tp->tm_min,tp->tm_sec); 

  return 0;
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Example explanation of alarm function in Linux
  • PHP executes 6 Linux command function code examples
  • Detailed explanation of the use of stat function and stat command in Linux
  • How to add a timeout to a Python function on Linux/Mac
  • Linux unlink function and how to delete files
  • Detailed explanation of the use of Linux lseek function
  • A brief analysis of the function calling process under the ARM architecture

<<:  JavaScript mobile H5 image generation solution explanation

>>:  Solution to ERROR 1054 (42S22) when changing password in MySQL 5.7

Recommend

jQuery simulates picker to achieve sliding selection effect

This article shares the specific code of jQuery t...

MySQL database JDBC programming (Java connects to MySQL)

Table of contents 1. Basic conditions for databas...

Detailed explanation of how to use Vue self-nested tree components

This article shares with you how to use the Vue s...

How to solve nginx 503 Service Temporarily Unavailable

Recently, after refreshing the website, 503 Servi...

WeChat applet implements calculator function

This article shares the specific code for the WeC...

Several techniques for playing sounds with CSS

CSS is the realm of style, layout, and presentati...

...

MySQL 5.7 cluster configuration steps

Table of contents 1. Modify the my.cnf file of se...

Div exceeds hidden text and hides the CSS code beyond the div part

Before hiding: After hiding: CSS: Copy code The co...

Implementation of grayscale release with Nginx and Lua

Install memcached yum install -y memcached #Start...

MYSQL master-slave replication knowledge points summary

An optimization solution when a single MYSQL serv...

Detailed explanation of Nginx process management and reloading principles

Process structure diagram Nginx is a multi-proces...

Parsing MySQL binlog

Table of contents 1. Introduction to binlog 2. Bi...

JS 9 Promise Interview Questions

Table of contents 1. Multiple .catch 2. Multiple ...