Thanks for all this help mats, all these are things I just wouldn't have picked up on. I've now changed it. Please, any more tips would be great. Also I still can't visualize how to check if the array passed in is large enough. If I use strlen() won't it stop at the first null character, so practically never be true? How do I make sure the array is big enough?
Also could sscanf() be used in the dateTimeFromString function?
Code:
#include "datetime.h"
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#define DATE_TIME_BUFF_SIZE 30
/* Return a DateTime record (i.e., a struct tm record) containing
* the information extracted from the given string, which is assumed
* to be one of the strings specifying date/time within the XML file.
* For example, s might be "20090310150506" (the double-quotes are
* not characters in the string), denoting 2009/03/10 15:05:06. The
* seconds (06 in the example) can be ignored -- they are not used
* by the program.
*/
DateTime dateTimeFromString(const char *s)
{
DateTime result;
char year[5];
char month[3];
char day[3];
char hour[3];
char min[3];
strncpy(year, s, 4);
strncpy(month, s + 4, 2);
strncpy(day, s + 6, 2);
strncpy(hour, s + 8, 2);
strncpy(min, s + 10, 2);
year[4] = 0;
month[2] = 0;
day[2] = 0;
hour[2] = 0;
min[2] = 0;
result.tm_year = atoi(year) - 1900;
result.tm_mon = atoi(month);
result.tm_mday = atoi(day);
result.tm_hour = atoi(hour);
result.tm_min = atoi(min);
result.tm_sec = 0;
result.tm_isdst = -1;
/* Use mktime to work out day of week and day of year */
mktime(&result);
return result;
}
/* Insert into the given buffer a string representation of the
* date part of the given DateTime record, assumed to have been
* created by a call to dateTimeFromString. The string should be
* of the form:
* Tuesday 10 March, 2009
* with both the day of the week and the month given in full.
* The supplied buffer must be at least DATE_TIME_BUFF_SIZE characters
* in length.
*/
void makeDateString(const DateTime *dateTime, char *buff)
{
if(sizeof(*buff) > (sizeof(int) * DATE_TIME_BUFF_SIZE))
{
strftime(buff, 30, "%A %d %B, %Y", dateTime);
}
}
/* Insert into the given buffer a string representation of the
* time part of the given DateTime record, assumed to have been
* created by a call to dateTimeFromString. The string should be
* four digits plus a terminating null, e.g. 1036 or 0309,
* specifying just the hour and the minute on a 24 hour clock basis.
* The supplied buffer must be at least DATE_TIME_BUFF_SIZE characters
* in length.
*/
void makeTimeString(const DateTime *dateTime, char *buff)
{
if(sizeof(*buff) > (sizeof(int) * DATE_TIME_BUFF_SIZE)) /* buffer is bigger than a maximum */
{
strftime(buff, 5, "%H%M", dateTime);
}
}
int sameDay(const DateTime *dt1, const DateTime *dt2)
{
int result;
char buff1[30], buff2[30];
makeDateString(dt1, buff1);
makeDateString(dt2, buff2);
if (strcmp(buff1, buff2) == 0)
{
result = 1;
}
else
{
result = 0;
}
return result;
}
int main()
{
char test[DATE_TIME_BUFF_SIZE + 1];
char buff[DATE_TIME_BUFF_SIZE + 1];
DateTime firstTry;
strncpy(test, "20090312150317", 14);
firstTry = dateTimeFromString(test);
makeDateString(&firstTry, buff);
printf("%s", buff);
return 1;
}