The C language displays the "current time" applet. The current time Applet
Today, I am idle and have written a small program that shows "current time" in C language.
The applet uses the <time. h> header file:
Time. h is the header file of date and time (C language and C ++)
Time mode:
Time_t time (time_t * timer) returns the number of seconds from the standard time point (generally midnight, January 1, January 1, 1970) to the current time.
Clock_t clock (void) obtains the cumulative number of milliseconds from program startup to this function call.
Function Name: localtime
Function prototype: struct tm * localtime (const time_t * timer)
Function: returns the machine time information expressed in the tm structure.
Function return: The time expressed in the tm structure. The tm structure is defined as follows:
Struct tm {
Int tm_sec;
Int tm_min;
Int tm_hour;
Int tm_mday;
Int tm_mon;
Int tm_year;
Int tm_wday;
Int tm_yday;
Int tm_isdst;
};
Parameter description: timer-machine time obtained using the time () function
Running result
A applet has two minor functions: 1. Countdown 2. display the original code of the "current time" program:
1 # include <time. h> 2 # include <stdio. h> 3 # include <windows. h> 4 5 void jiemianshezhi (); 6 void daojishi (); 7 void dangqianshijian (); 8 9 main () 10 {11 jiemianshezhi (); 12 13} 14 15 // UI settings 16 void jiemianshezhi () 17 {18 int c; 19 20 // set window 21 system ("title tanzi shijian V-1 "); 22 system ("color f4"); 23 system ("mode con: cols = 100 lines = 30 "); 24 25 // set the functional interface 26 printf ("\ t ------- tanzi shijian ------- \ n "); 27 printf ("\ t 1 countdown 2 current time \ n"); 28 printf ("\ t ----------------------------- \ n "); 29 printf ("Enter your choice: \ n"); 30 scanf ("% d", & c); 31 32 switch (c) 33 {34 case 1: daojishi (); break; 35 case 2: dangqianshijian (); break; 36 default: break; 37} 38} 39 40 // countdown 41 void daojishi () 42 {43 int c; 44 45 system ("cls"); 46 printf ("Countdown: \ n"); 47 printf ("------------------- \ n "); 48 printf ("1 30 s | 2 60 s \ n"); 49 printf ("3 120 s | 4 240 s \ n "); 50 printf ("----------------- \ n"); 51 printf ("Enter your choice: \ n"); 52 scanf ("% d", & c ); 53 system ("cls"); 54 55 switch (c) 56 {57 case 1: 58 printf ("30 s countdown begins... \ n "); 59 for (int I = 30; I> = 0; I --) 60 {61 Sleep (1000); 62 printf (" % d s \ n ", i); 63} 64 printf ("\ a"); 65 Sleep (1000); 66 printf ("\ a"); 67 system ("cls "); 68 jiemianshezhi (); 69 break; 70 71 case 2: 72 printf ("60 s countdown begins... \ n "); 73 for (int I = 60; I> = 0; I --) 74 {75 Sleep (1000); 76 printf (" % d s \ n ", i); 77} 78 printf ("\ a"); 79 Sleep (1000); 80 printf ("\ a"); 81 system ("cls "); 82 jiemianshezhi (); 83 break; 84 85 case 3: 86 printf ("120s countdown begins... \ n "); 87 for (int I = 120; I> = 0; I --) 88 {89 Sleep (1000); 90 printf (" % d s \ n ", i); 91} 92 printf ("\ a"); 93 Sleep (1000); 94 printf ("\ a"); 95 system ("cls "); 96 jiemianshezhi (); 97 break; 98 99 case 4:100 printf ("s countdown begins... \ n "); 101 for (int I = 240; I> = 0; I --) 102 {103 Sleep (1000); 104 printf (" % d s \ n ", i); 105} 106 printf ("\ a"); 107 Sleep (1000); 108 printf ("\ a"); 109 system ("cls "); 110 jiemianshezhi (); 111 break; 112} 113 114 115 116 117 118 // current time 119 void dangqianshijian () 120 {121 time_t sec = time (NULL ); 123 struct tm t = * localtime (& sec); 124 printf ("Current Time: % 02d: % 02d: % 02d \ n", t. tm_hour, t. tm_min, t. tm_sec); 125 system ("pause"); 126}