3

I've got a problem. I need to get things like day of year, day of month, month of year etc. I use this code:

#include <stdio.h>
#include <time.h>
int main(void)
{    
    time_t liczba_sekund;
    struct tm strukt;
    time(&liczba_sekund);
    localtime_r(&liczba_sekund, &strukt); 
    printf("today is %d day of year\nmonth is %d, month's day %d\n", strukt.tm_yday+1, strukt.tm_mon+1, strukt.tm_mday); 
    return 0;
}

First thing: why does gcc -std=c99 -pedantic -Wall return this warning:

My input: gcc test_data.c -o test_data.out -std=c99 -pedantic -Wall

Output:

test_data.c: In function ‘main’:

test_data.c:11:3: warning: implicit declaration of function ‘localtime_r’ [-Wimplicit-function-declaration]

Second thing: how to make it work on windows? While trying to compile it using Dev-C, I got this: https://i.stack.imgur.com/F8Qlx.jpg

@@EDIT -------------------- I have found an example for your localtime suggestion:

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

int main ()
{
    time_t time_raw_format;
    struct tm * ptr_time;

    time ( &time_raw_format );
    ptr_time = localtime ( &time_raw_format );
    printf ("Current local time and date: %s", asctime(ptr_time));
    return 0;
}

How can I change this to date format like this: 5.12.2012 or 5-12-2012? And how to get the day of the year?

I would love if the solution worked both on windows and linux.

1

2 Answers 2

8

localtime_r is not part of the C standard. Maybe you were looking for localtime?

localtime_r is really available on many linux systems:

Thread-safe versions asctime_r(), ctime_r(), gmtime_r() and localtime_r() are specified by SUSv2, and available since libc 5.2.5

However, since it isn't part of the standard you cannot use it on Windows.

How can I change this to date format like this: 5.12.2012 or 5-12-2012? And how to get the day of the year?

You have to use strftime instead of asctime:

int main ()
{
    time_t time_raw_format;
    struct tm * ptr_time;
    char buffer[50];

    time ( &time_raw_format );
    ptr_time = localtime ( &time_raw_format );
    if(strftime(buffer,50,"%d.%m.%Y",ptr_time) == 0){
        perror("Couldn't prepare formatted string");
    } else {
        printf ("Current local time and date: %s", buffer);
    }
    return 0;
}
3
  • Actually, in C99, implicit int return type is no longer part of the spec. Some compilers e.g. clang complain about this if it is in pedantic mode.
    – dreamlax
    Dec 6, 2012 at 5:15
  • @dreamlax: Woops. Up to which standard was the implicit return type part of the spec?
    – Zeta
    Dec 6, 2012 at 5:50
  • It was part of C89, but the introduction of the C99 standard mentions the removal of it. The same with implicit int argument types.
    – dreamlax
    Dec 6, 2012 at 7:29
0

localtime on Windows should be thread-safe per: http://msdn.microsoft.com/en-us/library/bf12f0hc%28VS.80%29.aspx

Both the 32-bit and 64-bit versions ofgmtime, mktime, mkgmtime, and localtimeall use a single tm structure per thread for the conversion. Each call to one of these routines destroys the result of the previous call.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.