/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2001 Alexey Zelkin <[email protected]>4* Copyright (c) 1997 FreeBSD Inc.5* All rights reserved.6*7* Copyright (c) 2011 The FreeBSD Foundation8*9* Portions of this software were developed by David Chisnall10* under sponsorship from the FreeBSD Foundation.11*12* Redistribution and use in source and binary forms, with or without13* modification, are permitted provided that the following conditions14* are met:15* 1. Redistributions of source code must retain the above copyright16* notice, this list of conditions and the following disclaimer.17* 2. Redistributions in binary form must reproduce the above copyright18* notice, this list of conditions and the following disclaimer in the19* documentation and/or other materials provided with the distribution.20*21* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include <stddef.h>3536#include "ldpart.h"37#include "timelocal.h"3839struct xlocale_time {40struct xlocale_component header;41char *buffer;42struct lc_time_T locale;43};4445struct xlocale_time __xlocale_global_time;4647#define LCTIME_SIZE (sizeof(struct lc_time_T) / sizeof(char *))4849static const struct lc_time_T _C_time_locale = {50{51"Jan", "Feb", "Mar", "Apr", "May", "Jun",52"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"53}, {54"January", "February", "March", "April", "May", "June",55"July", "August", "September", "October", "November", "December"56}, {57"Sun", "Mon", "Tue", "Wed",58"Thu", "Fri", "Sat"59}, {60"Sunday", "Monday", "Tuesday", "Wednesday",61"Thursday", "Friday", "Saturday"62},6364/* X_fmt */65"%H:%M:%S",6667/*68* x_fmt69* Since the C language standard calls for70* "date, using locale's date format," anything goes.71* Using just numbers (as here) makes Quakers happier;72* it's also compatible with SVR4.73*/74"%m/%d/%y",7576/*77* c_fmt78*/79"%a %b %e %H:%M:%S %Y",8081/* am */82"AM",8384/* pm */85"PM",8687/* date_fmt */88"%a %b %e %H:%M:%S %Z %Y",8990/* alt_month91* Standalone months forms for %OB92*/93{94"January", "February", "March", "April", "May", "June",95"July", "August", "September", "October", "November", "December"96},9798/* md_order99* Month / day order in dates100*/101"md",102103/* ampm_fmt104* To determine 12-hour clock format time (empty, if N/A)105*/106"%I:%M:%S %p"107};108109static void destruct_time(void *v)110{111struct xlocale_time *l = v;112if (l->buffer)113free(l->buffer);114free(l);115}116117#include <stdio.h>118struct lc_time_T *119__get_current_time_locale(locale_t loc)120{121return (loc->using_time_locale122? &((struct xlocale_time *)loc->components[XLC_TIME])->locale123: (struct lc_time_T *)&_C_time_locale);124}125126static int127time_load_locale(struct xlocale_time *l, int *using_locale, const char *name)128{129struct lc_time_T *time_locale = &l->locale;130return (__part_load_locale(name, using_locale,131&l->buffer, "LC_TIME",132LCTIME_SIZE, LCTIME_SIZE,133(const char **)time_locale));134}135int136__time_load_locale(const char *name)137{138return time_load_locale(&__xlocale_global_time,139&__xlocale_global_locale.using_time_locale, name);140}141void* __time_load(const char* name, locale_t loc)142{143struct xlocale_time *new = calloc(sizeof(struct xlocale_time), 1);144new->header.header.destructor = destruct_time;145if (time_load_locale(new, &loc->using_time_locale, name) == _LDP_ERROR)146{147xlocale_release(new);148return NULL;149}150return new;151}152153154155