1/*
2 * Copyright 2004-2007, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 * Copyright 2010, Oliver Tappe, zooey@hirschkaefer.de. All rights reserved.
4 * Distributed under the terms of the MIT License.
5 */
6
7
8#include <time.h>
9#include <stdio.h>
10
11#include "PosixLCTimeInfo.h"
12
13
14using BPrivate::Libroot::gPosixLCTimeInfo;
15
16
17static char*
18print_time(char* buffer, size_t bufferSize, const struct tm* tm)
19{
20	snprintf(buffer, bufferSize, "%.3s %.3s%3d %02d:%02d:%02d %d\n",
21		tm->tm_wday < 0 ? "???" : gPosixLCTimeInfo.wday[tm->tm_wday % 7],
22		tm->tm_mon < 0 ? "???" : gPosixLCTimeInfo.mon[tm->tm_mon % 12],
23		tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
24		1900 + tm->tm_year);
25
26	return buffer;
27}
28
29
30extern "C" char*
31asctime(const struct tm* tm)
32{
33	static char buffer[26];
34		// That's enough to hold normal dates (i.e. with 4-digit years), for any
35		// other dates the behaviour of asctime() is undefined according to the
36		// POSIX Base Specifications Issue 7.
37
38	return print_time(buffer, sizeof(buffer), tm);
39}
40
41
42extern "C" char*
43asctime_r(const struct tm* tm, char* buffer)
44{
45	return print_time(buffer, 26, tm);
46		// 26 bytes seems to be required by the standard, so we can't write more
47}
48