timelocal.c revision 73359
1/*- 2 * Copyright (c) 2001 Alexey Zelkin 3 * Copyright (c) 1997 FreeBSD Inc. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD: head/lib/libc/stdtime/timelocal.c 73359 2001-03-02 22:10:04Z ache $ 28 */ 29 30#include <stddef.h> 31 32#include "ldpart.h" 33#include "timelocal.h" 34 35static struct lc_time_T _time_locale; 36static int _time_using_locale; 37static char * time_locale_buf; 38 39#define LCTIME_SIZE_FULL (sizeof(struct lc_time_T) / sizeof(char *)) 40#define LCTIME_SIZE_MIN \ 41 (offsetof(struct lc_time_T, ampm_fmt) / sizeof(char *)) 42 43static const struct lc_time_T _C_time_locale = { 44 { 45 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 46 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 47 }, { 48 "January", "February", "March", "April", "May", "June", 49 "July", "August", "September", "October", "November", "December" 50 }, { 51 "Sun", "Mon", "Tue", "Wed", 52 "Thu", "Fri", "Sat" 53 }, { 54 "Sunday", "Monday", "Tuesday", "Wednesday", 55 "Thursday", "Friday", "Saturday" 56 }, 57 58 /* X_fmt */ 59 "%H:%M:%S", 60 61 /* 62 ** x_fmt 63 ** Since the C language standard calls for 64 ** "date, using locale's date format," anything goes. 65 ** Using just numbers (as here) makes Quakers happier; 66 ** it's also compatible with SVR4. 67 */ 68 "%m/%d/%y", 69 70 /* 71 ** c_fmt (ctime-compatible) 72 */ 73 "%a %Ef %T %Y", 74 75 /* am */ 76 "AM", 77 78 /* pm */ 79 "PM", 80 81 /* date_fmt */ 82 "%a %Ef %X %Z %Y", 83 84 { 85 "January", "February", "March", "April", "May", "June", 86 "July", "August", "September", "October", "November", "December" 87 }, 88 89 /* Ef_fmt 90 ** To determine short months / day order 91 */ 92 "%b %e", 93 94 /* EF_fmt 95 ** To determine long months / day order 96 */ 97 "%B %e", 98 99 /* ampm_fmt 100 ** To determine 12-hour clock format time (empty, if N/A) 101 */ 102 "%I:%M:%S %p" 103}; 104 105struct lc_time_T * 106__get_current_time_locale(void) { 107 return (_time_using_locale 108 ? &_time_locale 109 : (struct lc_time_T *)&_C_time_locale); 110} 111 112int 113__time_load_locale(const char *name) { 114 115 int ret; 116 117 _time_locale.ampm_fmt = _C_time_locale.ampm_fmt; 118 119 ret = __part_load_locale(name, &_time_using_locale, 120 time_locale_buf, "LC_TIME", 121 LCTIME_SIZE_FULL, LCTIME_SIZE_MIN, 122 (const char **)&_time_locale); 123 124 /* XXX: always overwrite for ctime format parsing compatibility */ 125 _time_locale.c_fmt = _C_time_locale.c_fmt; 126 127 return (ret); 128} 129