In this simple example, we'll explain how to format the label displaying month and year, and also set weekday names.
To format month and year label, we need to create a callback function to create a string given the selected time, declared under a struct tm
.
struct tm
, declared on time.h
, is a structure composed by nine integers:
- tm_sec seconds [0,59]
- tm_min minutes [0,59]
- tm_hour hour [0,23]
- tm_mday day of month [1,31]
- tm_mon month of year [0,11]
- tm_year years since 1900
- tm_wday day of week [0,6] (Sunday = 0)
- tm_yday day of year [0,365]
- tm_isdst daylight savings flag
- Note
- glib version has 2 additional fields.
For our function, only stuff that matters are tm_mon and tm_year. But we don't need to access it directly, since there are nice functions to format date and time, as strftime
. We will get abbreviated month (b) and year (y) (check strftime manpage for more) in our example: static char *
_format_month_year(struct tm *format_time)
{
char buf[32];
if (!strftime(buf, sizeof(buf), "%b %y", format_time)) return NULL;
return strdup(buf);
}
We need to alloc the string to be returned, and calendar widget will free it when it's not needed, what is done by strdup
. So let's register our callback to calendar object: elm_calendar_format_function_set(cal, _format_month_year);
To set weekday names, we should declare them as an array of strings: const char *weekdays[] =
{
"S", "M", "T", "W", "T", "F", "S"
};
And finally set them to calendar: elm_calendar_weekdays_names_set(cal, weekdays);
Our example will look like this:
See the full source code calendar_example_02.c here.