1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2001,2008 Oracle.  All rights reserved.
5 *
6 * $Id: ctime.c,v 1.8 2008/01/08 20:58:44 bostic Exp $
7 */
8
9#include "db_config.h"
10
11#include "db_int.h"
12
13static void  __os_brew_ct_numb __P((char *, int));
14
15/*
16 * __os_ctime --
17 *	Format a time-stamp.
18 */
19char *
20__os_ctime(tod, time_buf)
21	const time_t *tod;
22	char *time_buf;
23{
24	JulianType jt;
25	time_t tt;
26	char *ncp;
27
28	strcpy(time_buf, "Thu Jan 01 00:00:00 1970");
29	time_buf[CTIME_BUFLEN - 1] = '\0';
30
31	/*
32	 * Berkeley DB uses POSIX time values internally, convert to a BREW
33	 * time value.
34	 */
35	tt = *tod - BREW_EPOCH_OFFSET + LOCALTIMEOFFSET(NULL);
36	GETJULIANDATE(tt, &jt);
37
38	/*
39	 * wWeekDay : Day of the week 0-6 (0=Monday, 6=Sunday)
40	 */
41	ncp = &"MonTueWedThuFriSatSun"[jt.wWeekDay*3];
42	time_buf[0] = *ncp++;
43	time_buf[1] = *ncp++;
44	time_buf[2] = *ncp;
45	ncp = &"JanFebMarAprMayJunJulAugSepOctNovDec"[(jt.wMonth - 1) * 3];
46	time_buf[4] = *ncp++;
47	time_buf[5] = *ncp++;
48	time_buf[6] = *ncp;
49
50	__os_brew_ct_numb(time_buf + 8, jt.wDay);
51					/* Add 100 to keep the leading zero. */
52	__os_brew_ct_numb(time_buf + 11, jt.wHour + 100);
53	__os_brew_ct_numb(time_buf + 14, jt.wMinute + 100);
54	__os_brew_ct_numb(time_buf + 17, jt.wSecond + 100);
55
56	if (jt.wYear < 100) {		/* 9 99 */
57		time_buf[20] = ' ';
58		time_buf[21] = ' ';
59		__os_brew_ct_numb(time_buf + 22, jt.wYear);
60	} else {			/* 99 1999 */
61		__os_brew_ct_numb(time_buf + 20, jt.wYear / 100);
62		__os_brew_ct_numb(time_buf + 22, jt.wYear % 100 + 100);
63	}
64
65	return (time_buf);
66}
67
68/*
69 * __os_brew_ct_numb --
70 *	Append ASCII representations for two digits to a string.
71 */
72static void
73__os_brew_ct_numb(cp, n)
74	char *cp;
75	int n;
76{
77	cp[0] = ' ';
78	if (n >= 10)
79		cp[0] = (n / 10) % 10 + '0';
80	cp[1] = n % 10 + '0';
81}
82