1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2001,2008 Oracle.  All rights reserved.
5 *
6 * $Id: ce_ctime.c,v 12.6 2008/01/08 20:58:46 bostic Exp $
7 */
8
9#include "db_config.h"
10
11#include "db_int.h"
12
13static void  __os_windows_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	char *ncp;
25	__int64 i64_tod;
26	struct _FILETIME file_tod, file_loc;
27	struct _SYSTEMTIME sys_loc;
28static const __int64 SECS_BETWEEN_EPOCHS = 11644473600;
29static const __int64 SECS_TO_100NS = 10000000; /* 10^7 */
30
31	strcpy(time_buf, "Thu Jan 01 00:00:00 1970");
32	time_buf[CTIME_BUFLEN - 1] = '\0';
33
34	/* Convert the tod to a SYSTEM_TIME struct */
35	i64_tod = *tod;
36	i64_tod = (i64_tod + SECS_BETWEEN_EPOCHS)*SECS_TO_100NS;
37	memcpy(&file_tod, &i64_tod, sizeof(file_tod));
38	FileTimeToLocalFileTime(&file_tod, &file_loc);
39	FileTimeToSystemTime(&file_loc, &sys_loc);
40
41	/*
42	 * Convert the _SYSTEMTIME to the correct format in time_buf.
43	 * Based closely on the os_brew/ctime.c implementation.
44	 *
45	 * wWeekDay : Day of the week 0-6 (0=Monday, 6=Sunday)
46	 */
47	ncp = &"MonTueWedThuFriSatSun"[sys_loc.wDayOfWeek*3];
48	time_buf[0] = *ncp++;
49	time_buf[1] = *ncp++;
50	time_buf[2] = *ncp;
51	ncp = &"JanFebMarAprMayJunJulAugSepOctNovDec"[(sys_loc.wMonth - 1) * 3];
52	time_buf[4] = *ncp++;
53	time_buf[5] = *ncp++;
54	time_buf[6] = *ncp;
55
56	__os_windows_ct_numb(time_buf + 8, sys_loc.wDay);
57					/* Add 100 to keep the leading zero. */
58	__os_windows_ct_numb(time_buf + 11, sys_loc.wHour + 100);
59	__os_windows_ct_numb(time_buf + 14, sys_loc.wMinute + 100);
60	__os_windows_ct_numb(time_buf + 17, sys_loc.wSecond + 100);
61
62	if (sys_loc.wYear < 100) {		/* 9 99 */
63		time_buf[20] = ' ';
64		time_buf[21] = ' ';
65		__os_windows_ct_numb(time_buf + 22, sys_loc.wYear);
66	} else {			/* 99 1999 */
67		__os_windows_ct_numb(time_buf + 20, sys_loc.wYear / 100);
68		__os_windows_ct_numb(time_buf + 22, sys_loc.wYear % 100 + 100);
69	}
70
71	return (time_buf);
72}
73
74/*
75 * __os_windows_ct_numb --
76 *	Append ASCII representations for two digits to a string.
77 */
78static void
79__os_windows_ct_numb(cp, n)
80	char *cp;
81	int n;
82{
83	cp[0] = ' ';
84	if (n >= 10)
85		cp[0] = (n / 10) % 10 + '0';
86	cp[1] = n % 10 + '0';
87}
88