[LINUX] I tried to illustrate the time and time in C language

The C language has a time function that I personally find awkward. Even if it is difficult to handle, I will obey quietly because I do not have the skill to make more things, but I will write the connection because it was hard to remember. ..

Maybe we will increase the Windows edition, clock, timer, etc.?

Look at Linux here

tl;dr(too long didn't read) time_correlation_diagram.png

C language standard: time_t type

A type that indicates the number of seconds that have elapsed since 0:00:00 (UTC) on January 1, 1970. In other words, I don't know the date or time as it is </ font>.

Structure definition


#include <time.h>
time_t time(time_t *t);

C language standard time acquisition function. There are two ways to use it.

time()Notation


time_t current_time;
time(&current_time); //If you give a buffer, it will be put in there
or
time_t current_time = time(NULL); //Return value is OK

C language standard: structure struct tm type

Use when you want more detailed date information. It was a big success when I put the date on the log. You can also convert struct timespec and struct tm within the Linux Kernel. ..

Definition


struct tm {
    int tm_sec;        /*Seconds(0-60) */
    int tm_min;        /*Minutes(0-59) */
    int tm_hour;       /*time(0-23) */
    int tm_mday;       /*Date in the month(1-31) */
    int tm_mon;        /*Month(0-11) */
    int tm_year;       /*Year- 1900 */
    int tm_wday;       /*Day of the week(0-6,Sunday= 0) */
    int tm_yday;       /*Total date within the year(0-365,January 1= 0) */
    int tm_isdst;      /*Daylight saving time*/
};

time_t type → character string type

It is used when you want to express the time in characters.

Times of Day->Character notation


#include <time.h>
char *ctime(const time_t *timep);  // Thread unsafe
char *ctime_r(const time_t *timep, char *buf); // Thread safe

-** C language standard : ctime () Since it returns a pointer to the internal memory, it has a thread unsafe problem that the recipient changes when the internal memory is rewritten. - POSIX only **: ctime_r () Since the output buffer is requested from the user side, the value is guaranteed even if it is rewritten internally.

time_t type → struct tm type

It's a hassle to convert from the time_t type, and am I the only one who thinks it's okay to have a function that returns the struct tm of the current time? I'm sure I just don't know.

Conversion function


#include <time.h>
// GMT
struct tm *gmtime(const time_t *timep); // Thread unsafe
struct tm *gmtime_r(const time_t *timep, struct tm *result); // Thread safe
//Local time
struct tm *localtime(const time_t *timep); // Thread unsafe
struct tm *localtime_r(const time_t *timep, struct tm *result);  // Thread safe

struct tm type → string type

Conversion function


#include <stdio.h>
size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);

If you want to output it, you may put it in the printf function separately.

Recommended Posts

I tried to illustrate the time and time in C language
I tried to display the time and today's weather w
I tried to describe the traffic in real time with WebSocket
[C language] I want to generate random numbers in the specified range
I tried to extract and illustrate the stage of the story using COTOHA
I implemented the VGG16 model in Keras and tried to identify CIFAR10
I tried to graph the packages installed in Python
I tried using google test and CMake in C
I tried to identify the language using CNN + Melspectogram
Go language to see and remember Part 7 C language in GO language
[Linux] I learned LPIC lv1 in 10 days and tried to understand the mechanism of Linux.
I tried to summarize the code often used in Pandas
I tried programming the chi-square test in Python and Java.
I tried to summarize the commands often used in business
I tried to implement the mail sending function in Python
I tried to enumerate the differences between java and python
I tried to move the ball
I tried to estimate the interval.
I tried to automatically post to ChatWork at the time of deployment with fabric and ChatWork Api
[Natural language processing] I tried to visualize the hot topics this week in the Slack community
I also tried to imitate the function monad and State monad with a generator in Python
[Natural language processing] I tried to visualize the remarks of each member in the Slack community
Determine the date and time format in Python and convert to Unixtime
I want to record the execution time and keep a log.
Java programmer tried to touch Go language (for the time being)
[Introduction to Python] I compared the naming conventions of C # and Python.
I tried to process the image in "sketch style" with OpenCV
I tried to display the video playback time (OpenCV: Python version)
I tried to process the image in "pencil style" with OpenCV
I tried to implement PLSA in Python
I tried to summarize the umask command
I tried to implement permutation in Python
I tried to recognize the wake word
I tried to implement PLSA in Python 2
I tried to summarize the graphical modeling.
I tried to implement ADALINE in Python
I wrote the selection sort in C
I tried to estimate the pi stochastically
I tried to touch the COTOHA API
I tried to implement PPO in Python
I tried to create serverless batch processing for the first time with DynamoDB and Step Functions
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
[RHEL7 / CentOS7] I put in the log monitoring tool swatch and tried to notify by email
I tried fitting the exponential function and logistics function to the number of COVID-19 positive patients in Tokyo
[Python] I tried to summarize the set type (set) in an easy-to-understand manner.
I tried to summarize until I quit the bank and became an engineer
I tried moving the image to the specified folder by right-clicking and left-clicking
I tried to visualize the age group and rate distribution of Atcoder
I tried to express sadness and joy with the stable marriage problem.
How to get the date and time difference in seconds with python
I tried to solve the 2020 version of 100 language processing [Chapter 3: Regular expressions 25-29]
I tried to classify guitar chords in real time using machine learning
I tried to learn the angle from sin and cos with chainer
The file name was bad in Python and I was addicted to import
I tried to verify and analyze the acceleration of Python by Cython
For the first time in Numpy, I will update it from time to time
I tried to display the altitude value of DTM in a graph
I tried to train the RWA (Recurrent Weighted Average) model in Keras
I tried to control the network bandwidth and delay with the tc command
How to use the C library in Python
I tried to program bubble sort by language