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.