Index: boot/Makefile.common
===================================================================
--- boot/Makefile.common	(revision 32fef47bf1b96f02c8a4ccc9728b95e42ef4624a)
+++ boot/Makefile.common	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -184,5 +184,6 @@
 	$(USPACE_PATH)/app/vuhid/vuh \
 	$(USPACE_PATH)/app/mkbd/mkbd \
-	$(USPACE_PATH)/app/websrv/websrv
+	$(USPACE_PATH)/app/websrv/websrv \
+	$(USPACE_PATH)/app/date/date
 
 ifeq ($(CONFIG_PCC),y)
Index: boot/arch/amd64/Makefile.inc
===================================================================
--- boot/arch/amd64/Makefile.inc	(revision 32fef47bf1b96f02c8a4ccc9728b95e42ef4624a)
+++ boot/arch/amd64/Makefile.inc	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -42,4 +42,5 @@
 	char/ps2mouse \
 	char/xtkbd \
+	time/cmos-rtc \
 	bus/usb/ehci\
 	bus/usb/ohci \
Index: uspace/Makefile
===================================================================
--- uspace/Makefile	(revision 32fef47bf1b96f02c8a4ccc9728b95e42ef4624a)
+++ uspace/Makefile	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -71,4 +71,5 @@
 	app/sysinfo \
 	app/mkbd \
+	app/date \
 	srv/clipboard \
 	srv/loc \
@@ -122,5 +123,5 @@
 	drv/nic/ne2k \
 	drv/nic/e1k \
-	drv/nic/rtl8139
+	drv/nic/rtl8139 \
 
 ifeq ($(CONFIG_PCC),y)
@@ -146,4 +147,5 @@
 		drv/bus/isa \
 		drv/char/ns8250 \
+		drv/time/cmos-rtc \
 		srv/hw/irc/apic \
 		srv/hw/irc/i8259
@@ -156,4 +158,5 @@
 		drv/bus/isa \
 		drv/char/ns8250 \
+		drv/time/cmos-rtc \
 		srv/hw/irc/apic \
 		srv/hw/irc/i8259
Index: uspace/app/date/Makefile
===================================================================
--- uspace/app/date/Makefile	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
+++ uspace/app/date/Makefile	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -0,0 +1,35 @@
+#
+# Copyright (c) 2012 Maurizio Lombardi
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# - Redistributions of source code must retain the above copyright
+#   notice, this list of conditions and the following disclaimer.
+# - Redistributions in binary form must reproduce the above copyright
+#   notice, this list of conditions and the following disclaimer in the
+#   documentation and/or other materials provided with the distribution.
+# - The name of the author may not be used to endorse or promote products
+#   derived from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+
+USPACE_PREFIX = ../..
+BINARY = date
+
+SOURCES = \
+	date.c
+
+include $(USPACE_PREFIX)/Makefile.common
Index: uspace/app/date/date.c
===================================================================
--- uspace/app/date/date.c	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
+++ uspace/app/date/date.c	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -0,0 +1,287 @@
+/*
+ * Copyright (c) 2012 Maurizio Lombardi
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+
+#include <stdio.h>
+#include <device/clock_dev.h>
+#include <errno.h>
+#include <loc.h>
+#include <time.h>
+#include <malloc.h>
+#include <ipc/clock_ctl.h>
+#include <getopt.h>
+#include <ctype.h>
+
+#define NAME   "date"
+
+static int read_date_from_arg(char *wdate, struct tm *t);
+static int read_time_from_arg(char *wdate, struct tm *t);
+static int read_num_from_str(char *str, size_t len, int *n);
+static void usage(void);
+
+int
+main(int argc, char **argv)
+{
+	int rc, c;
+	category_id_t cat_id;
+	size_t        svc_cnt;
+	service_id_t  *svc_ids = NULL;
+	service_id_t  svc_id;
+	char          *svc_name = NULL;
+	bool          read_only = true;
+	char          *wdate = NULL;
+	char          *wtime = NULL;
+	sysarg_t      battery_ok;
+	struct tm     t;
+	int           n_args = argc;
+
+	while ((c = getopt(argc, argv, "hd:t:")) != -1) {
+		switch (c) {
+		case 'h':
+			usage();
+			return 0;
+		case 'd':
+			if (wdate) {
+				usage();
+				return 1;
+			}
+			wdate = (char *)optarg;
+			read_only = false;
+			n_args -= 2;
+			break;
+		case 't':
+			if (wtime) {
+				usage();
+				return 1;
+			}
+			wtime = (char *)optarg;
+			read_only = false;
+			n_args -= 2;
+			break;
+		case '?':
+			usage();
+			return 1;
+		}
+	}
+
+	if (n_args != 1) {
+		printf(NAME ": Unrecognized parameter\n");
+		usage();
+		return 1;
+	}
+
+	/* Get the id of the clock category */
+	rc = loc_category_get_id("clock", &cat_id, IPC_FLAG_BLOCKING);
+	if (rc != EOK) {
+		printf(NAME ": Cannot get clock category id\n");
+		goto exit;
+	}
+
+	/* Get the list of available services in the clock category */
+	rc = loc_category_get_svcs(cat_id, &svc_ids, &svc_cnt);
+	if (rc != EOK) {
+		printf(NAME ": Cannot get the list of services in category \
+		    clock\n");
+		goto exit;
+	}
+
+	/* Check if the are available services in the clock category */
+	if (svc_cnt == 0) {
+		printf(NAME ": No available service found in \
+		    the clock category\n");
+		goto exit;
+	}
+
+	/* Get the name of the clock service */
+	rc = loc_service_get_name(svc_ids[0], &svc_name);
+	if (rc != EOK) {
+		printf(NAME ": Cannot get the name of the service\n");
+		goto exit;
+	}
+
+	/* Get the service id for the device */
+	rc = loc_service_get_id(svc_name, &svc_id, 0);
+	if (rc != EOK) {
+		printf(NAME ": Cannot get the service id for device %s",
+		    svc_name);
+		goto exit;
+	}
+
+	/* Connect to the device */
+	async_sess_t *sess = loc_service_connect(EXCHANGE_SERIALIZE,
+	    svc_id, 0);
+	if (!sess) {
+		printf(NAME ": Cannot connect to the device\n");
+		goto exit;
+	}
+
+	/* Check the battery status (if present) */
+	async_exch_t *exch = async_exchange_begin(sess);
+	rc = async_req_0_1(exch, CLOCK_GET_BATTERY_STATUS, &battery_ok);
+	async_exchange_end(exch);
+
+	if (rc == EOK && !battery_ok)
+		printf(NAME ": Warning! RTC battery dead\n");
+
+	/* Read the current date/time */
+	rc = clock_dev_time_get(sess, &t);
+	if (rc != EOK) {
+		printf(NAME ": Cannot read the current time\n");
+		goto exit;
+	}
+
+	if (read_only) {
+		/* Print the current time and exit */
+		printf("%02d/%02d/%d ", t.tm_mday,
+		    t.tm_mon + 1, 1900 + t.tm_year);
+		printf("%02d:%02d:%02d\n", t.tm_hour, t.tm_min, t.tm_sec);
+	} else {
+		if (wdate) {
+			rc = read_date_from_arg(wdate, &t);
+			if (rc != EOK) {
+				printf(NAME ": error, date format not "
+				    "recognized\n");
+				usage();
+				goto exit;
+			}
+		}
+		if (wtime) {
+			rc = read_time_from_arg(wtime, &t);
+			if (rc != EOK) {
+				printf(NAME ": error, time format not "
+				    "recognized\n");
+				usage();
+				goto exit;
+			}
+		}
+
+		rc = clock_dev_time_set(sess, &t);
+		if (rc != EOK) {
+			printf(NAME ": error, Unable to set date/time\n");
+			goto exit;
+		}
+	}
+
+exit:
+	free(svc_name);
+	free(svc_ids);
+	return rc;
+}
+
+/** Read the day, month and year from a string
+ *  with the following format: DD/MM/YYYY
+ */
+static int
+read_date_from_arg(char *wdate, struct tm *t)
+{
+	int rc;
+
+	if (str_size(wdate) != 10) /* str_size("DD/MM/YYYY") == 10 */
+		return EINVAL;
+
+	if (wdate[2] != '/' ||
+	    wdate[5] != '/') {
+		return EINVAL;
+	}
+
+	rc = read_num_from_str(&wdate[0], 2, &t->tm_mday);
+	if (rc != EOK)
+		return rc;
+
+	rc = read_num_from_str(&wdate[3], 2, &t->tm_mon);
+	if (rc != EOK)
+		return rc;
+	t->tm_mon--;
+
+	rc = read_num_from_str(&wdate[6], 4, &t->tm_year);
+	t->tm_year -= 1900;
+	return rc;
+}
+
+/** Read the hours, minutes and seconds from a string
+ *  with the following format: HH:MM:SS or HH:MM
+ */
+static int
+read_time_from_arg(char *wtime, struct tm *t)
+{
+	int rc;
+	size_t len = str_size(wtime);
+	bool sec_present = len == 8;
+
+	/* str_size("HH:MM") == 5 */
+	/* str_size("HH:MM:SS") == 8 */
+	if (len != 8 && len != 5)
+		return EINVAL;
+
+	if (sec_present && wtime[5] != ':')
+		return EINVAL;
+
+	if (wtime[2] != ':')
+		return EINVAL;
+
+	rc = read_num_from_str(&wtime[0], 2, &t->tm_hour);
+	if (rc != EOK)
+		return rc;
+
+	rc = read_num_from_str(&wtime[3], 2, &t->tm_min);
+	if (rc != EOK)
+		return rc;
+
+	if (sec_present)
+		rc = read_num_from_str(&wtime[6], 2, &t->tm_sec);
+	else
+		t->tm_sec = 0;
+
+	return rc;
+}
+
+static int
+read_num_from_str(char *str, size_t len, int *n)
+{
+	size_t i;
+
+	*n = 0;
+
+	for (i = 0; i < len; ++i, ++str) {
+		if (!isdigit(*str))
+			return EINVAL;
+		*n *= 10;
+		*n += *str - '0';
+	}
+
+	return EOK;
+}
+
+static void
+usage(void)
+{
+	printf("Usage: date [-d DD/MM/YYYY] [-t HH:MM[:SS]]\n");
+	printf("       -d   Change the current date\n");
+	printf("       -t   Change the current time\n");
+}
+
Index: uspace/drv/bus/isa/isa.dev
===================================================================
--- uspace/drv/bus/isa/isa.dev	(revision 32fef47bf1b96f02c8a4ccc9728b95e42ef4624a)
+++ uspace/drv/bus/isa/isa.dev	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -27,2 +27,6 @@
 	dma 1
 	dma 5
+
+cmos-rtc:
+	match 100 isa/cmos-rtc
+	io_range 70 2
Index: uspace/drv/time/cmos-rtc/Makefile
===================================================================
--- uspace/drv/time/cmos-rtc/Makefile	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
+++ uspace/drv/time/cmos-rtc/Makefile	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -0,0 +1,37 @@
+#
+# Copyright (c) 2012 Maurizio Lombardi
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# - Redistributions of source code must retain the above copyright
+#   notice, this list of conditions and the following disclaimer.
+# - Redistributions in binary form must reproduce the above copyright
+#   notice, this list of conditions and the following disclaimer in the
+#   documentation and/or other materials provided with the distribution.
+# - The name of the author may not be used to endorse or promote products
+#   derived from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+
+USPACE_PREFIX = ../../..
+LIBS = $(LIBDRV_PREFIX)/libdrv.a
+EXTRA_CFLAGS += -I$(LIBDRV_PREFIX)/include
+BINARY = cmos-rtc
+
+SOURCES = \
+	cmos-rtc.c
+
+include $(USPACE_PREFIX)/Makefile.common
Index: uspace/drv/time/cmos-rtc/cmos-regs.h
===================================================================
--- uspace/drv/time/cmos-rtc/cmos-regs.h	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
+++ uspace/drv/time/cmos-rtc/cmos-regs.h	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2012 Maurizio Lombardi
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _CMOS_RTC_H_
+#define _CMOS_RTC_H_
+
+#define RTC_SEC           0x00
+#define RTC_MIN           0x02
+#define RTC_HOUR          0x04
+#define RTC_DAY           0x07
+#define RTC_MON           0x08
+#define RTC_YEAR          0x09
+
+#define RTC_STATUS_B      0x0B
+#define RTC_MASK_24H      0x02 /* 24h mode */
+#define RTC_MASK_BCD      0x04 /* BCD mode */
+#define RTC_MASK_INH      0x80 /* Inhibit updates */
+
+#define RTC_STATUS_D      0x0D
+#define RTC_BATTERY_OK    0x80 /* Battery status */
+
+#define RTC_STATUS_A      0x0A
+#define RTC_MASK_UPDATE   0x80 /* Update in progress */
+#define RTC_MASK_CLK_STOP 0x70 /* Stop the clock */
+
+#endif
+
Index: uspace/drv/time/cmos-rtc/cmos-rtc.c
===================================================================
--- uspace/drv/time/cmos-rtc/cmos-rtc.c	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
+++ uspace/drv/time/cmos-rtc/cmos-rtc.c	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -0,0 +1,706 @@
+/*
+ * Copyright (c) 2012 Maurizio Lombardi
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @defgroup CMOS RTC driver.
+ * @brief HelenOS RTC driver.
+ * @{
+ */
+
+/** @file
+ */
+
+#include <errno.h>
+#include <ddi.h>
+#include <libarch/ddi.h>
+#include <stdio.h>
+#include <ddf/driver.h>
+#include <ddf/log.h>
+#include <ops/clock_dev.h>
+#include <fibril_synch.h>
+#include <device/hw_res.h>
+#include <devman.h>
+#include <ipc/clock_ctl.h>
+
+#include "cmos-regs.h"
+
+#define NAME "cmos-rtc"
+
+#define REG_COUNT 2
+
+#define RTC_FROM_FNODE(fnode)  ((rtc_t *) ((fnode)->dev->driver_data))
+#define RTC_FROM_DEV(devnode)  ((rtc_t *) ((devnode)->driver_data))
+
+typedef struct rtc {
+	/** DDF device node */
+	ddf_dev_t *dev;
+	/** DDF function node */
+	ddf_fun_t *fun;
+	/** The fibril mutex for synchronizing the access to the device */
+	fibril_mutex_t mutex;
+	/** The base I/O address of the device registers */
+	uint32_t io_addr;
+	/** The I/O port used to access the CMOS registers */
+	ioport8_t *port;
+	/** true if a client is connected to the device */
+	bool client_connected;
+	/** true if device is removed */
+	bool removed;
+} rtc_t;
+
+
+static int  rtc_time_get(ddf_fun_t *fun, struct tm *t);
+static int  rtc_time_set(ddf_fun_t *fun, struct tm *t);
+static int  rtc_dev_add(ddf_dev_t *dev);
+static int  rtc_dev_initialize(rtc_t *rtc);
+static bool rtc_pio_enable(rtc_t *rtc);
+static void rtc_dev_cleanup(rtc_t *rtc);
+static int  rtc_open(ddf_fun_t *fun);
+static void rtc_close(ddf_fun_t *fun);
+static bool rtc_update_in_progress(rtc_t *rtc);
+static int  rtc_register_read(rtc_t *rtc, int reg);
+static unsigned bcd2bin(unsigned bcd);
+static unsigned bin2bcd(unsigned binary);
+static void rtc_default_handler(ddf_fun_t *fun,
+    ipc_callid_t callid, ipc_call_t *call);
+static int rtc_dev_remove(ddf_dev_t *dev);
+static int rtc_tm_sanity_check(struct tm *t, int epoch);
+static void rtc_register_write(rtc_t *rtc, int reg, int data);
+static bool is_leap_year(int year);
+
+static int days_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
+
+static ddf_dev_ops_t rtc_dev_ops;
+
+/** The RTC device driver's standard operations */
+static driver_ops_t rtc_ops = {
+	.dev_add = rtc_dev_add,
+	.dev_remove = rtc_dev_remove,
+};
+
+/** The RTC device driver structure */
+static driver_t rtc_driver = {
+	.name = NAME,
+	.driver_ops = &rtc_ops,
+};
+
+/** Clock interface */
+static clock_dev_ops_t rtc_clock_dev_ops = {
+	.time_get = rtc_time_get,
+	.time_set = rtc_time_set,
+};
+
+/** Initialize the RTC driver */
+static void
+rtc_init(void)
+{
+	ddf_log_init(NAME, LVL_ERROR);
+
+	rtc_dev_ops.open = rtc_open;
+	rtc_dev_ops.close = rtc_close;
+
+	rtc_dev_ops.interfaces[CLOCK_DEV_IFACE] = &rtc_clock_dev_ops;
+	rtc_dev_ops.default_handler = &rtc_default_handler;
+}
+
+/** Clean up the RTC soft state
+ *
+ * @param rtc  The RTC device
+ */
+static void
+rtc_dev_cleanup(rtc_t *rtc)
+{
+	if (rtc->dev->parent_sess) {
+		async_hangup(rtc->dev->parent_sess);
+		rtc->dev->parent_sess = NULL;
+	}
+}
+
+/** Enable the I/O ports of the device
+ *
+ * @param rtc  The real time clock device
+ *
+ * @return  true in case of success, false otherwise
+ */
+static bool
+rtc_pio_enable(rtc_t *rtc)
+{
+	if (pio_enable((void *)(uintptr_t) rtc->io_addr, REG_COUNT,
+	    (void **) &rtc->port)) {
+
+		ddf_msg(LVL_ERROR, "Cannot map the port %#" PRIx32
+		    " for device %s", rtc->io_addr, rtc->dev->name);
+		return false;
+	}
+
+	return true;
+}
+
+/** Initialize the RTC device
+ *
+ * @param rtc  Pointer to the RTC device
+ *
+ * @return  EOK on success or a negative error code
+ */ 
+static int
+rtc_dev_initialize(rtc_t *rtc)
+{
+	int rc;
+	size_t i;
+	hw_resource_t *res;
+	bool ioport = false;
+
+	ddf_msg(LVL_DEBUG, "rtc_dev_initialize %s", rtc->dev->name);
+
+	hw_resource_list_t hw_resources;
+	memset(&hw_resources, 0, sizeof(hw_resource_list_t));
+
+	/* Connect to the parent's driver */
+
+	rtc->dev->parent_sess = devman_parent_device_connect(EXCHANGE_SERIALIZE,
+	    rtc->dev->handle, IPC_FLAG_BLOCKING);
+	if (!rtc->dev->parent_sess) {
+		ddf_msg(LVL_ERROR, "Failed to connect to parent driver\
+		    of device %s.", rtc->dev->name);
+		rc = ENOENT;
+		goto error;
+	}
+
+	/* Get the HW resources */
+	rc = hw_res_get_resource_list(rtc->dev->parent_sess, &hw_resources);
+	if (rc != EOK) {
+		ddf_msg(LVL_ERROR, "Failed to get HW resources\
+		    for device %s", rtc->dev->name);
+		goto error;
+	}
+
+	for (i = 0; i < hw_resources.count; ++i) {
+		res = &hw_resources.resources[i];
+
+		if (res->type == IO_RANGE) {
+			if (res->res.io_range.size < REG_COUNT) {
+				ddf_msg(LVL_ERROR, "I/O range assigned to \
+				    device %s is too small", rtc->dev->name);
+				rc = ELIMIT;
+				goto error;
+			}
+			rtc->io_addr = res->res.io_range.address;
+			ioport = true;
+			ddf_msg(LVL_NOTE, "Device %s was assigned I/O address \
+			    0x%x", rtc->dev->name, rtc->io_addr);
+		}
+	}
+
+	if (!ioport) {
+		/* No I/O address assigned to this device */
+		ddf_msg(LVL_ERROR, "Missing HW resource for device %s",
+		    rtc->dev->name);
+		rc = ENOENT;
+		goto error;
+	}
+
+	hw_res_clean_resource_list(&hw_resources);
+
+	return EOK;
+
+error:
+	rtc_dev_cleanup(rtc);
+	hw_res_clean_resource_list(&hw_resources);
+
+	return rc;
+}
+
+/** Read a register from the CMOS memory
+ *
+ * @param rtc    The rtc device
+ * @param reg    The index of the register to read
+ *
+ * @return       The value of the register
+ */
+static int
+rtc_register_read(rtc_t *rtc, int reg)
+{
+	pio_write_8(rtc->port, reg);
+	return pio_read_8(rtc->port + 1);
+}
+
+/** Write a register to the CMOS memory
+ *
+ * @param rtc    The rtc device
+ * @param reg    The index of the register to write
+ * @param data   The data to write
+ */
+static void
+rtc_register_write(rtc_t *rtc, int reg, int data)
+{
+	pio_write_8(rtc->port, reg);
+	pio_write_8(rtc->port + 1, data);
+}
+
+/** Check if an update is in progress
+ *
+ * @param rtc  The rtc device
+ *
+ * @return  true if an update is in progress, false otherwise
+ */
+static bool
+rtc_update_in_progress(rtc_t *rtc)
+{
+	return rtc_register_read(rtc, RTC_STATUS_A) & RTC_MASK_UPDATE;
+}
+
+/** Read the current time from the CMOS
+ *
+ * @param fun  The RTC function
+ * @param t    Pointer to the time variable
+ *
+ * @return  EOK on success or a negative error code
+ */
+static int
+rtc_time_get(ddf_fun_t *fun, struct tm *t)
+{
+	bool bcd_mode;
+	bool pm_mode = false;
+	int  epoch = 1900;
+	rtc_t *rtc = RTC_FROM_FNODE(fun);
+
+	fibril_mutex_lock(&rtc->mutex);
+
+	/* now read the registers */
+	do {
+		/* Suspend until the update process has finished */
+		while (rtc_update_in_progress(rtc));
+
+		t->tm_sec  = rtc_register_read(rtc, RTC_SEC);
+		t->tm_min  = rtc_register_read(rtc, RTC_MIN);
+		t->tm_hour = rtc_register_read(rtc, RTC_HOUR);
+		t->tm_mday = rtc_register_read(rtc, RTC_DAY);
+		t->tm_mon  = rtc_register_read(rtc, RTC_MON);
+		t->tm_year = rtc_register_read(rtc, RTC_YEAR);
+
+		/* Now check if it is stable */
+	} while( t->tm_sec  != rtc_register_read(rtc, RTC_SEC) ||
+		 t->tm_min  != rtc_register_read(rtc, RTC_MIN) ||
+		 t->tm_mday != rtc_register_read(rtc, RTC_DAY) ||
+		 t->tm_mon  != rtc_register_read(rtc, RTC_MON) ||
+		 t->tm_year != rtc_register_read(rtc, RTC_YEAR));
+
+	/* Check if the RTC is working in 12h mode */
+	bool _12h_mode = !(rtc_register_read(rtc, RTC_STATUS_B) &
+	    RTC_MASK_24H);
+
+	if (_12h_mode) {
+		/* The RTC is working in 12h mode, check if it is AM or PM */
+		if (t->tm_hour & 0x80) {
+			/* PM flag is active, it must be cleared */
+			t->tm_hour &= ~0x80;
+			pm_mode = true;
+		}
+	}
+
+	/* Check if the RTC is working in BCD mode */
+	bcd_mode = !(rtc_register_read(rtc, RTC_STATUS_B) & RTC_MASK_BCD);
+
+	if (bcd_mode) {
+		t->tm_sec  = bcd2bin(t->tm_sec);
+		t->tm_min  = bcd2bin(t->tm_min);
+		t->tm_hour = bcd2bin(t->tm_hour);
+		t->tm_mday = bcd2bin(t->tm_mday);
+		t->tm_mon  = bcd2bin(t->tm_mon);
+		t->tm_year = bcd2bin(t->tm_year);
+	}
+
+	if (_12h_mode) {
+		/* Convert to 24h mode */
+		if (pm_mode) {
+			if (t->tm_hour < 12)
+				t->tm_hour += 12;
+		} else if (t->tm_hour == 12)
+			t->tm_hour = 0;
+	}
+
+	/* Count the months starting from 0, not from 1 */
+	t->tm_mon--;
+
+	if (t->tm_year < 100) {
+		/* tm_year is the number of years since 1900 but the
+		 * RTC epoch is 2000.
+		 */
+		epoch = 2000;
+		t->tm_year += 100;
+	}
+
+	fibril_mutex_unlock(&rtc->mutex);
+
+	return rtc_tm_sanity_check(t, epoch);
+}
+
+/** Set the time in the RTC
+ *
+ * @param fun  The RTC function
+ * @param t    The time value to set
+ *
+ * @return  EOK or a negative error code
+ */
+static int
+rtc_time_set(ddf_fun_t *fun, struct tm *t)
+{
+	int  rc;
+	bool bcd_mode;
+	int  reg_b;
+	int  reg_a;
+	int  epoch;
+	rtc_t *rtc = RTC_FROM_FNODE(fun);
+
+	fibril_mutex_lock(&rtc->mutex);
+
+	/* Detect the RTC epoch */
+	if (rtc_register_read(rtc, RTC_YEAR) < 100)
+		epoch = 2000;
+	else
+		epoch = 1900;
+
+	rc = rtc_tm_sanity_check(t, epoch);
+	if (rc != EOK) {
+		fibril_mutex_unlock(&rtc->mutex);
+		return rc;
+	}
+
+	t->tm_mon++; /* counts from 1, not from 0 */
+
+	reg_b = rtc_register_read(rtc, RTC_STATUS_B);
+
+	if (!(reg_b & RTC_MASK_24H)) {
+		/* Force 24h mode of operation */
+		reg_b |= RTC_MASK_24H;
+		rtc_register_write(rtc, RTC_STATUS_B, reg_b);
+	}
+
+	if (epoch == 2000) {
+		/* The RTC epoch is year 2000  but the tm_year
+		 * field counts years since 1900.
+		 */
+		t->tm_year -= 100;
+	}
+
+	/* Check if the rtc is working in bcd mode */
+	bcd_mode = !(reg_b & RTC_MASK_BCD);
+	if (bcd_mode) {
+		/* Convert the tm struct fields in BCD mode */
+		t->tm_sec  = bin2bcd(t->tm_sec);
+		t->tm_min  = bin2bcd(t->tm_min);
+		t->tm_hour = bin2bcd(t->tm_hour);
+		t->tm_mday = bin2bcd(t->tm_mday);
+		t->tm_mon  = bin2bcd(t->tm_mon);
+		t->tm_year = bin2bcd(t->tm_year);
+	}
+
+	/* Inhibit updates */
+	rtc_register_write(rtc, RTC_STATUS_B, reg_b | RTC_MASK_INH);
+
+	/* Write current time to RTC */
+	rtc_register_write(rtc, RTC_SEC, t->tm_sec);
+	rtc_register_write(rtc, RTC_MIN, t->tm_min);
+	rtc_register_write(rtc, RTC_HOUR, t->tm_hour);
+	rtc_register_write(rtc, RTC_DAY, t->tm_mday);
+	rtc_register_write(rtc, RTC_MON, t->tm_mon);
+	rtc_register_write(rtc, RTC_YEAR, t->tm_year);
+
+	/* Stop the clock */
+	reg_a = rtc_register_read(rtc, RTC_STATUS_A);
+	rtc_register_write(rtc, RTC_STATUS_A, RTC_MASK_CLK_STOP | reg_a);
+	
+	/* Enable updates */
+	rtc_register_write(rtc, RTC_STATUS_B, reg_b);
+	rtc_register_write(rtc, RTC_STATUS_A, reg_a);
+
+	fibril_mutex_unlock(&rtc->mutex);
+
+	return rc;
+}
+
+/** Check if the tm structure contains valid values
+ *
+ * @param t     The tm structure to check
+ * @param epoch The RTC epoch year
+ *
+ * @return      EOK on success or EINVAL
+ */
+static int
+rtc_tm_sanity_check(struct tm *t, int epoch)
+{
+	int ndays;
+
+	if (t->tm_sec < 0 || t->tm_sec > 59)
+		return EINVAL;
+	else if (t->tm_min < 0 || t->tm_min > 59)
+		return EINVAL;
+	else if (t->tm_hour < 0 || t->tm_hour > 23)
+		return EINVAL;
+	else if (t->tm_mday < 1 || t->tm_mday > 31)
+		return EINVAL;
+	else if (t->tm_mon < 0 || t->tm_mon > 11)
+		return EINVAL;
+	else if (epoch == 2000 && t->tm_year < 100)
+		return EINVAL;
+	else if (t->tm_year < 0 || t->tm_year > 199)
+		return EINVAL;
+
+	if (t->tm_mon == 1/* FEB */ && is_leap_year(t->tm_year))
+		ndays = 29;
+	else
+		ndays = days_month[t->tm_mon];
+
+	if (t->tm_mday > ndays)
+		return EINVAL;
+
+	return EOK;
+}
+
+/** Check if a year is a leap year
+ *
+ * @param year   The year to check
+ *
+ * @return       true if it is a leap year, false otherwise
+ */
+static bool
+is_leap_year(int year)
+{
+	bool r = false;
+
+	if (year % 4 == 0) {
+		if (year % 100 == 0)
+			r = year % 400 == 0;
+		else
+			r = true;
+	}
+
+	return r;
+}
+
+/** The dev_add callback of the rtc driver
+ *
+ * @param dev  The RTC device
+ *
+ * @return  EOK on success or a negative error code
+ */
+static int
+rtc_dev_add(ddf_dev_t *dev)
+{
+	rtc_t *rtc;
+	ddf_fun_t *fun = NULL;
+	int rc;
+	bool need_cleanup = false;
+
+	ddf_msg(LVL_DEBUG, "rtc_dev_add %s (handle = %d)",
+	    dev->name, (int) dev->handle);
+
+	rtc = ddf_dev_data_alloc(dev, sizeof(rtc_t));
+	if (!rtc)
+		return ENOMEM;
+
+	rtc->dev = dev;
+	fibril_mutex_initialize(&rtc->mutex);
+
+	rc = rtc_dev_initialize(rtc);
+	if (rc != EOK)
+		goto error;
+
+	need_cleanup = true;
+
+	if (!rtc_pio_enable(rtc)) {
+		rc = EADDRNOTAVAIL;
+		goto error;
+	}
+
+	fun = ddf_fun_create(dev, fun_exposed, "a");
+	if (!fun) {
+		ddf_msg(LVL_ERROR, "Failed creating function");
+		rc = ENOENT;
+		goto error;
+	}
+
+	fun->ops = &rtc_dev_ops;
+	rc = ddf_fun_bind(fun);
+	if (rc != EOK) {
+		ddf_msg(LVL_ERROR, "Failed binding function");
+		goto error;
+	}
+
+	rtc->fun = fun;
+
+	ddf_fun_add_to_category(fun, "clock");
+
+	rtc->client_connected = false;
+
+	ddf_msg(LVL_NOTE, "Device %s successfully initialized",
+	    dev->name);
+
+	return rc;
+
+error:
+	if (fun)
+		ddf_fun_destroy(fun);
+	if (need_cleanup)
+		rtc_dev_cleanup(rtc);
+	return rc;
+}
+
+/** The dev_remove callback for the rtc driver
+ *
+ * @param dev   The RTC device
+ *
+ * @return      EOK on success or a negative error code
+ */
+static int
+rtc_dev_remove(ddf_dev_t *dev)
+{
+	rtc_t *rtc = RTC_FROM_DEV(dev);
+	int rc;
+
+	fibril_mutex_lock(&rtc->mutex);
+	if (rtc->client_connected) {
+		fibril_mutex_unlock(&rtc->mutex);
+		return EBUSY;
+	}
+
+	rtc->removed = true;
+	fibril_mutex_unlock(&rtc->mutex);
+
+	rc = ddf_fun_unbind(rtc->fun);
+	if (rc != EOK) {
+		ddf_msg(LVL_ERROR, "Failed to unbind function");
+		return rc;
+	}
+
+	ddf_fun_destroy(rtc->fun);
+	rtc_dev_cleanup(rtc);
+
+	return rc;
+}
+
+/** Default handler for client requests not handled
+ *  by the standard interface
+ */
+static void
+rtc_default_handler(ddf_fun_t *fun, ipc_callid_t callid, ipc_call_t *call)
+{
+	sysarg_t method = IPC_GET_IMETHOD(*call);
+	rtc_t *rtc = RTC_FROM_FNODE(fun);
+	bool batt_ok;
+
+	switch (method) {
+	case CLOCK_GET_BATTERY_STATUS:
+		batt_ok = rtc_register_read(rtc, RTC_STATUS_D) &
+		    RTC_BATTERY_OK;
+		async_answer_1(callid, EOK, batt_ok);
+		break;
+	default:
+		async_answer_0(callid, ENOTSUP);
+	}
+}
+
+/** Open the device
+ *
+ * @param fun   The function node
+ *
+ * @return  EOK on success or a negative error code
+ */
+static int
+rtc_open(ddf_fun_t *fun)
+{
+	int rc;
+	rtc_t *rtc = RTC_FROM_FNODE(fun);
+
+	fibril_mutex_lock(&rtc->mutex);
+
+	if (rtc->client_connected)
+		rc = ELIMIT;
+	else if (rtc->removed)
+		rc = ENXIO;
+	else {
+		rc = EOK;
+		rtc->client_connected = true;
+	}
+
+	fibril_mutex_unlock(&rtc->mutex);
+	return rc;
+}
+
+/** Close the device
+ *
+ * @param fun  The function node
+ */
+static void
+rtc_close(ddf_fun_t *fun)
+{
+	rtc_t *rtc = RTC_FROM_FNODE(fun);
+
+	fibril_mutex_lock(&rtc->mutex);
+
+	assert(rtc->client_connected);
+	rtc->client_connected = false;
+
+	fibril_mutex_unlock(&rtc->mutex);
+}
+
+/** Convert from BCD mode to binary mode
+ *
+ * @param bcd   The number in BCD format to convert
+ *
+ * @return      The converted value
+ */
+static unsigned 
+bcd2bin(unsigned bcd)
+{
+	return ((bcd & 0xF0) >> 1) + ((bcd & 0xF0) >> 3) + (bcd & 0xf);
+}
+
+/** Convert from binary mode to BCD mode
+ *
+ * @param bcd   The number in binary mode to convert
+ *
+ * @return      The converted value
+ */
+static unsigned
+bin2bcd(unsigned binary)
+{
+	return ((binary / 10) << 4) + (binary % 10);
+}
+
+int
+main(int argc, char **argv)
+{
+	printf(NAME ": HelenOS RTC driver\n");
+	rtc_init();
+	return ddf_driver_main(&rtc_driver);
+}
+
+/**
+ * @}
+ */
Index: uspace/drv/time/cmos-rtc/cmos-rtc.ma
===================================================================
--- uspace/drv/time/cmos-rtc/cmos-rtc.ma	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
+++ uspace/drv/time/cmos-rtc/cmos-rtc.ma	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -0,0 +1,1 @@
+100 isa/cmos-rtc
Index: uspace/lib/c/Makefile
===================================================================
--- uspace/lib/c/Makefile	(revision 32fef47bf1b96f02c8a4ccc9728b95e42ef4624a)
+++ uspace/lib/c/Makefile	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -69,4 +69,5 @@
 	generic/device/hw_res_parsed.c \
 	generic/device/char_dev.c \
+	generic/device/clock_dev.c \
 	generic/device/nic.c \
 	generic/device/pci.c \
Index: uspace/lib/c/generic/device/clock_dev.c
===================================================================
--- uspace/lib/c/generic/device/clock_dev.c	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
+++ uspace/lib/c/generic/device/clock_dev.c	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -0,0 +1,107 @@
+/*
+ * Copyright (c) 2012 Maurizio Lombardi
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+ /** @addtogroup libc
+ * @{
+ */
+/** @file
+ */
+
+#include <ipc/dev_iface.h>
+#include <device/clock_dev.h>
+#include <errno.h>
+#include <async.h>
+#include <time.h>
+
+/** Read the current time from the device
+ *
+ * @param sess     Session of the device
+ * @param t        The current time that will be read from the device
+ *
+ * @return         EOK on success or a negative error code
+ */
+int
+clock_dev_time_get(async_sess_t *sess, struct tm *t)
+{
+	aid_t req;
+	int ret;
+
+	async_exch_t *exch = async_exchange_begin(sess);
+
+	req = async_send_1(exch, DEV_IFACE_ID(CLOCK_DEV_IFACE),
+	    CLOCK_DEV_TIME_GET, NULL);
+	ret = async_data_read_start(exch, t, sizeof(*t));
+
+	async_exchange_end(exch);
+
+	sysarg_t rc;
+	if (ret != EOK) {
+		async_wait_for(req, &rc);
+		if (rc == EOK)
+			return ret;
+	}
+
+	async_wait_for(req, &rc);
+	return (int)rc;
+}
+
+/** Set the current time
+ *
+ * @param sess   Session of the device
+ * @param t      The current time that will be written to the device
+ *
+ * @return       EOK on success or a negative error code
+ */
+int
+clock_dev_time_set(async_sess_t *sess, struct tm *t)
+{
+	aid_t req;
+	int ret;
+
+	async_exch_t *exch = async_exchange_begin(sess);
+
+	req = async_send_1(exch, DEV_IFACE_ID(CLOCK_DEV_IFACE),
+	    CLOCK_DEV_TIME_SET, NULL);
+	ret = async_data_write_start(exch, t, sizeof(*t));
+
+	async_exchange_end(exch);
+
+	sysarg_t rc;
+	if (ret != EOK) {
+		async_wait_for(req, &rc);
+		if (rc == EOK)
+			return ret;
+	}
+
+	async_wait_for(req, &rc);
+	return (int)rc;
+}
+
+/** @}
+ */
+
Index: uspace/lib/c/generic/time.c
===================================================================
--- uspace/lib/c/generic/time.c	(revision 32fef47bf1b96f02c8a4ccc9728b95e42ef4624a)
+++ uspace/lib/c/generic/time.c	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -1,4 +1,6 @@
 /*
  * Copyright (c) 2006 Ondrej Palkovsky
+ * Copyright (c) 2011 Petr Koupy
+ * Copyright (c) 2011 Jiri Zarevucky
  * All rights reserved.
  *
@@ -43,4 +45,9 @@
 #include <ddi.h>
 #include <libc.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <ctype.h>
+
+#define ASCTIME_BUF_LEN 26
 
 /** Pointer to kernel shared variables with time */
@@ -50,4 +57,337 @@
 	volatile sysarg_t seconds2;
 } *ktime = NULL;
+
+/* Helper functions ***********************************************************/
+
+#define HOURS_PER_DAY (24)
+#define MINS_PER_HOUR (60)
+#define SECS_PER_MIN (60)
+#define MINS_PER_DAY (MINS_PER_HOUR * HOURS_PER_DAY)
+#define SECS_PER_HOUR (SECS_PER_MIN * MINS_PER_HOUR)
+#define SECS_PER_DAY (SECS_PER_HOUR * HOURS_PER_DAY)
+
+/**
+ * Checks whether the year is a leap year.
+ *
+ * @param year Year since 1900 (e.g. for 1970, the value is 70).
+ * @return true if year is a leap year, false otherwise
+ */
+static bool _is_leap_year(time_t year)
+{
+	year += 1900;
+
+	if (year % 400 == 0)
+		return true;
+	if (year % 100 == 0)
+		return false;
+	if (year % 4 == 0)
+		return true;
+	return false;
+}
+
+/**
+ * Returns how many days there are in the given month of the given year.
+ * Note that year is only taken into account if month is February.
+ *
+ * @param year Year since 1900 (can be negative).
+ * @param mon Month of the year. 0 for January, 11 for December.
+ * @return Number of days in the specified month.
+ */
+static int _days_in_month(time_t year, time_t mon)
+{
+	assert(mon >= 0 && mon <= 11);
+
+	static int month_days[] =
+		{ 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
+
+	if (mon == 1) {
+		year += 1900;
+		/* february */
+		return _is_leap_year(year) ? 29 : 28;
+	} else {
+		return month_days[mon];
+	}
+}
+
+/**
+ * For specified year, month and day of month, returns which day of that year
+ * it is.
+ *
+ * For example, given date 2011-01-03, the corresponding expression is:
+ *     _day_of_year(111, 0, 3) == 2
+ *
+ * @param year Year (year 1900 = 0, can be negative).
+ * @param mon Month (January = 0).
+ * @param mday Day of month (First day is 1).
+ * @return Day of year (First day is 0).
+ */
+static int _day_of_year(time_t year, time_t mon, time_t mday)
+{
+	static int mdays[] =
+	    { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
+	static int leap_mdays[] =
+	    { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
+
+	return (_is_leap_year(year) ? leap_mdays[mon] : mdays[mon]) + mday - 1;
+}
+
+/**
+ * Integer division that rounds to negative infinity.
+ * Used by some functions in this file.
+ *
+ * @param op1 Dividend.
+ * @param op2 Divisor.
+ * @return Rounded quotient.
+ */
+static time_t _floor_div(time_t op1, time_t op2)
+{
+	if (op1 >= 0 || op1 % op2 == 0) {
+		return op1 / op2;
+	} else {
+		return op1 / op2 - 1;
+	}
+}
+
+/**
+ * Modulo that rounds to negative infinity.
+ * Used by some functions in this file.
+ *
+ * @param op1 Dividend.
+ * @param op2 Divisor.
+ * @return Remainder.
+ */
+static time_t _floor_mod(time_t op1, time_t op2)
+{
+	int div = _floor_div(op1, op2);
+
+	/* (a / b) * b + a % b == a */
+	/* thus, a % b == a - (a / b) * b */
+
+	int result = op1 - div * op2;
+	
+	/* Some paranoid checking to ensure I didn't make a mistake here. */
+	assert(result >= 0);
+	assert(result < op2);
+	assert(div * op2 + result == op1);
+	
+	return result;
+}
+
+/**
+ * Number of days since the Epoch.
+ * Epoch is 1970-01-01, which is also equal to day 0.
+ *
+ * @param year Year (year 1900 = 0, may be negative).
+ * @param mon Month (January = 0).
+ * @param mday Day of month (first day = 1).
+ * @return Number of days since the Epoch.
+ */
+static time_t _days_since_epoch(time_t year, time_t mon, time_t mday)
+{
+	return (year - 70) * 365 + _floor_div(year - 69, 4) -
+	    _floor_div(year - 1, 100) + _floor_div(year + 299, 400) +
+	    _day_of_year(year, mon, mday);
+}
+
+/**
+ * Seconds since the Epoch. see also _days_since_epoch().
+ * 
+ * @param tm Normalized broken-down time.
+ * @return Number of seconds since the epoch, not counting leap seconds.
+ */
+static time_t _secs_since_epoch(const struct tm *tm)
+{
+	return _days_since_epoch(tm->tm_year, tm->tm_mon, tm->tm_mday) *
+	    SECS_PER_DAY + tm->tm_hour * SECS_PER_HOUR +
+	    tm->tm_min * SECS_PER_MIN + tm->tm_sec;
+}
+
+/**
+ * Which day of week the specified date is.
+ * 
+ * @param year Year (year 1900 = 0).
+ * @param mon Month (January = 0).
+ * @param mday Day of month (first = 1).
+ * @return Day of week (Sunday = 0).
+ */
+static int _day_of_week(time_t year, time_t mon, time_t mday)
+{
+	/* 1970-01-01 is Thursday */
+	return _floor_mod((_days_since_epoch(year, mon, mday) + 4), 7);
+}
+
+/**
+ * Normalizes the broken-down time and optionally adds specified amount of
+ * seconds.
+ * 
+ * @param tm Broken-down time to normalize.
+ * @param sec_add Seconds to add.
+ * @return 0 on success, -1 on overflow
+ */
+static int _normalize_time(struct tm *tm, time_t sec_add)
+{
+	// TODO: DST correction
+
+	/* Set initial values. */
+	time_t sec = tm->tm_sec + sec_add;
+	time_t min = tm->tm_min;
+	time_t hour = tm->tm_hour;
+	time_t day = tm->tm_mday - 1;
+	time_t mon = tm->tm_mon;
+	time_t year = tm->tm_year;
+
+	/* Adjust time. */
+	min += _floor_div(sec, SECS_PER_MIN);
+	sec = _floor_mod(sec, SECS_PER_MIN);
+	hour += _floor_div(min, MINS_PER_HOUR);
+	min = _floor_mod(min, MINS_PER_HOUR);
+	day += _floor_div(hour, HOURS_PER_DAY);
+	hour = _floor_mod(hour, HOURS_PER_DAY);
+
+	/* Adjust month. */
+	year += _floor_div(mon, 12);
+	mon = _floor_mod(mon, 12);
+
+	/* Now the difficult part - days of month. */
+	
+	/* First, deal with whole cycles of 400 years = 146097 days. */
+	year += _floor_div(day, 146097) * 400;
+	day = _floor_mod(day, 146097);
+	
+	/* Then, go in one year steps. */
+	if (mon <= 1) {
+		/* January and February. */
+		while (day > 365) {
+			day -= _is_leap_year(year) ? 366 : 365;
+			year++;
+		}
+	} else {
+		/* Rest of the year. */
+		while (day > 365) {
+			day -= _is_leap_year(year + 1) ? 366 : 365;
+			year++;
+		}
+	}
+	
+	/* Finally, finish it off month per month. */
+	while (day >= _days_in_month(year, mon)) {
+		day -= _days_in_month(year, mon);
+		mon++;
+		if (mon >= 12) {
+			mon -= 12;
+			year++;
+		}
+	}
+	
+	/* Calculate the remaining two fields. */
+	tm->tm_yday = _day_of_year(year, mon, day + 1);
+	tm->tm_wday = _day_of_week(year, mon, day + 1);
+	
+	/* And put the values back to the struct. */
+	tm->tm_sec = (int) sec;
+	tm->tm_min = (int) min;
+	tm->tm_hour = (int) hour;
+	tm->tm_mday = (int) day + 1;
+	tm->tm_mon = (int) mon;
+	
+	/* Casts to work around libc brain-damage. */
+	if (year > ((int)INT_MAX) || year < ((int)INT_MIN)) {
+		tm->tm_year = (year < 0) ? ((int)INT_MIN) : ((int)INT_MAX);
+		return -1;
+	}
+	
+	tm->tm_year = (int) year;
+	return 0;
+}
+
+/**
+ * Which day the week-based year starts on, relative to the first calendar day.
+ * E.g. if the year starts on December 31st, the return value is -1.
+ *
+ * @param Year since 1900.
+ * @return Offset of week-based year relative to calendar year.
+ */
+static int _wbyear_offset(int year)
+{
+	int start_wday = _day_of_week(year, 0, 1);
+	return _floor_mod(4 - start_wday, 7) - 3;
+}
+
+/**
+ * Returns week-based year of the specified time.
+ *
+ * @param tm Normalized broken-down time.
+ * @return Week-based year.
+ */
+static int _wbyear(const struct tm *tm)
+{
+	int day = tm->tm_yday - _wbyear_offset(tm->tm_year);
+	if (day < 0) {
+		/* Last week of previous year. */
+		return tm->tm_year - 1;
+	}
+	if (day > 364 + _is_leap_year(tm->tm_year)) {
+		/* First week of next year. */
+		return tm->tm_year + 1;
+	}
+	/* All the other days are in the calendar year. */
+	return tm->tm_year;
+}
+
+/**
+ * Week number of the year, assuming weeks start on sunday.
+ * The first Sunday of January is the first day of week 1;
+ * days in the new year before this are in week 0.
+ *
+ * @param tm Normalized broken-down time.
+ * @return The week number (0 - 53).
+ */
+static int _sun_week_number(const struct tm *tm)
+{
+	int first_day = (7 - _day_of_week(tm->tm_year, 0, 1)) % 7;
+	return (tm->tm_yday - first_day + 7) / 7;
+}
+
+/**
+ * Week number of the year, assuming weeks start on monday.
+ * If the week containing January 1st has four or more days in the new year,
+ * then it is considered week 1. Otherwise, it is the last week of the previous
+ * year, and the next week is week 1. Both January 4th and the first Thursday
+ * of January are always in week 1.
+ *
+ * @param tm Normalized broken-down time.
+ * @return The week number (1 - 53).
+ */
+static int _iso_week_number(const struct tm *tm)
+{
+	int day = tm->tm_yday - _wbyear_offset(tm->tm_year);
+	if (day < 0) {
+		/* Last week of previous year. */
+		return 53;
+	}
+	if (day > 364 + _is_leap_year(tm->tm_year)) {
+		/* First week of next year. */
+		return 1;
+	}
+	/* All the other days give correct answer. */
+	return (day / 7 + 1);
+}
+
+/**
+ * Week number of the year, assuming weeks start on monday.
+ * The first Monday of January is the first day of week 1;
+ * days in the new year before this are in week 0. 
+ *
+ * @param tm Normalized broken-down time.
+ * @return The week number (0 - 53).
+ */
+static int _mon_week_number(const struct tm *tm)
+{
+	int first_day = (1 - _day_of_week(tm->tm_year, 0, 1)) % 7;
+	return (tm->tm_yday - first_day + 7) / 7;
+}
+
+/******************************************************************************/
+
 
 /** Add microseconds to given timeval.
@@ -228,4 +568,324 @@
 }
 
+/**
+ * This function first normalizes the provided broken-down time
+ * (moves all values to their proper bounds) and then tries to
+ * calculate the appropriate time_t representation.
+ *
+ * @param tm Broken-down time.
+ * @return time_t representation of the time, undefined value on overflow.
+ */
+time_t mktime(struct tm *tm)
+{
+	// TODO: take DST flag into account
+	// TODO: detect overflow
+
+	_normalize_time(tm, 0);
+	return _secs_since_epoch(tm);
+}
+
+/**
+ * Convert time and date to a string, based on a specified format and
+ * current locale.
+ * 
+ * @param s Buffer to write string to.
+ * @param maxsize Size of the buffer.
+ * @param format Format of the output.
+ * @param tm Broken-down time to format.
+ * @return Number of bytes written.
+ */
+size_t strftime(char *restrict s, size_t maxsize,
+    const char *restrict format, const struct tm *restrict tm)
+{
+	assert(s != NULL);
+	assert(format != NULL);
+	assert(tm != NULL);
+
+	// TODO: use locale
+	static const char *wday_abbr[] = {
+		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
+	};
+	static const char *wday[] = {
+		"Sunday", "Monday", "Tuesday", "Wednesday",
+		"Thursday", "Friday", "Saturday"
+	};
+	static const char *mon_abbr[] = {
+		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
+		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
+	};
+	static const char *mon[] = {
+		"January", "February", "March", "April", "May", "June", "July",
+		"August", "September", "October", "November", "December"
+	};
+	
+	if (maxsize < 1) {
+		return 0;
+	}
+	
+	char *ptr = s;
+	size_t consumed;
+	size_t remaining = maxsize;
+	
+	#define append(...) { \
+		/* FIXME: this requires POSIX-correct snprintf */ \
+		/*        otherwise it won't work with non-ascii chars */ \
+		consumed = snprintf(ptr, remaining, __VA_ARGS__); \
+		if (consumed >= remaining) { \
+			return 0; \
+		} \
+		ptr += consumed; \
+		remaining -= consumed; \
+	}
+	
+	#define recurse(fmt) { \
+		consumed = strftime(ptr, remaining, fmt, tm); \
+		if (consumed == 0) { \
+			return 0; \
+		} \
+		ptr += consumed; \
+		remaining -= consumed; \
+	}
+	
+	#define TO_12H(hour) (((hour) > 12) ? ((hour) - 12) : \
+	    (((hour) == 0) ? 12 : (hour)))
+	
+	while (*format != '\0') {
+		if (*format != '%') {
+			append("%c", *format);
+			format++;
+			continue;
+		}
+		
+		format++;
+		if (*format == '0' || *format == '+') {
+			// TODO: padding
+			format++;
+		}
+		while (isdigit(*format)) {
+			// TODO: padding
+			format++;
+		}
+		if (*format == 'O' || *format == 'E') {
+			// TODO: locale's alternative format
+			format++;
+		}
+		
+		switch (*format) {
+		case 'a':
+			append("%s", wday_abbr[tm->tm_wday]); break;
+		case 'A':
+			append("%s", wday[tm->tm_wday]); break;
+		case 'b':
+			append("%s", mon_abbr[tm->tm_mon]); break;
+		case 'B':
+			append("%s", mon[tm->tm_mon]); break;
+		case 'c':
+			// TODO: locale-specific datetime format
+			recurse("%Y-%m-%d %H:%M:%S"); break;
+		case 'C':
+			append("%02d", (1900 + tm->tm_year) / 100); break;
+		case 'd':
+			append("%02d", tm->tm_mday); break;
+		case 'D':
+			recurse("%m/%d/%y"); break;
+		case 'e':
+			append("%2d", tm->tm_mday); break;
+		case 'F':
+			recurse("%+4Y-%m-%d"); break;
+		case 'g':
+			append("%02d", _wbyear(tm) % 100); break;
+		case 'G':
+			append("%d", _wbyear(tm)); break;
+		case 'h':
+			recurse("%b"); break;
+		case 'H':
+			append("%02d", tm->tm_hour); break;
+		case 'I':
+			append("%02d", TO_12H(tm->tm_hour)); break;
+		case 'j':
+			append("%03d", tm->tm_yday); break;
+		case 'k':
+			append("%2d", tm->tm_hour); break;
+		case 'l':
+			append("%2d", TO_12H(tm->tm_hour)); break;
+		case 'm':
+			append("%02d", tm->tm_mon); break;
+		case 'M':
+			append("%02d", tm->tm_min); break;
+		case 'n':
+			append("\n"); break;
+		case 'p':
+			append("%s", tm->tm_hour < 12 ? "AM" : "PM"); break;
+		case 'P':
+			append("%s", tm->tm_hour < 12 ? "am" : "PM"); break;
+		case 'r':
+			recurse("%I:%M:%S %p"); break;
+		case 'R':
+			recurse("%H:%M"); break;
+		case 's':
+			append("%ld", _secs_since_epoch(tm)); break;
+		case 'S':
+			append("%02d", tm->tm_sec); break;
+		case 't':
+			append("\t"); break;
+		case 'T':
+			recurse("%H:%M:%S"); break;
+		case 'u':
+			append("%d", (tm->tm_wday == 0) ? 7 : tm->tm_wday);
+			break;
+		case 'U':
+			append("%02d", _sun_week_number(tm)); break;
+		case 'V':
+			append("%02d", _iso_week_number(tm)); break;
+		case 'w':
+			append("%d", tm->tm_wday); break;
+		case 'W':
+			append("%02d", _mon_week_number(tm)); break;
+		case 'x':
+			// TODO: locale-specific date format
+			recurse("%Y-%m-%d"); break;
+		case 'X':
+			// TODO: locale-specific time format
+			recurse("%H:%M:%S"); break;
+		case 'y':
+			append("%02d", tm->tm_year % 100); break;
+		case 'Y':
+			append("%d", 1900 + tm->tm_year); break;
+		case 'z':
+			// TODO: timezone
+			break;
+		case 'Z':
+			// TODO: timezone
+			break;
+		case '%':
+			append("%%");
+			break;
+		default:
+			/* Invalid specifier, print verbatim. */
+			while (*format != '%') {
+				format--;
+			}
+			append("%%");
+			break;
+		}
+		format++;
+	}
+	
+	#undef append
+	#undef recurse
+	
+	return maxsize - remaining;
+}
+
+struct tm *gmtime(const time_t *timer)
+{
+	assert(timer != NULL);
+
+	static struct tm result;
+
+	/* Set result to epoch. */
+	result.tm_sec = 0;
+	result.tm_min = 0;
+	result.tm_hour = 0;
+	result.tm_mday = 1;
+	result.tm_mon = 0;
+	result.tm_year = 70; /* 1970 */
+
+	if (_normalize_time(&result, *timer) == -1) {
+		errno = EOVERFLOW;
+		return NULL;
+	}
+
+	return &result;
+}
+
+/**
+ * Converts broken-down time to a string in format
+ * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
+ *
+ * @param timeptr Broken-down time structure.
+ * @return Pointer to a statically allocated string.
+ */
+char *asctime(const struct tm *timeptr)
+{
+	static char buf[ASCTIME_BUF_LEN];
+
+	assert(timeptr != NULL);
+
+	static const char *wday[] = {
+		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
+	};
+	static const char *mon[] = {
+		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
+		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
+	};
+
+	snprintf(buf, ASCTIME_BUF_LEN, "%s %s %2d %02d:%02d:%02d %d\n",
+	    wday[timeptr->tm_wday],
+	    mon[timeptr->tm_mon],
+	    timeptr->tm_mday, timeptr->tm_hour,
+	    timeptr->tm_min, timeptr->tm_sec,
+	    1900 + timeptr->tm_year);
+
+	return buf;
+
+}
+
+/**
+ * Converts a time value to a broken-down local time.
+ *
+ * @param timer Time to convert.
+ * @return Normalized broken-down time in local timezone, NULL on overflow.
+ */
+struct tm *localtime(const time_t *timer)
+{
+	// TODO: deal with timezone
+	// currently assumes system and all times are in GMT
+
+	static struct tm result;
+
+	/* Set result to epoch. */
+	result.tm_sec = 0;
+	result.tm_min = 0;
+	result.tm_hour = 0;
+	result.tm_mday = 1;
+	result.tm_mon = 0;
+	result.tm_year = 70; /* 1970 */
+
+	if (_normalize_time(&result, *timer) == -1) {
+		errno = EOVERFLOW;
+		return NULL;
+	}
+
+	return &result;
+}
+
+/**
+ * Equivalent to asctime(localtime(clock)).
+ * 
+ * @param timer Time to convert.
+ * @return Pointer to a statically allocated string holding the date.
+ */
+char *ctime(const time_t *timer)
+{
+	struct tm *loctime = localtime(timer);
+	if (loctime == NULL) {
+		return NULL;
+	}
+	return asctime(loctime);
+}
+
+/**
+ * Calculate the difference between two times, in seconds.
+ * 
+ * @param time1 First time.
+ * @param time0 Second time.
+ * @return Time in seconds.
+ */
+double difftime(time_t time1, time_t time0)
+{
+	return (double) (time1 - time0);
+}
+
 /** @}
  */
Index: uspace/lib/c/include/device/clock_dev.h
===================================================================
--- uspace/lib/c/include/device/clock_dev.h	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
+++ uspace/lib/c/include/device/clock_dev.h	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2012 Maurizio Lombardi
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup libc
+ * @{
+ */
+/** @file
+ */
+
+#ifndef LIBC_DEVICE_CLOCK_DEV_H_
+#define LIBC_DEVICE_CLOCK_DEV_H_
+
+#include <async.h>
+#include <time.h>
+
+typedef enum {
+	CLOCK_DEV_TIME_GET = 0,
+	CLOCK_DEV_TIME_SET,
+} clock_dev_method_t;
+
+extern int clock_dev_time_get(async_sess_t *, struct tm *);
+extern int clock_dev_time_set(async_sess_t *, struct tm *);
+
+#endif
+
Index: uspace/lib/c/include/ipc/clock_ctl.h
===================================================================
--- uspace/lib/c/include/ipc/clock_ctl.h	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
+++ uspace/lib/c/include/ipc/clock_ctl.h	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2012 Maurizio Lombardi
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef LIBC_IPC_CLOCK_CTL_H_
+#define LIBC_IPC_CLOCK_CTL_H_
+
+#include <ipc/dev_iface.h>
+
+typedef enum {
+	CLOCK_GET_BATTERY_STATUS = DEV_FIRST_CUSTOM_METHOD,
+} clock_ctl_t;
+
+#endif
+
Index: uspace/lib/c/include/ipc/dev_iface.h
===================================================================
--- uspace/lib/c/include/ipc/dev_iface.h	(revision 32fef47bf1b96f02c8a4ccc9728b95e42ef4624a)
+++ uspace/lib/c/include/ipc/dev_iface.h	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -52,4 +52,7 @@
 	USBHID_DEV_IFACE,
 
+	/** Interface provided by Real Time Clock devices */
+	CLOCK_DEV_IFACE,
+
 	DEV_IFACE_MAX
 } dev_inferface_idx_t;
Index: uspace/lib/c/include/sys/time.h
===================================================================
--- uspace/lib/c/include/sys/time.h	(revision 32fef47bf1b96f02c8a4ccc9728b95e42ef4624a)
+++ uspace/lib/c/include/sys/time.h	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -1,4 +1,6 @@
 /*
  * Copyright (c) 2006 Ondrej Palkovsky
+ * Copyright (c) 2011 Petr Koupy
+ * Copyright (c) 2011 Jiri Zarevucky
  * All rights reserved.
  *
@@ -39,4 +41,5 @@
 
 #define DST_NONE 0
+#define ASCTIME_BUF_LEN 26
 
 typedef long time_t;
@@ -45,4 +48,16 @@
 typedef uint32_t useconds_t;
 typedef uint32_t mseconds_t;
+
+struct tm {
+	int tm_sec;         /* Seconds [0,60]. */
+	int tm_min;         /* Minutes [0,59]. */
+	int tm_hour;        /* Hour [0,23]. */
+	int tm_mday;        /* Day of month [1,31]. */
+	int tm_mon;         /* Month of year [0,11]. */
+	int tm_year;        /* Years since 1900. */
+	int tm_wday;        /* Day of week [0,6] (Sunday = 0). */
+	int tm_yday;        /* Day of year [0,365]. */
+	int tm_isdst;       /* Daylight Savings flag. */
+};
 
 struct timeval {
@@ -64,4 +79,13 @@
 extern void udelay(useconds_t);
 
+extern time_t mktime(struct tm *tm);
+extern struct tm *gmtime(const time_t *timer);
+extern char *asctime(const struct tm *timeptr);
+extern struct tm *localtime(const time_t *timer);
+extern char *ctime(const time_t *timer);
+extern double difftime(time_t time1, time_t time0);
+extern size_t strftime(char *restrict s, size_t maxsize,
+    const char *restrict format, const struct tm *restrict tm);
+
 #endif
 
Index: uspace/lib/drv/Makefile
===================================================================
--- uspace/lib/drv/Makefile	(revision 32fef47bf1b96f02c8a4ccc9728b95e42ef4624a)
+++ uspace/lib/drv/Makefile	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -44,5 +44,6 @@
 	generic/remote_pci.c \
 	generic/remote_usbhc.c \
-	generic/remote_usbhid.c
+	generic/remote_usbhid.c \
+	generic/remote_clock_dev.c
 
 include $(USPACE_PREFIX)/Makefile.common
Index: uspace/lib/drv/generic/dev_iface.c
===================================================================
--- uspace/lib/drv/generic/dev_iface.c	(revision 32fef47bf1b96f02c8a4ccc9728b95e42ef4624a)
+++ uspace/lib/drv/generic/dev_iface.c	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -41,4 +41,5 @@
 #include "remote_hw_res.h"
 #include "remote_char_dev.h"
+#include "remote_clock_dev.h"
 #include "remote_nic.h"
 #include "remote_usb.h"
@@ -55,5 +56,6 @@
 		&remote_usb_iface,
 		&remote_usbhc_iface,
-		&remote_usbhid_iface
+		&remote_usbhid_iface,
+		&remote_clock_dev_iface,
 	}
 };
Index: uspace/lib/drv/generic/remote_clock_dev.c
===================================================================
--- uspace/lib/drv/generic/remote_clock_dev.c	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
+++ uspace/lib/drv/generic/remote_clock_dev.c	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -0,0 +1,143 @@
+/*
+ * Copyright (c) 2012 Maurizio Lombardi
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup libdrv
+ * @{
+ */
+/** @file
+ */
+
+#include <async.h>
+#include <errno.h>
+#include <time.h>
+
+#include <ops/clock_dev.h>
+#include <ddf/driver.h>
+
+static void remote_clock_time_get(ddf_fun_t *, void *, ipc_callid_t,
+    ipc_call_t *);
+static void remote_clock_time_set(ddf_fun_t *, void *, ipc_callid_t,
+    ipc_call_t *);
+
+/** Remote clock interface operations */
+static remote_iface_func_ptr_t remote_clock_dev_iface_ops[] = {
+	&remote_clock_time_get,
+	&remote_clock_time_set,
+};
+
+/** Remote clock interface structure
+ *
+ * Interface for processing requests from remote clients
+ * addressed by the clock interface.
+ */
+remote_iface_t remote_clock_dev_iface = {
+	.method_count = sizeof(remote_clock_dev_iface_ops) /
+	    sizeof(remote_iface_func_ptr_t),
+	.methods = remote_clock_dev_iface_ops,
+};
+
+/** Process the time_get() request from the remote client
+ *
+ * @param fun   The function from which the time is read
+ * @param ops   The local ops structure
+ */
+static void
+remote_clock_time_get(ddf_fun_t *fun, void *ops, ipc_callid_t callid,
+    ipc_call_t *call)
+{
+	clock_dev_ops_t *clock_dev_ops = (clock_dev_ops_t *) ops;
+	ipc_callid_t cid;
+	struct tm t;
+	int rc;
+	size_t len;
+
+	if (!async_data_read_receive(&cid, &len)) {
+		/* TODO: Handle protocol error */
+		async_answer_0(callid, EINVAL);
+		return;
+	}
+
+	if (!clock_dev_ops->time_get) {
+		/* The driver does not provide the time_get() functionality */
+		async_data_read_finalize(cid, NULL, 0);
+		async_answer_0(callid, ENOTSUP);
+		return;
+	}
+
+	rc = (*clock_dev_ops->time_get)(fun, &t);
+
+	if (rc != EOK) {
+		/* Some error occurred */
+		async_data_read_finalize(cid, NULL, 0);
+		async_answer_0(callid, rc);
+		return;
+	}
+
+	/* The operation was successful */
+	async_data_read_finalize(cid, &t, sizeof(struct tm));
+	async_answer_0(callid, rc);
+}
+
+/** Process the write request from the remote client
+ *
+ * @param fun   The function to which the data are written
+ * @param ops   The local ops structure
+ */
+static void remote_clock_time_set(ddf_fun_t *fun, void *ops,
+    ipc_callid_t callid, ipc_call_t *call)
+{
+	clock_dev_ops_t *clock_dev_ops = (clock_dev_ops_t *) ops;
+	int          rc;
+	struct tm    t;
+	ipc_callid_t cid;
+	size_t       len;
+
+	if (!async_data_write_receive(&cid, &len)) {
+		/* TODO: Handle protocol error */
+		async_answer_0(callid, EINVAL);
+		return;
+	}
+
+	if (!clock_dev_ops->time_set) {
+		/* The driver does not support the time_set() functionality */
+		async_data_write_finalize(cid, NULL, 0);
+		async_answer_0(callid, ENOTSUP);
+		return;
+	}
+
+	async_data_write_finalize(cid, &t, sizeof(struct tm));
+
+	rc = (*clock_dev_ops->time_set)(fun, &t);
+
+	async_answer_0(callid, rc);
+}
+
+/**
+ * @}
+ */
+
Index: uspace/lib/drv/include/ops/clock_dev.h
===================================================================
--- uspace/lib/drv/include/ops/clock_dev.h	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
+++ uspace/lib/drv/include/ops/clock_dev.h	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2012 Maurizio Lombardi
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup libdrv
+ * @{
+ */
+/** @file
+ */
+
+#ifndef LIBDRV_OPS_CLOCK_DEV_H_
+#define LIBDRV_OPS_CLOCK_DEV_H_
+
+#include <time.h>
+#include "../ddf/driver.h"
+
+typedef struct {
+	int (*time_get)(ddf_fun_t *, struct tm *);
+	int (*time_set)(ddf_fun_t *, struct tm *);
+} clock_dev_ops_t;
+
+#endif
+
+/**
+ * @}
+ */
Index: uspace/lib/drv/include/remote_clock_dev.h
===================================================================
--- uspace/lib/drv/include/remote_clock_dev.h	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
+++ uspace/lib/drv/include/remote_clock_dev.h	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2012 Maurizio Lombardi
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup libdrv
+ * @{
+ */
+/** @file
+ */
+
+#ifndef LIBDRV_REMOTE_CLOCK_DEV_H_
+#define LIBDRV_REMOTE_CLOCK_DEV_H_
+
+extern remote_iface_t remote_clock_dev_iface;
+
+#endif
+
+/**
+ * @}
+ */
+
Index: uspace/lib/posix/time.c
===================================================================
--- uspace/lib/posix/time.c	(revision 32fef47bf1b96f02c8a4ccc9728b95e42ef4624a)
+++ uspace/lib/posix/time.c	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -140,5 +140,5 @@
  * Used by some functions in this file.
  *
- * @param op1 Divident.
+ * @param op1 Dividend.
  * @param op2 Divisor.
  * @return Rounded quotient.
@@ -157,5 +157,5 @@
  * Used by some functions in this file.
  *
- * @param op1 Divident.
+ * @param op1 Dividend.
  * @param op2 Divisor.
  * @return Remainder.
@@ -195,17 +195,4 @@
 
 /**
- * Seconds since the Epoch. see also _days_since_epoch().
- * 
- * @param tm Normalized broken-down time.
- * @return Number of seconds since the epoch, not counting leap seconds.
- */
-static time_t _secs_since_epoch(const struct posix_tm *tm)
-{
-	return _days_since_epoch(tm->tm_year, tm->tm_mon, tm->tm_mday) *
-	    SECS_PER_DAY + tm->tm_hour * SECS_PER_HOUR +
-	    tm->tm_min * SECS_PER_MIN + tm->tm_sec;
-}
-
-/**
  * Which day of week the specified date is.
  * 
@@ -229,5 +216,5 @@
  * @return 0 on success, -1 on overflow
  */
-static int _normalize_time(struct posix_tm *tm, time_t sec_add)
+static int _normalize_time(struct tm *tm, time_t sec_add)
 {
 	// TODO: DST correction
@@ -305,91 +292,4 @@
 }
 
-/**
- * Which day the week-based year starts on, relative to the first calendar day.
- * E.g. if the year starts on December 31st, the return value is -1.
- *
- * @param Year since 1900.
- * @return Offset of week-based year relative to calendar year.
- */
-static int _wbyear_offset(int year)
-{
-	int start_wday = _day_of_week(year, 0, 1);
-	return _floor_mod(4 - start_wday, 7) - 3;
-}
-
-/**
- * Returns week-based year of the specified time.
- *
- * @param tm Normalized broken-down time.
- * @return Week-based year.
- */
-static int _wbyear(const struct posix_tm *tm)
-{
-	int day = tm->tm_yday - _wbyear_offset(tm->tm_year);
-	if (day < 0) {
-		/* Last week of previous year. */
-		return tm->tm_year - 1;
-	}
-	if (day > 364 + _is_leap_year(tm->tm_year)) {
-		/* First week of next year. */
-		return tm->tm_year + 1;
-	}
-	/* All the other days are in the calendar year. */
-	return tm->tm_year;
-}
-
-/**
- * Week number of the year, assuming weeks start on sunday.
- * The first Sunday of January is the first day of week 1;
- * days in the new year before this are in week 0.
- *
- * @param tm Normalized broken-down time.
- * @return The week number (0 - 53).
- */
-static int _sun_week_number(const struct posix_tm *tm)
-{
-	int first_day = (7 - _day_of_week(tm->tm_year, 0, 1)) % 7;
-	return (tm->tm_yday - first_day + 7) / 7;
-}
-
-/**
- * Week number of the year, assuming weeks start on monday.
- * If the week containing January 1st has four or more days in the new year,
- * then it is considered week 1. Otherwise, it is the last week of the previous
- * year, and the next week is week 1. Both January 4th and the first Thursday
- * of January are always in week 1.
- *
- * @param tm Normalized broken-down time.
- * @return The week number (1 - 53).
- */
-static int _iso_week_number(const struct posix_tm *tm)
-{
-	int day = tm->tm_yday - _wbyear_offset(tm->tm_year);
-	if (day < 0) {
-		/* Last week of previous year. */
-		return 53;
-	}
-	if (day > 364 + _is_leap_year(tm->tm_year)) {
-		/* First week of next year. */
-		return 1;
-	}
-	/* All the other days give correct answer. */
-	return (day / 7 + 1);
-}
-
-/**
- * Week number of the year, assuming weeks start on monday.
- * The first Monday of January is the first day of week 1;
- * days in the new year before this are in week 0. 
- *
- * @param tm Normalized broken-down time.
- * @return The week number (0 - 53).
- */
-static int _mon_week_number(const struct posix_tm *tm)
-{
-	int first_day = (1 - _day_of_week(tm->tm_year, 0, 1)) % 7;
-	return (tm->tm_yday - first_day + 7) / 7;
-}
-
 /******************************************************************************/
 
@@ -411,47 +311,4 @@
 
 /**
- * Calculate the difference between two times, in seconds.
- * 
- * @param time1 First time.
- * @param time0 Second time.
- * @return Time in seconds.
- */
-double posix_difftime(time_t time1, time_t time0)
-{
-	return (double) (time1 - time0);
-}
-
-/**
- * This function first normalizes the provided broken-down time
- * (moves all values to their proper bounds) and then tries to
- * calculate the appropriate time_t representation.
- *
- * @param tm Broken-down time.
- * @return time_t representation of the time, undefined value on overflow.
- */
-time_t posix_mktime(struct posix_tm *tm)
-{
-	// TODO: take DST flag into account
-	// TODO: detect overflow
-
-	_normalize_time(tm, 0);
-	return _secs_since_epoch(tm);
-}
-
-/**
- * Converts a time value to a broken-down UTC time.
- *
- * @param timer Time to convert.
- * @return Normalized broken-down time in UTC, NULL on overflow.
- */
-struct posix_tm *posix_gmtime(const time_t *timer)
-{
-	assert(timer != NULL);
-
-	static struct posix_tm result;
-	return posix_gmtime_r(timer, &result);
-}
-
-/**
  * Converts a time value to a broken-down UTC time.
  * 
@@ -460,6 +317,6 @@
  * @return Value of result on success, NULL on overflow.
  */
-struct posix_tm *posix_gmtime_r(const time_t *restrict timer,
-    struct posix_tm *restrict result)
+struct tm *posix_gmtime_r(const time_t *restrict timer,
+    struct tm *restrict result)
 {
 	assert(timer != NULL);
@@ -484,16 +341,4 @@
 /**
  * Converts a time value to a broken-down local time.
- *
- * @param timer Time to convert.
- * @return Normalized broken-down time in local timezone, NULL on overflow.
- */
-struct posix_tm *posix_localtime(const time_t *timer)
-{
-	static struct posix_tm result;
-	return posix_localtime_r(timer, &result);
-}
-
-/**
- * Converts a time value to a broken-down local time.
  * 
  * @param timer Time to convert.
@@ -501,23 +346,10 @@
  * @return Value of result on success, NULL on overflow.
  */
-struct posix_tm *posix_localtime_r(const time_t *restrict timer,
-    struct posix_tm *restrict result)
+struct tm *posix_localtime_r(const time_t *restrict timer,
+    struct tm *restrict result)
 {
 	// TODO: deal with timezone
 	// currently assumes system and all times are in GMT
 	return posix_gmtime_r(timer, result);
-}
-
-/**
- * Converts broken-down time to a string in format
- * "Sun Jan 1 00:00:00 1970\n". (Obsolete)
- *
- * @param timeptr Broken-down time structure.
- * @return Pointer to a statically allocated string.
- */
-char *posix_asctime(const struct posix_tm *timeptr)
-{
-	static char buf[ASCTIME_BUF_LEN];
-	return posix_asctime_r(timeptr, buf);
 }
 
@@ -531,5 +363,5 @@
  * @return Value of buf.
  */
-char *posix_asctime_r(const struct posix_tm *restrict timeptr,
+char *posix_asctime_r(const struct tm *restrict timeptr,
     char *restrict buf)
 {
@@ -556,19 +388,4 @@
 
 /**
- * Equivalent to asctime(localtime(clock)).
- * 
- * @param timer Time to convert.
- * @return Pointer to a statically allocated string holding the date.
- */
-char *posix_ctime(const time_t *timer)
-{
-	struct posix_tm *loctime = posix_localtime(timer);
-	if (loctime == NULL) {
-		return NULL;
-	}
-	return posix_asctime(loctime);
-}
-
-/**
  * Reentrant variant of ctime().
  * 
@@ -580,201 +397,9 @@
 char *posix_ctime_r(const time_t *timer, char *buf)
 {
-	struct posix_tm loctime;
+	struct tm loctime;
 	if (posix_localtime_r(timer, &loctime) == NULL) {
 		return NULL;
 	}
 	return posix_asctime_r(&loctime, buf);
-}
-
-/**
- * Convert time and date to a string, based on a specified format and
- * current locale.
- * 
- * @param s Buffer to write string to.
- * @param maxsize Size of the buffer.
- * @param format Format of the output.
- * @param tm Broken-down time to format.
- * @return Number of bytes written.
- */
-size_t posix_strftime(char *restrict s, size_t maxsize,
-    const char *restrict format, const struct posix_tm *restrict tm)
-{
-	assert(s != NULL);
-	assert(format != NULL);
-	assert(tm != NULL);
-
-	// TODO: use locale
-	static const char *wday_abbr[] = {
-		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
-	};
-	static const char *wday[] = {
-		"Sunday", "Monday", "Tuesday", "Wednesday",
-		"Thursday", "Friday", "Saturday"
-	};
-	static const char *mon_abbr[] = {
-		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
-		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
-	};
-	static const char *mon[] = {
-		"January", "February", "March", "April", "May", "June", "July",
-		"August", "September", "October", "November", "December"
-	};
-	
-	if (maxsize < 1) {
-		return 0;
-	}
-	
-	char *ptr = s;
-	size_t consumed;
-	size_t remaining = maxsize;
-	
-	#define append(...) { \
-		/* FIXME: this requires POSIX-correct snprintf */ \
-		/*        otherwise it won't work with non-ascii chars */ \
-		consumed = snprintf(ptr, remaining, __VA_ARGS__); \
-		if (consumed >= remaining) { \
-			return 0; \
-		} \
-		ptr += consumed; \
-		remaining -= consumed; \
-	}
-	
-	#define recurse(fmt) { \
-		consumed = posix_strftime(ptr, remaining, fmt, tm); \
-		if (consumed == 0) { \
-			return 0; \
-		} \
-		ptr += consumed; \
-		remaining -= consumed; \
-	}
-	
-	#define TO_12H(hour) (((hour) > 12) ? ((hour) - 12) : \
-	    (((hour) == 0) ? 12 : (hour)))
-	
-	while (*format != '\0') {
-		if (*format != '%') {
-			append("%c", *format);
-			format++;
-			continue;
-		}
-		
-		format++;
-		if (*format == '0' || *format == '+') {
-			// TODO: padding
-			format++;
-		}
-		while (isdigit(*format)) {
-			// TODO: padding
-			format++;
-		}
-		if (*format == 'O' || *format == 'E') {
-			// TODO: locale's alternative format
-			format++;
-		}
-		
-		switch (*format) {
-		case 'a':
-			append("%s", wday_abbr[tm->tm_wday]); break;
-		case 'A':
-			append("%s", wday[tm->tm_wday]); break;
-		case 'b':
-			append("%s", mon_abbr[tm->tm_mon]); break;
-		case 'B':
-			append("%s", mon[tm->tm_mon]); break;
-		case 'c':
-			// TODO: locale-specific datetime format
-			recurse("%Y-%m-%d %H:%M:%S"); break;
-		case 'C':
-			append("%02d", (1900 + tm->tm_year) / 100); break;
-		case 'd':
-			append("%02d", tm->tm_mday); break;
-		case 'D':
-			recurse("%m/%d/%y"); break;
-		case 'e':
-			append("%2d", tm->tm_mday); break;
-		case 'F':
-			recurse("%+4Y-%m-%d"); break;
-		case 'g':
-			append("%02d", _wbyear(tm) % 100); break;
-		case 'G':
-			append("%d", _wbyear(tm)); break;
-		case 'h':
-			recurse("%b"); break;
-		case 'H':
-			append("%02d", tm->tm_hour); break;
-		case 'I':
-			append("%02d", TO_12H(tm->tm_hour)); break;
-		case 'j':
-			append("%03d", tm->tm_yday); break;
-		case 'k':
-			append("%2d", tm->tm_hour); break;
-		case 'l':
-			append("%2d", TO_12H(tm->tm_hour)); break;
-		case 'm':
-			append("%02d", tm->tm_mon); break;
-		case 'M':
-			append("%02d", tm->tm_min); break;
-		case 'n':
-			append("\n"); break;
-		case 'p':
-			append("%s", tm->tm_hour < 12 ? "AM" : "PM"); break;
-		case 'P':
-			append("%s", tm->tm_hour < 12 ? "am" : "PM"); break;
-		case 'r':
-			recurse("%I:%M:%S %p"); break;
-		case 'R':
-			recurse("%H:%M"); break;
-		case 's':
-			append("%ld", _secs_since_epoch(tm)); break;
-		case 'S':
-			append("%02d", tm->tm_sec); break;
-		case 't':
-			append("\t"); break;
-		case 'T':
-			recurse("%H:%M:%S"); break;
-		case 'u':
-			append("%d", (tm->tm_wday == 0) ? 7 : tm->tm_wday); break;
-		case 'U':
-			append("%02d", _sun_week_number(tm)); break;
-		case 'V':
-			append("%02d", _iso_week_number(tm)); break;
-		case 'w':
-			append("%d", tm->tm_wday); break;
-		case 'W':
-			append("%02d", _mon_week_number(tm)); break;
-		case 'x':
-			// TODO: locale-specific date format
-			recurse("%Y-%m-%d"); break;
-		case 'X':
-			// TODO: locale-specific time format
-			recurse("%H:%M:%S"); break;
-		case 'y':
-			append("%02d", tm->tm_year % 100); break;
-		case 'Y':
-			append("%d", 1900 + tm->tm_year); break;
-		case 'z':
-			// TODO: timezone
-			break;
-		case 'Z':
-			// TODO: timezone
-			break;
-		case '%':
-			append("%%");
-			break;
-		default:
-			/* Invalid specifier, print verbatim. */
-			while (*format != '%') {
-				format--;
-			}
-			append("%%");
-			break;
-		}
-		format++;
-	}
-	
-	#undef append
-	#undef recurse
-	
-	return maxsize - remaining;
 }
 
@@ -893,5 +518,6 @@
 	stats_task_t *task_stats = stats_get_task(task_get_id());
 	if (task_stats) {
-		total_cycles = (posix_clock_t) (task_stats->kcycles + task_stats->ucycles);
+		total_cycles = (posix_clock_t) (task_stats->kcycles +
+		    task_stats->ucycles);
 		free(task_stats);
 		task_stats = 0;
Index: uspace/lib/posix/time.h
===================================================================
--- uspace/lib/posix/time.h	(revision 32fef47bf1b96f02c8a4ccc9728b95e42ef4624a)
+++ uspace/lib/posix/time.h	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -63,21 +63,6 @@
 #endif
 
-#undef ASCTIME_BUF_LEN
-#define ASCTIME_BUF_LEN 26
-
 #undef CLOCK_REALTIME
 #define CLOCK_REALTIME ((posix_clockid_t) 0)
-
-struct posix_tm {
-	int tm_sec;         /* Seconds [0,60]. */
-	int tm_min;         /* Minutes [0,59]. */
-	int tm_hour;        /* Hour [0,23]. */
-	int tm_mday;        /* Day of month [1,31]. */
-	int tm_mon;         /* Month of year [0,11]. */
-	int tm_year;        /* Years since 1900. */
-	int tm_wday;        /* Day of week [0,6] (Sunday = 0). */
-	int tm_yday;        /* Day of year [0,365]. */
-	int tm_isdst;       /* Daylight Savings flag. */
-};
 
 struct posix_timespec {
@@ -99,24 +84,14 @@
 extern void posix_tzset(void);
 
-/* Elapsed Time */
-extern double posix_difftime(time_t time1, time_t time0);
-
 /* Broken-down Time */
-extern time_t posix_mktime(struct posix_tm *tm);
-extern struct posix_tm *posix_gmtime(const time_t *timer);
-extern struct posix_tm *posix_gmtime_r(const time_t *restrict timer,
-    struct posix_tm *restrict result);
-extern struct posix_tm *posix_localtime(const time_t *timer);
-extern struct posix_tm *posix_localtime_r(const time_t *restrict timer,
-    struct posix_tm *restrict result);
+extern struct tm *posix_gmtime_r(const time_t *restrict timer,
+    struct tm *restrict result);
+extern struct tm *posix_localtime_r(const time_t *restrict timer,
+    struct tm *restrict result);
 
 /* Formatting Calendar Time */
-extern char *posix_asctime(const struct posix_tm *timeptr);
-extern char *posix_asctime_r(const struct posix_tm *restrict timeptr,
+extern char *posix_asctime_r(const struct tm *restrict timeptr,
     char *restrict buf);
-extern char *posix_ctime(const time_t *timer);
 extern char *posix_ctime_r(const time_t *timer, char *buf);
-extern size_t posix_strftime(char *restrict s, size_t maxsize,
-    const char *restrict format, const struct posix_tm *restrict tm);
 
 /* Clocks */
@@ -134,5 +109,4 @@
 
 #ifndef LIBPOSIX_INTERNAL
-	#define tm posix_tm
 	#define timespec posix_timespec
 	#define itimerspec posix_itimerspec
@@ -144,17 +118,9 @@
 	#define tzset posix_tzset
 
-	#define difftime posix_difftime
-
-	#define mktime posix_mktime
-	#define gmtime posix_gmtime
 	#define gmtime_r posix_gmtime_r
-	#define localtime posix_localtime
 	#define localtime_r posix_localtime_r
 
-	#define asctime posix_asctime
 	#define asctime_r posix_asctime_r
-	#define ctime posix_ctime
 	#define ctime_r posix_ctime_r
-	#define strftime posix_strftime
 
 	#define clock_getres posix_clock_getres
Index: uspace/srv/loc/loc.c
===================================================================
--- uspace/srv/loc/loc.c	(revision 32fef47bf1b96f02c8a4ccc9728b95e42ef4624a)
+++ uspace/srv/loc/loc.c	(revision 7719958acb56b8b553bd6f0bfe042be335827596)
@@ -1308,4 +1308,7 @@
 	categ_dir_add_cat(&cdir, cat);
 
+	cat = category_new("clock");
+	categ_dir_add_cat(&cdir, cat);
+
 	cat = category_new("test3");
 	categ_dir_add_cat(&cdir, cat);
