Source

pebble / nederlands.c

Full commit
/** \file
 * Word clock, with Dutch time
 *
 * een uur
 * een over een
 * kwart over een
 * tien voor half twee
 * half twee
 * vijf over half twee
 * kwart voor twee
 * 
 * And 's ochtends, 's middags, 's avonds, 's nachts
 * vier uur
 * half vier 
 */
#include "pebble_os.h"
#include "pebble_app.h"
#include "pebble_fonts.h"


static const char *nums[] = {
	"",
	"een",
	"twee",
	"drie",
	"vier",
	"vijf",
	"zes",
	"zeven",
	"acht",
	"negen",
	"tien",
	"elf",
	"twaalf",
	"dertien",
	"veertien",
	"kwart",
};


static const char *
min_string(
	int i
)
{
	return nums[i];
}


static const char *
hour_string(
	int h
)
{
	if (h == 0 || h == 24)
		return "middernacht";

	if (h == 12)
		return "middag";

	if (h < 12)
		return nums[h];
	else
		return nums[h - 12];
}


static void
nederlands_format(
	char * buf,
	int hour,
	int min
)
{
	buf[0] = '\0';

	if (min == 0)
	{
		// just the hour + uur, except at midnight and noon
		strcat(buf, hour_string(hour));
		if (hour != 0 && hour != 12)
			strcat(buf, "\nuur");
	} else
	if (min <= 15)
	{
		// over the hour
		strcat(buf, min_string(min));
		strcat(buf, "\nover\n");
		strcat(buf, hour_string(hour));
	} else
	if (min < 30)
	{
		// over the kwart
		strcat(buf, min_string(30 - min));
		strcat(buf, "\nvoor half\n");
		strcat(buf, hour_string(++hour));
	} else
	if (min == 30)
	{
		// just the half
		strcat(buf, "half\n");
		strcat(buf, hour_string(++hour));
	} else
	if (min < 45)
	{
		// over the half
		strcat(buf, min_string(min - 30));
		strcat(buf, "\nover half\n");
		strcat(buf, hour_string(++hour));
	} else
	if (min == 45)
	{
		// just the kwart
		strcat(buf, "kwart voor\n");
		strcat(buf, hour_string(++hour));
	} else
	if (min < 60)
	{
		// over the kwart
		strcat(buf, min_string(60 - min));
		strcat(buf, "\nvoor\n");
		strcat(buf, hour_string(++hour));
	}

	// add am/pm like phrase
	if (hour == 0 || hour == 12 || hour == 24)
	{
		// nothing to do
	} else
	if (hour < 12)
		strcat(buf, "\ns'ochtends");
	else
	if (hour < 18)
		strcat(buf, "\ns'middags");
	else
	if (hour < 21)
		strcat(buf, "\ns'avonds");
	else
		strcat(buf, "\ns'nachts");
}

PBL_APP_INFO(
	"Nederlandse",
	"hudson",
	1, // Version
	INVALID_RESOURCE,
	APP_INFO_WATCH_FACE
);


static char time_buffer[32];
static Window window;
static TextLayer time_layer;

/** Called once per minute */
static void
handle_tick(
	AppContextRef ctx,
	PebbleTickEvent * const event
)
{
	(void) ctx;
	const PblTm * const ptm = event->tick_time;

	int hour = ptm->tm_hour;
	int min = ptm->tm_min;

	nederlands_format(time_buffer, hour,  min);

/*
	string_format_time(
		time_buffer,
		sizeof(time_buffer),
		"%H:%M",
		event->tick_time
	);
*/
	text_layer_set_text(&time_layer, time_buffer);
}


static void
handle_init(
	AppContextRef ctx
)
{
	(void) ctx;

	window_init(&window, "Main");
	window_stack_push(&window, true);
	window_set_background_color(&window, GColorBlack);

	text_layer_init(&time_layer, GRect(0,0,144,168));
	time_buffer[0] = '\0';
	text_layer_set_text(&time_layer, time_buffer);
	text_layer_set_text_color(&time_layer, GColorWhite);
	text_layer_set_background_color(&time_layer, GColorBlack);
	text_layer_set_font(&time_layer, fonts_get_system_font(FONT_KEY_GOTHAM_30_BLACK));
	layer_add_child(&window.layer, &time_layer.layer);
}


void
pbl_main(
	void * const params
)
{
	PebbleAppHandlers handlers = {
		.init_handler	= &handle_init,
		.tick_info	= {
			.tick_handler = &handle_tick,
			.tick_units = MINUTE_UNIT,
		},
	};

	app_event_loop(params, &handlers);
}