0

These two links have been helpful thus far:

But I cannot quite get there yet. Using the info from the two links above, I can get their sample to work nicely:

int main(int argc, char *argv[])
{
   struct tm tm;
   char str_date[256];

   strptime("01/26/12", "%m/%d/%y", &tm);
   strftime(str_date, sizeof(str_date), "%A, %d %B %Y", &tm);
   printf("%s\n", str_date);

   return 0;
}

This returns "Thursday, 26 January 2012" on my console which is correct.

All good so far.

However, everything I have tried with the date in yyyy.mm.dd format in strptime gives me this on the console "?, 00 ? 2019"

int main(int argc, char *argv[])
{
   struct tm tm;
   char str_date[256];

   strptime("1912.02.14", "%y.%m.%d", &tm);
   strftime(str_date, sizeof(str_date), "%A, %d %B %Y", &tm);
   printf("%s\n", str_date);

   return 0;
}

If I can get strptime to work correctly, I can juggle around the format specifiers in strftime to get the output I want.

ANSWER: Thanks to BladeMight's help, this code works very well for me:

#include <stdio.h>
#include <time.h>
char *strptime(const char *buf, const char *format, struct tm *tm);

int main(int argc, char *argv[])
{
   struct tm tm;
   char str_date[256];

   strptime("1912.02.14", "%Y.%m.%d", &tm);
   strftime(str_date, sizeof(str_date), "%B %d, %Y", &tm);
   printf("%s\n", str_date);

   return 0;
 }

If I leave out:

char *strptime(const char *buf, const char *format, struct tm *tm);

Then I get compiler errors:

78.c: In function ‘main’:
78.c:11:5: warning: implicit declaration of function ‘strptime’; did you  mean ‘strftime’? [-Wimplicit-function-declaration]
 strptime("1912.02.14", "%Y.%m.%d", &tm);
 ^~~~~~~~
 strftime

If I add the two defines mentioned in the answer of the 2nd link I posted at the top then I can leave out the definition of strptime.

I still had an issue with 1 digit days either displaying with a leading zero or leading space in my final output regardless of what formatting I tried. In the end, I just wrote my own function to take care of this.

I appreciate everyone's help as I learned quite a bit on this issue.

Jman
  • 187
  • 1
  • 3
  • 12

1 Answers1

1

Your format of Year differs, so you should use another format, the %Y instead of %y, code:

#include <stdio.h>
#include <time.h>
char *strptime(const char *buf, const char *format, struct tm *tm);
int main(int argc, char *argv[])
{
   struct tm tm;
   char str_date[256];

   strptime("1912.02.14", "%Y.%m.%d", &tm);
   strftime(str_date, sizeof(str_date), "%A, %d %B %Y", &tm);
   printf("%s\n", str_date);

   return 0;
}

Outputs:

Wednesday, 14 February 1912
BladeMight
  • 2,670
  • 2
  • 21
  • 35
  • `char *strptime(const char *buf, const char *format, struct tm *tm);` is superfluous as its prototype is contained in `time.h`. – David C. Rankin Jan 06 '19 at 05:00
  • @BladeMight. Thanks for your help! With very minor tweaking of the date format in strftime, it outputs exactly what I was looking for. – Jman Jan 06 '19 at 15:12
  • @DavidC.Rankin Strange, when I tried compiling without this line gcc gives me error that it is not declared. – BladeMight Jan 06 '19 at 19:17
  • You need to `#define _XOPEN_SOURCE 700` **before** you include the header files. See [strptime(3) - Linux manual page](http://man7.org/linux/man-pages/man3/strptime.3.html) **Note:** the comment `/* See feature_test_macros(7) */` – David C. Rankin Jan 06 '19 at 19:50
  • @DavidC.Rankin Thanks for the info, didn't know that. – BladeMight Jan 06 '19 at 23:03