I am trying to get the system date into 3 different varibles Month,Day, and Year but I cant figure out how to get the system date from the OS.

Dani AI

Generated

Quick follow-up for that ties the earlier replies together. The approach you used gets the job done, but a few practical improvements make the code safer and more portable. As noted, use a proper int main(...) and make sure each printf format matches the variable type. Avoid nonstandard getch() (use getchar() or let the program exit normally). As said, the method is portable, but watch out for thread-safety and resolution limits.

If you need a fractional day with six digits of precision, get sub-second time from the OS and combine it with the broken-down clock. On POSIX use clock_gettime (or gettimeofday) to get subseconds, then compute fractional day as (hours3600 + minutes60 + seconds + subseconds)/86400.0. For multithreaded code prefer the reentrant conversion function (localtime_r on POSIX or localtime_s on MSVC). On Windows you can also use GetLocalTime for millisecond precision.

Example (POSIX) showing the safer pattern and how to compute the fractional day:

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

int main(void)
{
    struct timespec ts;
    clock_gettime(CLOCK_REALTIME, &ts); /* POSIX */
    time_t t = ts.tv_sec;
    struct tm tm;
    localtime_r(&t, &tm); /* thread-safe */

    int month = tm.tm_mon + 1;
    int day = tm.tm_mday;
    int year = tm.tm_year + 1900;
    double day_frac = (tm.tm_hour*3600 + tm.tm_min*60 + tm.tm_sec + ts.tv_nsec*1e-9) / 86400.0;

    printf("Month=%d\nDay=%d\nYear=%d\nDay fraction=%.6f\n", month, day, year, day_frac);
    return 0;
}

References and notes: POSIX localtime vs localtime_r and details on broken-down time are documented on the man pages (localtime). For high-resolution clocks see clock_gettime. On Windows, consider GetLocalTime for higher-resolution local time (GetLocalTime).

Recommended Answers

All 6 Replies

There's a header called time.h (ctime in C++) that contains the functions and types you need. Try a google search for details, and if you have trouble with the actual implementation, feel free to ask here for help.

This is what I have for the code to get the system date and put it in 3 variables, is this the best way to do this?

void main()

{
  
    struct tm *Sys_T = NULL;                     
  
    time_t Tval = 0;                            
    Tval = time(NULL);                          
    Sys_T = localtime(&Tval);                   

     Day=Sys_T->tm_mday;
    Month=Sys_T->tm_mon+1;
    Year=1900 + Sys_T->tm_year;

    printf("\nMonth = %d", Month);
    printf("\nDay   = %6.6f", Day);
    printf("\nYear  = %d", Year);
    printf("\n\n\n");

    printf("Hit any key to exit ! \n\n");
    
    getch();

}

>>is this the best way to do this?

Yes, that's how all the programs I've seen does it, although there are other os-specific functions. The code you wrote is completely portable and correct.

I'm clueless as to why the programmer decided to use %6.6f instead of %d in the second printf statement. I'm just as clueless about why the programmer used .

I'm clueless as to why the programmer decided to use %6.6f instead of %d in the second printf statement. I'm just as clueless about why the programmer used .

I did not look at that part of the code -- but you are right.:)

The Month and the Year are integer variables. I used a double varible for the day because later in the program I will be adding the fraction of the day it. I need to have 6 digits of precision later in the program. That was the only reason I used %6.6f for the output in that statement.

For this example you are right I should have used a %d. Thanks for looking at this for me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.