0

Write a program to simulate simple digital clock displaying

HOURS(s) : MINUTES(s): SECOND(s)

  • How to get local system time ?

  • How to split local system time into hours , minutes and seconds ?

Edit 1:

This is what I have tried, but its far from perfect (dosnt even use time.h)

     #include <stdio.h>
     #include <conio.h>
     #include <dos.h> void main() { int h,m,s; h=0; m=0; s=0; while(1) {

     if(s>59) {m=m+1; s=0; } 
      if(m>59) { h=h+1; m=0; } 
      if(h>11) { h=0; m=0; s=0; } 
      delay(1000); 
      s=s+1; 
      clrscr(); 
      printf("\n DIGITAL CLOCK");
      printf("\n HOUR:MINUTE:SECOND"); 
      printf("\n%d:%d:%d",h,m,s); 
}

}
David Robinson
  • 77,383
  • 16
  • 167
  • 187
Failed_Noob
  • 1,347
  • 18
  • 51
  • 67
  • 5
    What have you tried? Do you have some notes that discuss the time functions in the C standard library? – tinman Nov 28 '11 at 11:18
  • @Failed_noob: post the code that you have written so far - that way you get help fixing it – Paul R Nov 28 '11 at 11:28

5 Answers5

7

How to get local system time ? How to split local system time into hours , minutes and seconds ?

time.h

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

int main(void) {

    time_t rawtime;
    struct tm*  time_;

    time(&rawtime);
    time_ = localtime(&rawtime);

    printf("%i:%i:%i %i %i %i\n", time_->tm_hour, time_->tm_min, 
            time_->tm_sec, time_->tm_mday, time_->tm_mon+1,
            time_->tm_year+1900);

    return 0;
}

Why did I do +1900 and +1 you can read here

Jan Vorcak
  • 19,261
  • 14
  • 54
  • 90
  • already repaired, btw you should really look at the documentation I posted, you learn nothing without it, if you are not able to write a function that shows time when you see a function which shows date ... that means you don't want to think, just to copy the result – Jan Vorcak Nov 28 '11 at 11:37
3

from wikipedia: C date and time functions

Adrien Plisson
  • 22,486
  • 6
  • 42
  • 73
1

You can use this simplest code to get the above problem done.

#include <stdio.h>
int main()
{
 printf("%s %s",__TIME__,__DATE__);
 return 0;
 } 
  • I don't like this method because it lacks control. **However** as you say it gets the job done and is easy. – Xantium Oct 09 '17 at 17:36
1

Do a " man localtime() " . Try to figure out the structure being passed to the function and the return values . Till u write yourself you wont learn . Dont copy paste from above and submit ur homework :)

0
#include<stdio.h>
#include<conio.h>
int main(void)

{
    clrscr();
    int h = 0, m = 0, s = 0;
    printf("Digi_Clock \n");
    printf("Enter time (hh : mm : ss) \n");
    scanf(" %d %d %d", &h, &m, &s);

start:;

    for (h; h <= 24; h++) {
        for (m; m <= 60; m++) {
            for (s; s <= 60; s++) {
                clrscr();

                printf(" %d : %d : %d \n", h, m, s);

                for (double i = 0; i <= 6699000; i++) {
                    i++;
                    i--;
                }
            }
            s = 0;
        }
        m = 0;
    }

    goto start;
    getch();
}
HMD
  • 2,202
  • 6
  • 24
  • 37
Ramses
  • 1
  • While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – HMD Dec 24 '18 at 15:49