A note about the functions of the Linux standard library that handles time

A memorandum about some functions in CTIME (3)

Mold and structure to use

time_t type

A type to put unix time. It seems that it is implemented as an 8-byte integer type in my environment.

tm structure

struct tm {
    int tm_sec;    /* Seconds (0-60) */
    int tm_min;    /* Minutes (0-59) */
    int tm_hour;   /* Hours (0-23) */
    int tm_mday;   /* Day of 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;   /* Day in the year (0-365, 1 Jan = 0) */
    int tm_isdst;  /* Daylight saving time */
};

Function to use

char *ctime(const time_t *timep)

Receives a time_t type value and returns the corresponding local time string representation.

Example

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

int main(int argc, char *argv[]) {
    time_t now = time(NULL);
    printf("%s", ctime(&now));
}

output

Sun May 19 20:23:47 2019

struct tm *gmtime(const time_t *timep)

Takes a time_t type value and returns a pointer to the corresponding UTC tm structure.

Example

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

int main(int argc, char *argv[]) {
	time_t now = time(NULL);
	struct tm *hoge = gmtime(&now);
	printf(
			"%d-%d-%d %d:%d:%d\n",
			hoge->tm_year+1900,
			hoge->tm_mon+1,
			hoge->tm_mday,
			hoge->tm_hour,
			hoge->tm_min,
			hoge->tm_sec
	);
}

output

2019-5-19 14:39:29

struct tm *localtime(const time_t *timep)

Takes a time_t type value and returns a pointer to the corresponding local time tm structure.

Example

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

int main(int argc, char *argv[]) {
	time_t now = time(NULL);
	struct tm *hoge = localtime(&now);
	printf(
			"%d-%d-%d %d:%d:%d\n",
			hoge->tm_year+1900,
			hoge->tm_mon+1,
			hoge->tm_mday,
			hoge->tm_hour,
			hoge->tm_min,
			hoge->tm_sec
	);
}

output

2019-5-19 23:45:2

char *asctime(const struct tm *tm)

Takes a pointer to a tm structure and returns a string representation of the corresponding time.

Example

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

int main(int argc, char *argv[]) {
	time_t now = time(NULL);
	printf("%s", asctime(localtime(&now)));
}

output

Sun May 19 20:23:47 2019

reference

CTIME (3) man page

Recommended Posts

A note about the functions of the Linux standard library that handles time
A note about the python version of python virtualenv
Created a Python library DateTimeRange that handles time ranges
About the process that the Linux kernel handles x86 microcode
A note about mock (Python mock library)
On Linux, the time stamp of a file is a little past.
A function that measures the processing time of a method in python
[python] A note that started to understand the behavior of matplotlib.pyplot
A quick overview of the Linux kernel
A note about doing the Pyramid tutorial
[Python] Note: A self-made function that finds the area of the normal distribution
[Note] Deploying Azure Functions for the first time
The story of Django creating a library that might be a little more useful
A memorandum about the Python tesseract wrapper library
[Note] About the role of underscore "_" in Python
A note about the new style base class
A note on the library implementation that explores hyperparameters using Bayesian optimization in Python
The story of Linux that I want to teach myself half a year ago
Summary of versions of the Python standard library that are now server validated on https
[Linux] A list of Linux commands that beginners should know
A story that reduces the effort of operation / maintenance
[Python] A program that counts the number of valleys
A memorandum about the warning of the pylint output result
Add a list of numpy library functions little by little --a
Make a BOT that shortens the URL of Discord
Make a note of the list of basic Pandas usage
For the first time, I learned about Unix (Linux).
# Function that returns the character code of a string
Note that the latest link of ius has changed
Generate that shape of the bottom of a PET bottle
A story about changing the master name of BlueZ
A story that analyzed the delivery of Nico Nama.
A reminder about the implementation of recommendations in Python
[Python] A program that compares the positions of kangaroos.
Python Note: The mystery of assigning a variable to a variable
A note about __call__
A note about subprocess
A note about mprotect (2)
A library that monitors the life and death of other machines by pinging from Python
About the case that it became a Chinese font after updating with Linux (correction method)
About the contents of wscript when building a D language environment like that with Waf
[Note] A shell script that checks the CPU usage of a specific process in a while loop.
A story about porting the code of "Try and understand how Linux works" to Rust
A tool that automatically turns the gacha of a social game
Avoiding the pitfalls of using a Mac (for Linux users?)
A note on the default behavior of collate_fn in PyTorch
A note about hitting the Facebook API with the Python SDK
Inherit the standard library to find the average value of Queue
Add a list of numpy library functions little by little --c
Grep so that grep does not appear at the time of grep
A memo about the behavior of bowtie2 during multiple hits
[Note] Import of a file in the parent directory in Python
A rough summary of the differences between Windows and Linux
A story about clustering time series data of foreign exchange
A Python script that compares the contents of two directories
Let's summarize the basic functions of TensorFlow by creating a neural network that learns XOR gates
About the ease of Python
A note about KornShell (ksh)
A note about TensorFlow Introduction
About the components of Luigi
A brief summary of Linux