Index: uspace/lib/posix/locale.c
===================================================================
--- uspace/lib/posix/locale.c	(revision 08053f7123977444e2f85f150a548e9fc13f8473)
+++ uspace/lib/posix/locale.c	(revision 324d46b796a17e2bb13119a062bb6626158f8afa)
@@ -42,5 +42,5 @@
 #include "string.h"
 
-struct _posix_locale {
+struct __posix_locale {
 	int _dummy;
 };
@@ -83,5 +83,5 @@
 }
 
-struct posix_lconv *localeconv(void)
+struct posix_lconv *posix_localeconv(void)
 {
 	// TODO
@@ -95,10 +95,10 @@
 		return NULL;
 	}
-	posix_locale_t copy = malloc(sizeof(struct _posix_locale));
+	posix_locale_t copy = malloc(sizeof(struct __posix_locale));
 	if (copy == NULL) {
 		errno = ENOMEM;
 		return NULL;
 	}
-	memcpy(copy, locobj, sizeof(struct _posix_locale));
+	memcpy(copy, locobj, sizeof(struct __posix_locale));
 	return copy;
 }
@@ -118,5 +118,5 @@
 	}
 	// TODO
-	posix_locale_t new = malloc(sizeof(struct _posix_locale));
+	posix_locale_t new = malloc(sizeof(struct __posix_locale));
 	if (new == NULL) {
 		errno = ENOMEM;
Index: uspace/lib/posix/locale.h
===================================================================
--- uspace/lib/posix/locale.h	(revision 08053f7123977444e2f85f150a548e9fc13f8473)
+++ uspace/lib/posix/locale.h	(revision 324d46b796a17e2bb13119a062bb6626158f8afa)
@@ -38,4 +38,12 @@
 #ifndef NULL
 	#define NULL ((void *) 0)
+#endif
+
+#ifndef __locale_t_defined
+	#define __locale_t_defined
+	typedef struct __posix_locale *posix_locale_t;
+	#ifndef LIBPOSIX_INTERNAL
+		#define locale_t posix_locale_t
+	#endif
 #endif
 
@@ -101,8 +109,6 @@
 };
 
-typedef struct _posix_locale *posix_locale_t;
-
 extern char *posix_setlocale(int category, const char *locale);
-extern struct posix_lconv *localeconv(void);
+extern struct posix_lconv *posix_localeconv(void);
 
 /* POSIX Extensions */
@@ -115,5 +121,4 @@
 #ifndef LIBPOSIX_INTERNAL
 	#define lconv posix_lconv
-	#define locale_t posix_locale_t
 
 	#define setlocale posix_setlocale
Index: uspace/lib/posix/time.c
===================================================================
--- uspace/lib/posix/time.c	(revision 08053f7123977444e2f85f150a548e9fc13f8473)
+++ uspace/lib/posix/time.c	(revision 324d46b796a17e2bb13119a062bb6626158f8afa)
@@ -36,10 +36,294 @@
 #define LIBPOSIX_INTERNAL
 
+/* Must be first. */
+#include "stdbool.h"
+
 #include "internal/common.h"
 #include "time.h"
+
+#include "ctype.h"
+#include "errno.h"
 
 #include "libc/malloc.h"
 #include "libc/task.h"
 #include "libc/stats.h"
+
+// TODO: documentation
+// TODO: test everything in this file
+
+/* 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)
+
+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;
+}
+
+static int _days_in_month(time_t year, time_t mon)
+{
+	assert(mon >= 0 && mon <= 11);
+	year += 1900;
+
+	static int month_days[] =
+		{ 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
+
+	if (mon == 1) {
+		/* february */
+		return _is_leap_year(year) ? 29 : 28;
+	} else {
+		return month_days[mon];
+	}
+}
+
+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.
+ */
+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.
+ */
+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;
+}
+
+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);
+}
+
+/* Assumes normalized broken-down time. */
+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;
+}
+
+static int _day_of_week(time_t year, time_t mon, time_t mday)
+{
+	/* 1970-01-01 is Thursday */
+	return (_days_since_epoch(year, mon, mday) + 4) % 7;
+}
+
+struct _long_tm {
+	time_t tm_sec;
+	time_t tm_min;
+	time_t tm_hour;
+	time_t tm_mday;
+	time_t tm_mon;
+	time_t tm_year;
+	int tm_wday;
+	int tm_yday;
+	int tm_isdst;
+};
+
+static void _posix_to_long_tm(struct _long_tm *ltm, struct posix_tm *ptm)
+{
+	assert(ltm != NULL && ptm != NULL);
+	ltm->tm_sec = ptm->tm_sec;
+	ltm->tm_min = ptm->tm_min;
+	ltm->tm_hour = ptm->tm_hour;
+	ltm->tm_mday = ptm->tm_mday;
+	ltm->tm_mon = ptm->tm_mon;
+	ltm->tm_year = ptm->tm_year;
+	ltm->tm_wday = ptm->tm_wday;
+	ltm->tm_yday = ptm->tm_yday;
+	ltm->tm_isdst = ptm->tm_isdst;
+}
+
+static void _long_to_posix_tm(struct posix_tm *ptm, struct _long_tm *ltm)
+{
+	assert(ltm != NULL && ptm != NULL);
+	// FIXME: the cast should be unnecessary, libarch/common.h brain-damage
+	assert((ltm->tm_year >= (int) INT_MIN) && (ltm->tm_year <= (int) INT_MAX));
+
+	ptm->tm_sec = ltm->tm_sec;
+	ptm->tm_min = ltm->tm_min;
+	ptm->tm_hour = ltm->tm_hour;
+	ptm->tm_mday = ltm->tm_mday;
+	ptm->tm_mon = ltm->tm_mon;
+	ptm->tm_year = ltm->tm_year;
+	ptm->tm_wday = ltm->tm_wday;
+	ptm->tm_yday = ltm->tm_yday;
+	ptm->tm_isdst = ltm->tm_isdst;
+}
+
+static void _normalize_time(struct _long_tm *tm)
+{
+	// TODO: DST correction
+
+	/* Adjust time. */
+	tm->tm_min += _floor_div(tm->tm_sec, SECS_PER_MIN);
+	tm->tm_sec = _floor_mod(tm->tm_sec, SECS_PER_MIN);
+	tm->tm_hour += _floor_div(tm->tm_min, MINS_PER_HOUR);
+	tm->tm_min = _floor_mod(tm->tm_min, MINS_PER_HOUR);
+	tm->tm_mday += _floor_div(tm->tm_hour, HOURS_PER_DAY);
+	tm->tm_hour = _floor_mod(tm->tm_hour, HOURS_PER_DAY);
+
+	/* Adjust month. */
+	tm->tm_year += _floor_div(tm->tm_mon, 12);
+	tm->tm_mon = _floor_mod(tm->tm_mon, 12);
+
+	/* Now the difficult part - days of month. */
+	/* Slow, but simple. */
+	// TODO: do this faster
+
+	while (tm->tm_mday < 1) {
+		tm->tm_mon--;
+		if (tm->tm_mon == -1) {
+			tm->tm_mon = 11;
+			tm->tm_year--;
+		}
+		
+		tm->tm_mday += _days_in_month(tm->tm_year, tm->tm_mon);
+	}
+
+	while (tm->tm_mday > _days_in_month(tm->tm_year, tm->tm_mon)) {
+		tm->tm_mday -= _days_in_month(tm->tm_year, tm->tm_mon);
+		
+		tm->tm_mon++;
+		if (tm->tm_mon == 12) {
+			tm->tm_mon = 0;
+			tm->tm_year++;
+		}
+	}
+
+	/* Calculate the remaining two fields. */
+	tm->tm_yday = _day_of_year(tm->tm_year, tm->tm_mon, tm->tm_mday);
+	tm->tm_wday = _day_of_week(tm->tm_year, tm->tm_mon, tm->tm_mday);
+}
+
+/* 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.
+ */
+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.
+ * Assumes normalized broken-down time.
+ */
+static int _wbyear(const struct posix_tm *tm)
+{
+	if (tm->tm_yday < _wbyear_offset(tm->tm_year)) {
+		return tm->tm_year - 1;
+	}
+	if (tm->tm_yday > (364 + _is_leap_year(tm->tm_year) +
+	    _wbyear_offset(tm->tm_year + 1))) {
+		return tm->tm_year + 1;
+	}
+	return tm->tm_year;
+}
+
+/* Number of week in year, when week starts on sunday.
+ */
+static int _sun_week_number(const struct posix_tm *tm)
+{
+	// TODO
+	not_implemented();
+}
+
+/* Number of week in week-based year.
+ */
+static int _iso_week_number(const struct posix_tm *tm)
+{
+	// TODO
+	not_implemented();
+}
+
+/* Number of week in year, when week starts on monday.
+ */
+static int _mon_week_number(const struct posix_tm *tm)
+{
+	// TODO
+	not_implemented();
+}
+
+/******************************************************************************/
+
+int posix_daylight;
+long posix_timezone;
+char *posix_tzname[2];
+
+void posix_tzset(void)
+{
+	// TODO: read environment
+	posix_tzname[0] = (char *) "GMT";
+	posix_tzname[1] = (char *) "GMT";
+	posix_daylight = 0;
+	posix_timezone = 0;
+}
+
+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 timeptr 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
+
+	struct _long_tm ltm;
+	_posix_to_long_tm(&ltm, tm);
+	_normalize_time(&ltm);
+	_long_to_posix_tm(tm, &ltm);
+
+	return _secs_since_epoch(tm);
+}
 
 /**
@@ -50,7 +334,37 @@
 struct posix_tm *posix_localtime(const time_t *timep)
 {
-	// TODO
-	static struct posix_tm result = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
-	return &result;
+	static struct posix_tm result;
+	return posix_localtime_r(timep, &result);
+}
+
+struct posix_tm *posix_localtime_r(const time_t *restrict timer,
+    struct posix_tm *restrict result)
+{
+	assert(timer != NULL);
+	assert(result != NULL);
+
+	// TODO: deal with timezone
+	// currently assumes system and all times are in GMT
+
+	/* Set epoch and seconds to _long_tm struct and normalize to get
+	 * correct values.
+	 */
+	struct _long_tm ltm = {
+		.tm_sec = *timer,
+		.tm_min = 0,
+		.tm_hour = 0, /* 00:00:xx */
+		.tm_mday = 1,
+		.tm_mon = 0, /* January 1st */
+		.tm_year = 70, /* 1970 */
+	};
+	_normalize_time(&ltm);
+
+	if (ltm.tm_year < (int) INT_MIN || ltm.tm_year > (int) INT_MAX) {
+		errno = EOVERFLOW;
+		return NULL;
+	}
+
+	_long_to_posix_tm(result, &ltm);
+	return result;
 }
 
@@ -60,9 +374,32 @@
  * @return
  */
-char *posix_asctime(const struct posix_tm *tm)
-{
-	// TODO
-	static char result[] = "Sun Jan 01 00:00:00 1900\n";
-	return result;
+char *posix_asctime(const struct posix_tm *timeptr)
+{
+	static char buf[ASCTIME_BUF_LEN];
+	return posix_asctime_r(timeptr, buf);
+}
+
+char *posix_asctime_r(const struct posix_tm *restrict timeptr,
+    char *restrict buf)
+{
+	assert(timeptr != NULL);
+	assert(buf != 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;
 }
 
@@ -85,11 +422,180 @@
  * @return
  */
-size_t posix_strftime(char *s, size_t maxsize, const char *format, const struct posix_tm *tm)
-{
-	// TODO
-	if (maxsize >= 1) {
-		*s = '\0';
-	}
-	return 0;
+size_t posix_strftime(char *s, size_t maxsize,
+    const char *format, const struct posix_tm *tm)
+{
+	// 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;
 }
 
Index: uspace/lib/posix/time.h
===================================================================
--- uspace/lib/posix/time.h	(revision 08053f7123977444e2f85f150a548e9fc13f8473)
+++ uspace/lib/posix/time.h	(revision 324d46b796a17e2bb13119a062bb6626158f8afa)
@@ -38,4 +38,5 @@
 
 #include "libc/time.h"
+#include "sys/types.h"
 
 #ifndef NULL
@@ -43,6 +44,21 @@
 #endif
 
-#undef CLOCKS_PER_SEC
-#define CLOCKS_PER_SEC 1000000L
+#ifndef CLOCKS_PER_SEC
+	#define CLOCKS_PER_SEC (1000000L)
+#endif
+
+#ifndef __locale_t_defined
+	#define __locale_t_defined
+	typedef struct __posix_locale *posix_locale_t;
+	#ifndef LIBPOSIX_INTERNAL
+		#define locale_t posix_locale_t
+	#endif
+#endif
+
+#undef ASCTIME_BUF_LEN
+#define ASCTIME_BUF_LEN 26
+
+#undef CLOCK_REALTIME
+#define CLOCK_REALTIME ((posix_clockid_t) 0)
 
 struct posix_tm {
@@ -58,16 +74,45 @@
 };
 
+// FIXME: should be in sys/types.h
 typedef long posix_clock_t;
 
+struct posix_timespec {
+	time_t tv_sec; /* Seconds. */
+	long tv_nsec; /* Nanoseconds. */
+};
+
+struct posix_itimerspec {
+	struct posix_timespec it_interval; /* Timer period. */
+	struct posix_timespec it_value; /* Timer expiration. */
+};
+
+/* Timezones */
+
+extern int posix_daylight;
+extern long posix_timezone;
+extern char *posix_tzname[2];
+
+extern void posix_tzset(void);
+
+/* time_t */
+
+extern double posix_difftime(time_t time1, time_t time0);
+
 /* Broken-down Time */
+extern time_t posix_mktime(struct posix_tm *timeptr);
 extern struct posix_tm *posix_localtime(const time_t *timep);
-
+extern struct posix_tm *posix_localtime_r(const time_t *restrict timer,
+    struct posix_tm *restrict result);
 /* Formatting Calendar Time */
-extern char *posix_asctime(const struct posix_tm *tm);
+extern char *posix_asctime(const struct posix_tm *timeptr);
+extern char *posix_asctime_r(const struct posix_tm *restrict timeptr,
+    char *restrict buf);
 extern char *posix_ctime(const time_t *timep);
-extern size_t posix_strftime(char *restrict s, size_t maxsize, const char *restrict format, const struct posix_tm *restrict tm);
+extern size_t posix_strftime(char *restrict s, size_t maxsize,
+    const char *restrict format, const struct posix_tm *restrict tm);
 
 /* CPU Time */
 extern posix_clock_t posix_clock(void);
+
 
 #ifndef LIBPOSIX_INTERNAL
@@ -75,8 +120,19 @@
 
 	#define clock_t posix_clock_t
+	#define timespec posix_timespec
+	#define itimerspec posix_itimerspec
 
+	#define difftime posix_difftime
+	#define mktime posix_mktime
 	#define localtime posix_localtime
+	#define localtime_r posix_localtime_r
+
+	#define daylight posix_daylight
+	#define timezone posix_timezone
+	#define tzname posix_tzname
+	#define tzset posix_tzset
 
 	#define asctime posix_asctime
+	#define asctime_r posix_asctime_r
 	#define ctime posix_ctime
 	#define strftime posix_strftime
