Countrytime.c

3
/*HACKINTOSH RAO \M/ CODES FOR GNU LINUX SYSTEM CALLS USED:1)time_t time(time_t *); 2)struct tm *gmtime(time_t *); DESCRIPTION:AFTER CALLING "time" system call the returned value of type time_t(%ld) is stored first; then the same address of the same value is to be sent to "gmtime" system call; gmtime returns a pointer to the built in structure tm; then the pointer is used to access the members of the structure which will be filled with the information about the time , date ,day , year etc.......... In this program just fetching the time and year using the pointer to the structure is illustrated "gmtime" returns time in GMT and function timeconversion is used to change it to time of a given country*/ #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<time.h> void timeconversion(int *hour,int *min,int diff_hour,int diff_min); /* timeconversion function is used to convert GMT to time of a given country using the difference from GMT ex:INDIA +5.30 GMT*/ void align(void); int main() { time_t time_in_sec;/*built in data type to store very large numbers*/ int hour,min,choice,diff_hour,diff_min; struct tm *timestructure;/*built in structure with members which fill the details like day,time and so on*/ for(;;) { time_in_sec=time(&time_in_sec); timestructure=gmtime(&time_in_sec); hour=timestructure->tm_hour; min=timestructure->tm_min; printf("\n\t1.GMT\n\t2.Indian Standard Time\n\t3.WASHINGTON D.C(U.S.A)\n\t4.LONDON U.K\n\t5.U.A.E\n\t6.AUSTRALIA\n\t7.EXIT\n"); printf("Enter 1 for GMT or Choose the country: "); scanf("%d",&choice); switch(choice) { case 2:diff_hour=5;//IST is +5.30 hours ahead of GMT diff_min=30; timeconversion(&hour,&min,diff_hour,diff_min); align(); printf("Indian standard Time: %02d:%02d:%02d ",hour,min,timestructure->tm_sec); align(); break ; case 1:align(); printf("GMT: %02d:%02d:%02d ",timestructure->tm_hour,timestructure- >tm_min,timestructure->tm_sec); align(); break; case 3:diff_hour=-5;//time in USA is +5.30 hours ahead of GMT diff_min=0;

description

System Program to get the time of 5 countries in GNU/LINUX

Transcript of Countrytime.c

Page 1: Countrytime.c

/*HACKINTOSH RAO \M/ CODES FOR GNU LINUX SYSTEM CALLS USED:1)time_t time(time_t *); 2)struct tm *gmtime(time_t *); DESCRIPTION:AFTER CALLING "time" system call the returned value of type time_t(%ld) is stored first; then the same address of the same value is to be sent to "gmtime" system call; gmtime returns a pointer to the built in structure tm; then the pointer is used to access the members of the structure which will be filled with the information about the time , date ,day , year etc.......... In this program just fetching the time and year using the pointer to the structure is illustrated "gmtime" returns time in GMT and function timeconversion is used to change it to time of a given country*/#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<time.h>void timeconversion(int *hour,int *min,int diff_hour,int diff_min);/* timeconversion function is used to convert GMT to time of a given country using the difference from GMT ex:INDIA +5.30 GMT*/void align(void);int main(){ time_t time_in_sec;/*built in data type to store very large numbers*/ int hour,min,choice,diff_hour,diff_min; struct tm *timestructure;/*built in structure with members which fill the details like day,time and so on*/

for(;;) { time_in_sec=time(&time_in_sec); timestructure=gmtime(&time_in_sec); hour=timestructure->tm_hour; min=timestructure->tm_min; printf("\n\t1.GMT\n\t2.Indian Standard Time\n\t3.WASHINGTON D.C(U.S.A)\n\t4.LONDON U.K\n\t5.U.A.E\n\t6.AUSTRALIA\n\t7.EXIT\n"); printf("Enter 1 for GMT or Choose the country: "); scanf("%d",&choice); switch(choice) {

case 2:diff_hour=5;//IST is +5.30 hours ahead of GMT diff_min=30; timeconversion(&hour,&min,diff_hour,diff_min);

align(); printf("Indian standard Time: %02d:%02d:%02d ",hour,min,timestructure->tm_sec); align(); break ; case 1:align(); printf("GMT: %02d:%02d:%02d ",timestructure->tm_hour,timestructure->tm_min,timestructure->tm_sec); align(); break; case 3:diff_hour=-5;//time in USA is +5.30 hours ahead of GMT diff_min=0;

Page 2: Countrytime.c

timeconversion(&hour,&min,diff_hour,diff_min);

align(); printf("WASHINGTON D.C U.S.A: %02d:%02d:%02d ",hour,min,timestructure->tm_sec); align(); break ; case 4:diff_hour=0;//IST is +5.30 hours ahead of GMT diff_min=0; timeconversion(&hour,&min,diff_hour,diff_min);

align(); printf("UK: %02d:%02d:%02d ",hour,min,timestructure->tm_sec); align(); break ; case 5:diff_hour=4;//IST is +5.30 hours ahead of GMT diff_min=0; timeconversion(&hour,&min,diff_hour,diff_min);

align(); printf("UAE: %02d:%02d:%02d ",hour,min,timestructure->tm_sec); align(); break ; case 6:diff_hour=11;//IST is +5.30 hours ahead of GMT diff_min=0; timeconversion(&hour,&min,diff_hour,diff_min);

align(); printf("MELBOURNE,AUSTRALIA: %02d:%02d:%02d ",hour,min,timestructure->tm_sec); align(); break ; case 7:exit(0);

default:align(); printf("SELECT THE WRIGHT OPTION"); align();

} }}void timeconversion(int *hour,int *min,int diff_hour,int diff_min){

*hour=(*hour+diff_hour)%24;/*so a simple math to convert GMT to time of a given country*/ *min=*min+diff_min; if((*min)>=60) { *hour=(*hour+1)%24; *min=*min-60;

}

}void align()//function just to get the alignments right

Page 3: Countrytime.c

{ printf("\n\n*************************************************************************\n\n");}