#include<stdio.h>
#include<graphics.h>
#include<time.h>
int gd=DETECT,gm,x,y;
void main()
{
time_t now;
struct tm *timeinfo;
initgraph(&gd,&gm,"..\\BGI\\");
x=getmaxx()/2-80;
y=getmaxy()/2-20;
while(!kbhit())
{
time(&now);
timeinfo=localtime(&now);
printf("%2d : %2d : %2d",timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
delay(1000);
cleardevice();
}
getch();
}
Search This Blog
Showing posts with label time. Show all posts
Showing posts with label time. Show all posts
Digital Clock C Program
This is a c program to display a digital clock showing correct time. It reads time at 1 second interval and displays it on the screen. To know how to get date and time in C, see this post: How to Get Time and Date in C - C Program to Read and Display Time and Date
How to Get Time and Date in C - C Program to Read and Display Time and Date
In this post, we will discuss how to read system time (current time) and date in c. To get system time and date, we use the time.h header file. It contains functions related to time and date. The function time() reads the current time and stores in the variable given as parameter. For example, if we call time(now), then the variable now is assigned the current date and time value. This parameter should be of type time_t. A sample code is:
time_t now; time ( &now );
Note that we should pass the reference to a variable of type time_t. But, the value stored by the function time() contains all information including date and time. So, the value appears to be an unintelligible random number. There fore, to extract hours, minutes, seconds, year, month, day of month etc from this value, we should convert into an object of structure struct tm. The function localtime() converts the value for us. This conversion to date and time can be done as follows:
C Program to Display and Set System Time and Date Using 8086 Interrupts
This is a C Program to Display and Set System Time and Date Using 8086 Interrupts. Interrupt number 21 (in hexadecimal) is used to:
![]() |
| Output of c Program to get and set system time and date |
- Get System time
- Set System time
- Get System Date
- Set System Date
To Get system date:
- Call interrupt 0x21 (hexadecimal)
- AH (higher byte of accumulator) should be made equal to 2A (hex) before calling interrupt
Output:
- day in DL
- Month in DH
- Year in CX
To set System Date:
- Call interrupt 0x21 (hexadecimal)
- AH (higher byte of accumulator) should be made equal to 2B (hex) before calling interrupt
- Before Calling interrupt, store:- Year in CX
- Month in DH
- Day in DL
To Get system time:
- Call interrupt 0x21 (hexadecimal)
- AH (higher byte of accumulator) should be made equal to 2C (hex) before calling interrupt
Output:
- Hours in CH
- Minutes in CL
- Seconds in DH
- 1/100 th of a second in DL
To set System Time:
- Call interrupt 0x21 (hexadecimal)
- AH (higher byte of accumulator) should be made equal to 2D (hex) before calling interrupt
- Before Calling interrupt, store:
- Hour in CH
- Minutes in CL
- Seconds in DH
- 1/100 th of second in DL
#include<stdio.h>
#include<dos.h>
#include<conio.h>
void main()
{
int day,month,year,tflag=0,dflag=0,hr,min,sec;
union REGS inreg,outreg;
DISDATE:printf("\nCurrent date:\n");
inreg.h.ah=0x2a;
int86(0x21,&inreg,&outreg);
printf("%d-%d-%d ",outreg.h.dl,outreg.h.dh,outreg.x.cx);
switch(outreg.h.al)
{
case 0:printf("Sunday");break;
case 1:printf("Monday");break;
case 2:printf("Tueday");break;
case 3:printf("Wednesday");break;
case 4:printf("Thursday");break;
case 5:printf("Friday");break;
case 6:printf("Sunday");break;
}
if(dflag==1)
goto SETTIME;
DISTIME:printf("\nCurrent Time:\n");
inreg.h.ah=0x2c;
int86(0x21,&inreg,&outreg);
printf("%d:%d:%d.%d ",outreg.h.ch,outreg.h.cl,outreg.h.dh,outreg.h.dl);
if(tflag==1)
goto END;
SETDATE:printf("\nSet Date.\nDay,Month and Year\n");
scanf("%d%d%d",&day,&month,&year);
inreg.h.ah=0x2b;
inreg.x.cx=year;
inreg.h.dh=month;
inreg.h.dl=day;
int86(0x21,&inreg,&outreg);
if(outreg.h.al==0x00)
{
dflag=1;
goto DISDATE;
}
else
{
printf("\nInvalid entry");
getch();
exit(0);
}
SETTIME:printf("\nSet Time. Enter Time.\nHour,Minutes and Seconds\n");
scanf("%d%d%d",&hr,&min,&sec);
inreg.h.ah=0x2d;
inreg.h.ch=hr;
inreg.h.dh=sec;
inreg.h.cl=min;
inreg.h.dl=0;
int86(0x21,&inreg,&outreg);
if(outreg.h.al==0x00)
{
tflag=1;
goto DISTIME;
}
else
{
printf("\nInvalid entry");
getch();
exit(0);
}
END: getch();
}
Subscribe to:
Posts (Atom)
