Use struct tm to print a specific date and strftime

Therefore, I need to specifically use struct tm to print my birthday, which I successfully completed. However, I also need to use strftime () to print in different formats. That I ran into my problem since strftime () only recognizes pointer parameters.

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

int main(){

    struct tm str_bday;
    time_t time_bday;
    char buffer[15];

    str_bday.tm_year = 1994 - 1900 ;
    str_bday.tm_mon = 7 - 1;
    str_bday.tm_mday = 30;
    str_bday.tm_hour = 12;
    str_bday.tm_min = 53;
    time_bday = mktime(&str_bday);
    if(time_bday == (time_t)-1)
        fprintf(stdout,"error\n");
    else
        {
        fprintf(stdout,"My birthday in second is: %ld \n",time_bday);
        fprintf(stdout,"My birthday is: %s\n", ctime(&time_bday));//Wed July 22 12:53:00 1998
        strftime(buffer,15,"%d/%m/%Y",time_bday);
        fprintf(stdout,"My birthday in D/M/Y format is %s",buffer);
        }
    return 0;
}

Errors:

Error:  passing argument 4 of ‘strftime’ makes pointer from integer without a cast

    expected ‘const struct tm * restrict’ but argument is of type ‘time_t’

Can someone tell me how to fix this?

EDIT: changing the_b_b time to str_bday works! But now the program displays a random time and date every time I run it.

EDIT: instead of fprintf () after strftime (), I used puts (buffer) and it worked fine. Also, changing buffer [15] to buffer [30], since I have hours, minutes, and seconds.

+4
source share
2

strftime, , const struct tm* :

size_t strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr);

&str_bday time_bday .

struct tm , , , , . struct tm str_bday = {0}, .

+4

mktime(&str_bday); 1 of struct tm time_t.

0 .

// struct tm str_bday;
struct tm str_bday = { 0 };

OP- , tm_sec, , , .

. OP- tm_isdst struct tm str_bday = { 0 },

str_bday.tm_isdst = 0; // Set time stamp to **standard** time.

, OP - , , .

// add
str_bday.tm_isdst = -1; // Let mktime() determine DST setting
// or if one knowns it is daylight time.
str_bday.tm_isdst = 1; // Set time stamp to **daylight** time.
// or if one knowns it is standard time.
str_bday.tm_isdst = 0; // Set time stamp to **standard** time.

ctime(&time_bday)

// My birthday is: Sat Jul 30 13:53:00 1994
My birthday is: Sat Jul 30 12:53:00 1994

#include <locale.h>
#include <time.h>
int main() {

  struct tm str_bday = { 0 };
  time_t time_bday;
  char buffer[15];

  str_bday.tm_year = 1994 - 1900;
  str_bday.tm_mon = 7 - 1;
  str_bday.tm_mday = 30;
  str_bday.tm_hour = 12;
  str_bday.tm_min = 53;
  str_bday.tm_isdst = -1;
  time_bday = mktime(&str_bday);

  strftime(buffer, sizeof buffer, "%d/%m/%Y", &str_bday);
  if (time_bday == (time_t) -1) {
    fprintf(stdout, "error\n");
  } else {
    // Do not assume `long`.  Better to cast to a wide type. 
    fprintf(stdout, "My birthday in second is: %lld \n", (long long) time_bday);
    fprintf(stdout, "My birthday is: %s", ctime(&time_bday));
    // strftime(buffer,15,"%d/%m/%Y",time_bday);
    strftime(buffer, sizeof buffer, "%d/%m/%Y", &str_bday);
    fprintf(stdout, "My birthday in D/M/Y format is %s\n", buffer);
  }
  return 0;
}

1 tm_wday tm_yday , .

0

Source: https://habr.com/ru/post/1696207/


All Articles