当前位置:网站首页>[C language] system date & time

[C language] system date & time

2022-06-24 07:51:00 Chenze

write in front

Hello everyone , I'm Chen Ze , I hope after you read it , It can help you , Insufficient, please correct ! Learn and communicate together
2021 Blog star of the year, Internet of things and embedded development TOP5~2021 Blog star Top100~ Alibaba cloud experts ^ Star Blogger ~ Nuggets ⇿InfoQ Creator ~ Zhou Bang 77» General list 1766
This paper is written by Chen Ze original CSDN First episode If you need to reprint, please inform
Personal home page - Chen Ze's blog _CSDN Blog
Welcome to → give the thumbs-up + Collection ️ + Leaving a message. ​
Series column -
YY_ Chen Ze's blog -CSDN Blog
️ We are not on the stage of our choice , Acting is not the script we chose  

Catalog 「 system date & Time 」↓

write in front

⒈ Topic content

⒉ Subject requirements

⒊ Think about the problem

⒋ Their thinking

¹time - Library function

²localtime - Library function

⒌ Program code  

⒍ Code run results

⒈ Topic content

Output the date and time of the system .

There is only one in this code main Function to save each control command in an array , Then apply the loop statement to set up an endless loop . In this loop, the user is asked to enter the command instruction , And judge whether the command entered by the user is the same as the command stored in the array . If they are the same , Then execute the corresponding contents .

⒉ Subject requirements

The user needs to input a command to perform a certain operation , If the command is entered incorrectly , The system will prompt .

When the user enters a command character "0" Will be displayed Help information .

When the user enters a command character "1" Will be displayed system date .

When the user enters a command character "2" Will be displayed system time .

When the user enters a command character "3" Will execute Exit the system .

⒊ Think about the problem

One : It is necessary to ensure that the program can always be executed , Wait for the user's command to prevent the main function from ending .

Two : Get system date and system time .

⒋ Their thinking

Structure struct tm The structure members are as follows ↓

int tm_sec         Seconds after minutes (0-61)  The extra two seconds are used to deal with the second skipping problem 
int tm_min         Minutes after hours (0-59)
int tm_hour        Hours after midnight (0-23)
int tm_mday        The day of the month (0-31)  The day of the month 
int tm_mon         The number of months after one month (0-11)  The month of this year 
int tm_year       1900 Years after , To add 1900 It means that year 
int tm_wday        Days after Sunday (0-6)  The day of the week 
int tm_yday        Days after January 1 (0-365), The day of the year , Leap year 366 Japan 
int tm_isdst       Daylight saving time sign ( Greater than 0 The value of indicates that daylight saving time is valid ,0 Invalid description , Negative numbers indicate that information is not available )

¹time - Library function

describe

C Library functions in language  time_t time(time_t *seconds)  Back from era Epoch(1970-01-01 00:00:00 UTC) Time elapsed since , In seconds . If  seconds  Not empty , The return value is also stored in the variable  seconds  in .

Statement

Here is time() Declaration of functions .

time_t time(time_t *seconds)

notes → The type of storage is the time type, that is time_t Before we get the system date, we need to define a variable of time type .

Parameters

seconds -- This is a pointer of type time_t A pointer to the object of , Used to store seconds Value .

Return value

With time_t Object returns the current calendar time .


²localtime - Library function

describe

C Library function  struct tm *localtime(const time_t *timer)  Use timer To fill in  tm  structure .timer  The value of is decomposed into  tm  structure , And expressed in local time zone .

Statement

Here is localtime() Declaration of functions .

struct tm *localtime(const time_t *timer)

Parameters

timer -- This is a pointer to the calendar time time_t Pointer to value .

Return value

This function returns a pointer to  tm  Pointer to structure , The structure is filled with time information .

⒌ Program code  

system date & Time → The code example is as follows ↓

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<time.h>
#include<Windows.h>
// Structure pointer variable points to (->) Is the member variable in the structure type 
struct tm* fun_Time;
void color(short x)
{
	if (x >= 0 && x <= 15)
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
	else
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
void menu()
{
	color(0);       
	system("cls");	
	color(10);
	printf("|---------   System & Time  --------|\n");
	printf("|-------* 1. system date  *------|\n");
	printf("|-------* 2. system time  *------|\n");
	printf("|-------* 3. Exit the system  *------|\n");
}
void Time()
{
	time_t Time;// Define time type 
	time(&Time);// Get the system date 
	// notes : Pointer variable receive address , Here we define the structure pointer variable, so we only need to use the variable in the structure pointer to store the address .
	//struct tm* fun_Time = localtime(&Time); Empathy 
	fun_Time = localtime(&Time);// Convert to system time 
}
int main(void)
{
	const int date[4] = {0,1,2,3};
	int n = 0;
	printf(" Please enter [0] Get help :");
	while (1)
	{
		color(1);
		scanf_s("%d", &n);
		if (date[0] == n)
			menu();
		else if (date[1] == n)
		{
			Time();// Time attribute 
			printf(" system date :%d-%d-%d\n", 1900 + fun_Time->tm_year, fun_Time->tm_mon + 1, fun_Time->tm_hour);
		}
		else if (date[2] == n)
		{
			Time();// Time attribute 
			printf(" system date :%d:%d:%d\n", fun_Time->tm_hour, fun_Time->tm_min, fun_Time->tm_sec);
		}
		else if (date[3] == n)
		{
			printf(" sign out EXIT!\n");
			break;
		}
		else
			printf(" The command you entered is wrong , Please re-enter :");
	}
	return 0;
}

⒍ Code run results

⒈ pick up information  

⒉ system date ⒊ system time ⒋ sign out EXIT

原网站

版权声明
本文为[Chenze]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240338318277.html