timelocal.c revision 235785
1296177Sjhibbits/*- 2296177Sjhibbits * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org> 3296177Sjhibbits * Copyright (c) 1997 FreeBSD Inc. 4296177Sjhibbits * All rights reserved. 5296177Sjhibbits * 6296177Sjhibbits * Copyright (c) 2011 The FreeBSD Foundation 7296177Sjhibbits * All rights reserved. 8296177Sjhibbits * Portions of this software were developed by David Chisnall 9296177Sjhibbits * under sponsorship from the FreeBSD Foundation. 10296177Sjhibbits * 11296177Sjhibbits * Redistribution and use in source and binary forms, with or without 12296177Sjhibbits * modification, are permitted provided that the following conditions 13296177Sjhibbits * are met: 14296177Sjhibbits * 1. Redistributions of source code must retain the above copyright 15296177Sjhibbits * notice, this list of conditions and the following disclaimer. 16296177Sjhibbits * 2. Redistributions in binary form must reproduce the above copyright 17296177Sjhibbits * notice, this list of conditions and the following disclaimer in the 18296177Sjhibbits * documentation and/or other materials provided with the distribution. 19296177Sjhibbits * 20296177Sjhibbits * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21296177Sjhibbits * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22296177Sjhibbits * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23296177Sjhibbits * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24296177Sjhibbits * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25296177Sjhibbits * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26296177Sjhibbits * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27296177Sjhibbits * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28296177Sjhibbits * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29296177Sjhibbits * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30296177Sjhibbits * SUCH DAMAGE. 31296177Sjhibbits */ 32296177Sjhibbits 33296177Sjhibbits#include <sys/cdefs.h> 34296177Sjhibbits__FBSDID("$FreeBSD: stable/9/lib/libc/stdtime/timelocal.c 235785 2012-05-22 14:40:39Z theraven $"); 35296177Sjhibbits 36296177Sjhibbits#include <stddef.h> 37296177Sjhibbits 38296177Sjhibbits#include "ldpart.h" 39296177Sjhibbits#include "timelocal.h" 40296177Sjhibbits 41296177Sjhibbitsstruct xlocale_time { 42296177Sjhibbits struct xlocale_component header; 43296177Sjhibbits char *buffer; 44296177Sjhibbits struct lc_time_T locale; 45296177Sjhibbits}; 46296177Sjhibbits 47296177Sjhibbitsstruct xlocale_time __xlocale_global_time; 48296177Sjhibbits 49296177Sjhibbits#define LCTIME_SIZE (sizeof(struct lc_time_T) / sizeof(char *)) 50296177Sjhibbits 51296177Sjhibbitsstatic const struct lc_time_T _C_time_locale = { 52296177Sjhibbits { 53296177Sjhibbits "Jan", "Feb", "Mar", "Apr", "May", "Jun", 54296177Sjhibbits "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 55296177Sjhibbits }, { 56296177Sjhibbits "January", "February", "March", "April", "May", "June", 57296177Sjhibbits "July", "August", "September", "October", "November", "December" 58296177Sjhibbits }, { 59296177Sjhibbits "Sun", "Mon", "Tue", "Wed", 60296177Sjhibbits "Thu", "Fri", "Sat" 61296177Sjhibbits }, { 62 "Sunday", "Monday", "Tuesday", "Wednesday", 63 "Thursday", "Friday", "Saturday" 64 }, 65 66 /* X_fmt */ 67 "%H:%M:%S", 68 69 /* 70 * x_fmt 71 * Since the C language standard calls for 72 * "date, using locale's date format," anything goes. 73 * Using just numbers (as here) makes Quakers happier; 74 * it's also compatible with SVR4. 75 */ 76 "%m/%d/%y", 77 78 /* 79 * c_fmt 80 */ 81 "%a %b %e %H:%M:%S %Y", 82 83 /* am */ 84 "AM", 85 86 /* pm */ 87 "PM", 88 89 /* date_fmt */ 90 "%a %b %e %H:%M:%S %Z %Y", 91 92 /* alt_month 93 * Standalone months forms for %OB 94 */ 95 { 96 "January", "February", "March", "April", "May", "June", 97 "July", "August", "September", "October", "November", "December" 98 }, 99 100 /* md_order 101 * Month / day order in dates 102 */ 103 "md", 104 105 /* ampm_fmt 106 * To determine 12-hour clock format time (empty, if N/A) 107 */ 108 "%I:%M:%S %p" 109}; 110 111static void destruct_time(void *v) 112{ 113 struct xlocale_time *l = v; 114 if (l->buffer) 115 free(l->buffer); 116 free(l); 117} 118 119#include <stdio.h> 120struct lc_time_T * 121__get_current_time_locale(locale_t loc) 122{ 123 return (loc->using_time_locale 124 ? &((struct xlocale_time *)loc->components[XLC_TIME])->locale 125 : (struct lc_time_T *)&_C_time_locale); 126} 127 128static int 129time_load_locale(struct xlocale_time *l, int *using_locale, const char *name) 130{ 131 struct lc_time_T *time_locale = &l->locale; 132 return (__part_load_locale(name, using_locale, 133 &l->buffer, "LC_TIME", 134 LCTIME_SIZE, LCTIME_SIZE, 135 (const char **)time_locale)); 136} 137int 138__time_load_locale(const char *name) 139{ 140 return time_load_locale(&__xlocale_global_time, 141 &__xlocale_global_locale.using_time_locale, name); 142} 143void* __time_load(const char* name, locale_t loc) 144{ 145 struct xlocale_time *new = calloc(sizeof(struct xlocale_time), 1); 146 new->header.header.destructor = destruct_time; 147 if (time_load_locale(new, &loc->using_time_locale, name) == _LDP_ERROR) 148 { 149 xlocale_release(new); 150 return NULL; 151 } 152 return new; 153} 154 155