0

I have to display date and time separately using gettime() and getdate() in C programming language. The code I have written only displays date and time on same line. I want this code to be done using only core C not in a windows format.The editor I am using is Visual Studio 2008

Below I have post my code which only shows date time on a single line.

#include "stdafx.h"
#include <conio.h>
#include <stdio.h>
#include <time.h>

char *gettime();
int_main(int argc,_TCHAR* argv[])
{
printf("The time is %s\n",gettime());
getch();
return 0;
}
char *gettime()
{

time_t t;
tm b;
time(&t);
return ctime(&t);

}
user-ag
  • 224
  • 3
  • 20

2 Answers2

0

You can use localtime/gmtime

struct tm * timeinfo;
timeinfo = localtime (&t);

And work with tm structure components as you need, also you can use strfrtime format function if you need only print date and time.

Defter
  • 214
  • 1
  • 7
0

Main function is the same for all the different ways:

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

char * gettime();
char * getdate();

int main()
{
    printf("The time is %s\n", gettime());
    printf("The date is %s\n", getdate());

    return 0;
}

One way you could do it is with manipulating the strings coming back from ctime() function. We know they are built in a similar way, the 1st 12 chars are week-day, month, month-day, then comes 8 chars of time, then finally the year. You could create functions like this:

char * gettime()
{
    time_t t;

    //use static so not to save the var in stack, but in the data/bss segment
    //you can also make it a global scope, use dynamic memory allocation, or
    //use other methods as to prevent it from being erased when the function returns.
    static char * time_str; 


    time(&t);
    time_str = ctime(&t) + 11;
    time_str[9] = 0; //null-terminator, eol

    return time_str;
}

char * getdate()
{
    time_t t;

    static char * date_str; 
    static char * year;

    time(&t);
    date_str = ctime(&t) + 4;
    date_str[6] = 0;

    year = date_str + 15;
    year[5] = 0;

    strcat(date_str, year);

    return date_str;
}

The second way to do this is using localtime() function to create a tm-struct, and then extract what you need from it.

char * gettime()
{
    time_t t;
    struct tm *info;
    static char time_str[10]; 

    time(&t);
    info = localtime(&t);
    sprintf(time_str,"%d:%d:%d",(*info).tm_hour, (*info).tm_min, (*info).tm_sec);

    return time_str;
}

char * getdate()
{
    time_t t;
    struct tm *info;
    static char date_str[12]; 

    time(&t);
    info = localtime(&t);
    sprintf(date_str,"%d/%d/%d",(*info).tm_mday, (*info).tm_mon+1, (*info).tm_year+1900);

    return date_str;
}

You can make it a bit more clean using the strftime() function:

char * gettime()
{
    time_t t;
    struct tm *info;
    static char time_str[10]; 

    time(&t);
    info = localtime(&t);
    strftime(time_str, 10, "%S:%M:%H",info);

    return time_str;
}

char * getdate()
{
    time_t t;
    struct tm *info;
    static char date_str[12]; 

    time(&t);
    info = localtime(&t);
    strftime(date_str, 12, "%d:%m:%Y",info);

    return date_str;
}
Maverick Meerkat
  • 5,737
  • 3
  • 47
  • 66