All pastes #280472 Raw Edit

VoiDeD

public cpp v1 · immutable
·published 2006-12-15 22:54 UTC
rendered paste body
/*  * Time :: VoiDAPI Test Project * ----------------------------------- * Coded by: VoiDeD * * Uses: - GNU C library (string) *       - Heavy WinAPI usage (time) *       - VoiDAPI * * Time Spent: About 30mins of reading WinAPI docs * * Todo: - Clean up the code *       - Make the time string format better*/#include "voidapi.h"//main windowv_window* wnd;//three labels for infov_control* loctime;v_control* utctime;v_control* timez;//thread for updating timev_thread* thr;//winapi time structsSYSTEMTIME local;SYSTEMTIME utc;TIME_ZONE_INFORMATION tzi;//used as temporary storagestd::string tstr;void exit (v_window* wind) { //called when the main window is closed	//stops the thread	thr->kill();	//destroys the window	wind->destroy();}void settime(SYSTEMTIME time, v_label* label, char* orig) { //used to set the labels	//allocates enough data for time data	char* hour = (char*)malloc(2);	char* minute = (char*)malloc(2);	char* second = (char*)malloc(2);	char* ms = (char*)malloc(3);	char* m = (char*)malloc(2);	//24 hour -> 12 hour	if (time.wHour > 12) {		itoa(time.wHour-12, hour, 10);		m = "PM";	}	else {		itoa(time.wHour, hour, 10);		m = "AM";	}	//convert times to strings	itoa(time.wMinute, minute, 10);	itoa(time.wSecond, second, 10);	itoa(time.wMilliseconds, ms, 10);	//format the string	tstr = orig;	if (strlen(hour) == 1)		tstr += "0";	tstr += hour;	tstr += ":";	if (strlen(minute) == 1)		tstr += "0";	tstr += minute;	tstr += ":";	if (strlen(second) == 1)		tstr += "0";	tstr += second;	tstr += ".";	tstr += ms;	for (int x = 0; x < 3 - strlen(ms); x++) {		tstr += "0";	}	tstr += " ";	tstr += m;	//set the label	label->set_text(tstr.c_str());}void loop(v_thread* thread, void* data) {	//used for temp storage	std::string str;	while (true) {		//gets system and local time		GetSystemTime(&utc);		GetLocalTime(&local);		//calls settime to set the labels		settime(local, (v_label*)loctime, "Local Time: ");		settime(utc, (v_label*)utctime, "  UTC Time: ");		str = "  Timezone: ";		//get time zone information		if (GetTimeZoneInformation(&tzi) == TIME_ZONE_ID_STANDARD)			str += (char*)tzi.StandardName;		else			str += (char*)tzi.DaylightName;		//set time zone label		timez->set_text(str.c_str());		//sleep thread for 1 ms		thread->sleep(1);	}}int main(int argc, char* argv[]) {	//creates a new window	wnd = new v_window("Time", new v_size(300,100));		//creates 3 labels	loctime = new v_label("Local Time:", new v_size(300, 20), new v_point(5,5), wnd->get_hwnd(), false);	utctime = new v_label("  UTC Time:", new v_size(300, 20), new v_point(5, 25), wnd->get_hwnd(), false);	timez = new v_label("  Timezone:", new v_size(300, 20), new v_point(5, 45), wnd->get_hwnd(), false);		//creates a new thread and runs it	thr = new v_thread(&loop, NULL);	thr->start();	//registers the close event	wnd->register_event(WM_CLOSE, &exit);		//add the 3 labels	wnd->control_add(loctime);	wnd->control_add(utctime);	wnd->control_add(timez);		//show the window	wnd->show();}