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

How to install Nginx and configure multiple domain names

Nginx Installation CentOS 6.x yum does not have n...

Solve the problem of MySQL using not in to include null values

Notice! ! ! select * from user where uid not in (...

Zookeeper stand-alone environment and cluster environment construction

1. Single machine environment construction# 1.1 D...

Example of how to set up a third-level domain name in nginx

Problem Description By configuring nginx, you can...

Circular progress bar implemented with CSS

Achieve results Implementation Code html <div ...

WeChat applet records user movement trajectory

Table of contents Add Configuration json configur...

Docker builds kubectl image implementation steps

If the program service is deployed using k8s inte...

A brief discussion on several advantages of Vue3

Table of contents 1. Source code 1.1 Monorepo 1.2...

The difference between KEY, PRIMARY KEY, UNIQUE KEY, and INDEX in MySQL

The problem raised in the title can be broken dow...

MySQL installation and configuration tutorial for Mac

This article shares the MySQL installation tutori...

Web design skills: iframe adaptive height problem

Maybe some people have not come across this issue ...