1/* localtime example */
2#include <stdio.h>
3#include <time.h>
4
5int main ()
6{
7 time_t rawtime;
8 struct tm * timeinfo;
9
10 time ( &rawtime );
11 timeinfo = localtime ( &rawtime );
12 printf ( "Current local time and date: %s", asctime (timeinfo) );
13
14 return 0;
15}
16
1#include <time.h>
2#include <stdlib.h>
3#include <stdio.h>
4
5int main(void)
6{
7 time_t current_time;
8 char* c_time_string;
9
10 /* Obtain current time. */
11 current_time = time(NULL);
12
13 if (current_time == ((time_t)-1))
14 {
15 (void) fprintf(stderr, "Failure to obtain the current time.\n");
16 exit(EXIT_FAILURE);
17 }
18
19 /* Convert to local time format. */
20 c_time_string = ctime(¤t_time);
21
22 if (c_time_string == NULL)
23 {
24 (void) fprintf(stderr, "Failure to convert the current time.\n");
25 exit(EXIT_FAILURE);
26 }
27
28 /* Print to stdout. ctime() has already added a terminating newline character. */
29 (void) printf("Current time is %s", c_time_string);
30 exit(EXIT_SUCCESS);
31}
32
1/** A working clock of time and date
2 that using your own computer's
3 local time to run the clock
4 without setting the time and date
5 in the code itself. **/
6
7//C libraries statement
8#include <stdio.h>
9#include <stdlib.h>
10#include <time.h>
11
12//Programming the delay command
13void delay(int secondsNumber)
14{
15 int milliSecondsNumber = 1000 * secondsNumber;
16 clock_t startTime = clock();
17 while(clock() < startTime + milliSecondsNumber);
18}
19
20//Driver program
21int main(void)
22{
23 //Declaring the variable
24 char buff[100];
25
26 //Making the clock run forever
27 for(; ;)
28 {
29 //Seting the clock over your computer local time
30 time_t now = time(0);
31 strftime(buff, 100, " %H:%M.%S \n %d/%m/%Y", localtime(&now));
32
33 //Cleaning the command line and printing the clock
34 system("cls");
35 printf("\n %s\n", buff);
36
37 //Seting a delay of one second between each print
38 delay(1);
39 }
40}
41
42///The code itself without the details:
43
44#include <stdio.h>
45#include <stdlib.h>
46#include <time.h>
47
48void delay(int numOfSec)
49{
50 int numOfMilliSec = 1000 * numOfSec;
51 clock_t startTime = clock();
52 while(clock() < startTime + numOfMilliSec);
53}
54
55int main(void)
56{
57 char buff[100];
58
59 for(; ;)
60 {
61 time_t now = time(0);
62 strftime(buff, 100, " %H:%M.%S \n %d/%m/%Y", localtime(&now));
63
64 system("cls");
65 printf("\n %s\n", buff);
66
67 delay(1);
68 }
69}