The output has to be printed Date: 2021-FEB-01
After receiving the input month and year value, for example, 2018 January should be displayed 2018-JAN-01.
how could I display YYYY-MMM-DD?
I used else if from Jan to Dec. Is there a simpler code?
if (month == 1)
printf("Date: %d-JAN-01\n", year);
else if (month == 2)
printf("Date: %d-FEB-01\n", year);
...
CodePudding user response:
The standard library time.h supports what you are trying to achieve (almost). For example:
struct tm date = {.tm_mday = 1,
.tm_mon = month - 1, // January == 0
.tm_year = year - 1900 } ; // Years since 1900
mktime( &date ) ;
char timstr[] = "YYYY-MMM-DD" ;
strftime( timstr, sizeof(timstr), "%Y-%b-%d", &date ) ;
printf( "%s\n", timstr ) ;
However the month will be output Mmm
rather then MMM
, e.g. Jan
rather the JAN
. If that is an issue then you might simply coerce the timstr[6]
and timstr[7]
to upper case thus:
strftime( timstr, sizeof(timstr), "%Y-%b-%d", &date ) ;
timstr[6] = toupper( timstr[6] ) ;
timstr[7] = toupper( timstr[7] ) ;
printf( "%s\n", timstr ) ;
or you might define a more generic conversion function for greater flexibility - should you change the format for example:
#include <ctype.h>
char* strtoupper( char* str )
{
for( char* cp = str; *cp != 0; cp )
{
*cp = toupper( *cp ) ;
}
return str ;
}
then for example:
printf( "%s\n", strtoupper( timstr ) ) ;
A complete example:
#include <stdio.h>
#include <time.h>
#include <ctype.h>
char* strtoupper( char* str )
{
for( char* cp = str; *cp != 0; cp )
{
*cp = toupper( *cp ) ;
}
return str ;
}
int main()
{
int month = 5 ;
int year = 1967 ;
struct tm date = {.tm_mday = 1,
.tm_mon = month - 1,
.tm_year = year - 1900 } ;
mktime( &date ) ;
char timstr[] = "YYYY-MMM-DD" ;
strftime( timstr, sizeof(timstr), "%Y-%b-%d", &date ) ;
printf( "%s\n", strtoupper( timstr ) ) ;
return 0;
}
Outputs:
1967-MAY-01
As does:
#include <stdio.h>
#include <time.h>
#include <ctype.h>
int main()
{
int month = 5 ;
int year = 1967 ;
struct tm date = {.tm_mday = 1,
.tm_mon = month - 1,
.tm_year = year - 1900 } ;
mktime( &date ) ;
char timstr[] = "YYYY-MMM-DD" ;
strftime( timstr, sizeof(timstr), "%Y-%b-%d", &date ) ;
timstr[6] = toupper( timstr[6] ) ;
timstr[7] = toupper( timstr[7] ) ;
printf( "%s\n", timstr ) ;
return 0;
}
CodePudding user response:
You can use an array of string literals:
#include <stdio.h>
int main(void)
{
const char *months[] = {"", "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
"JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
int year = 2022;
int month = 2;
printf("Date: %d-%s-01\n", year, months[month]);
return 0;
}
Is there any way to express it without using arrays?
Yes, take a look to strftime:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
struct tm *tmp;
time_t t;
char str[12];
time(&t);
tmp = localtime(&t);
strftime(str, sizeof(str), "%Y-%b-%d\n", tmp);
printf("Date: %s\n", str);
return(0);
}
The output is:
Date: 2022-Feb-11
Note that time()
takes the current time, you can also fill a struct tm
by hand:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
struct tm tmp = {
.tm_year = 2022 - 1900,
.tm_mon = 2,
.tm_mday = 11,
.tm_hour = 0,
.tm_min = 0,
.tm_sec= 0
};
char str[12];
strftime(str, sizeof(str), "%Y-%b-%d\n", &tmp);
printf("Date: %s\n", str);
return(0);
}