All about programming in GNU/LINUX

System program in GNU/LINUX to get time of 5 countries using gmtime and time system call

//download link for the file:http://www.slideshare.net/hackintoshrao/countrytimec

/*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;
               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
{
    printf(“\n\n*************************************************************************\n\n”);
}
Image

Advertisements

3 responses

  1. Good one. But why is time_in_sec=time(&time_in_sec) required ?

    January 6, 2012 at 12:06 am

    • Strictly speaking its better to follow the prototype of the function which is “time_t time(time_t *)”, so rather than to leave out the return value i believe its better to assign it , ya in this case its fine even if we dont catch the return value , but i believe its a very good practice to do so .

      January 6, 2012 at 12:53 am

    • i dont think i answered to ur point in my previous reply……..The argument to gmtime() must to contain variable of type time_t with some creditable data containing no.of.seconds , that could happen only after a call to time() , which fills in the data into the variable which can be then fed into gmtime()

      January 6, 2012 at 5:33 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s