#include <stdio.h>
#include <time.h>
#include <string.h>
int main(){
time_t rawtime;
struct tm *info;
char buf[80];
time(&rawtime);
info= localtime(&rawtime);
//Wed Nov 30 17:00:54 2022
strftime(buf, 80, "%a %b %d %X %Y ", info);
//time zone msia = MST
char zone[5]={'M','S','T'};
int carr1= 13, line1= 10;
char carriage= carr1, linefeed= line1;
zone[4]= carriage;
zone[5]= linefeed;
//concat
int len1= strlen(buf);
for (int len2=0; len2<5; len1++, len2++){
buf[len1]=zone[len2];
}
printf("%s",buf);
}
I expect to get Thu Dec 12 xx:xx:xx MST, however it only outputs starting from second index and becomes hu Dec 12 xx:xx:xx MST. I don't quite understand why it does that after manually concatenating it.
zone[5]= linefeed; is writing to the 6th element of zone which only has 5 elements. And the location where zone[5] would be is the same as where buf[0] is (at least for me and, apparently, for you, though I don't think that is guaranteed).
Related
These two links have been helpful thus far:
How do I printf a date in C?
Compiler gets warnings when using strptime function (C)
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.
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
I'm trying to write a program in C, read data from the keyboard in the form dd-mm-yy and want to display the date as January 3rd, 1999.
How can i do it?
Try it, hope helps to solve your problem
#include <stdio.h>
#include <time.h>
int main()
{
struct tm tm_info;
char buffer[255];
char days[32][5] = {" ","1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th","21st","22nd","23rd","24th","25th","26th","27th","28th","29th","30th","31st"};
strptime("09-07-2017", "%d-%m-%Y ", &tm_info);
strftime(buffer, 26, "%B", &tm_info);
printf("%s ",buffer);
strftime(buffer, 26, "%d", &tm_info);
int day = int(buffer[0]-'0')*10 + int(buffer[1]-'0');
printf("%s, ",days[day]);
strftime(buffer, 26, "%Y", &tm_info);
printf("%s ",buffer);
}
Use strptime & strftime. Like this ->
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main()
{
struct tm tm;
char buf[255];
memset(&tm, 0, sizeof(struct tm));
strptime("3-1-1999", "%d-%m-%Y ", &tm);
strftime(buf, sizeof(buf), "%d %b %Y", &tm);
puts(buf);
exit(EXIT_SUCCESS);
}
It will show the output =>
03 Jan 1999
When I execute the following code:
#include <time.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
time_t rawtime = 0;
time_t secs;
struct tm* timeinfo = gmtime(&rawtime);
printf("rawtime : %s\n", asctime(timeinfo));
secs = mktime(timeinfo);
printf("converted time : %s\n", asctime(gmtime(&secs)));
return 0;
}
The output is :
rawtime : Thu Jan 1 00:00:00 1970
converted time : Wed Dec 31 23:00:00 1969
Why is this one hour difference?
I am running Ubuntu 14.10 64bit btw.
Because mktime converts a local time, not a system time (gmtime), into a timestamp.
I know how to get current date & time and save it into an array. But I would like to print it in format: dd.mm.YYY_HH:MM:ss. How can I change my code to achieve this?
#include <stdio.h>
#include <time.h>
char *datetime()
{
char *array = (char*)malloc(sizeof(char)*25);
time_t result;
result = time(NULL);
sprintf(array, "%s", asctime(localtime(&result)));
array[25] = '\0';
return array;
}
int main(void)
{
// prints Sat Aug 3 18:39:07 2013
printf("%s", datetime());
// how to print:
// 03.08.2013_18:39:07
// ?
return(0);
}
Using strftime - see here
This will look like this -
time_t rawtime;
time (&rawtime);
struct tm *timeinfo = localtime (&rawtime);
strftime(array, sizeof(array)-1, "%d.%m.%y_%H:%M:%S", timeinfo);
You can use strftime function:
http://www.gnu.org/software/libc/manual/html_node/Formatting-Calendar-Time.html
strftime is described in C99 Standard in 7.23.3.5.
I know that with the usage of ctime like this
time_t now;
time(&now);
fprintf(ft,"%s",ctime(&now));
returns me the datetime in this way
Tue Jun 18 12:45:52 2013
My question is if there is something similar with ctime to get the time in this format
2013/06/18 10:15:26
Use strftime
#include <stdio.h>
#include <time.h>
int main()
{
struct tm *tp;
time_t t;
char s[80];
t = time(NULL);
tp = localtime(&t);
strftime(s, 80, "%Y/%m/%d %H:%M:%S", tp);
printf("%s\n", s);
return 0;
}
See the manpages for localtime and strftime. The first function converts the timestamp in a structure with the date/time elements, the second converts that to a string using a format string.
Broken-down time is stored in the structure tm which is defined in as follows:
struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};
It is possible to display individual variables in the format we desire to achieve.
#include <stdio.h>
#include <time.h>
int main(void){
FILE *ft = stdout;
char outbuff[32];
struct tm *timeptr;
time_t now;
time(&now);
timeptr = localtime(&now);
strftime(outbuff, sizeof(outbuff), "%Y/%m/%d %H:%M:%S", timeptr);//%H:24 hour
fprintf(ft,"%s", outbuff);
return 0;
}