summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibC/langinfo.cpp
blob: a44f70afe16aa31c4e729cfbcfe62aad4f8fa46f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright (c) 2021, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/DateConstants.h>
#include <langinfo.h>

// Values taken from glibc's en_US locale files.
static char const* __nl_langinfo(nl_item item)
{
    switch (item) {
    case CODESET:
        return "UTF-8";
    case D_T_FMT:
        return "%a %d %b %Y %r %Z";
    case D_FMT:
        return "%m/%d/%Y";
    case T_FMT:
        return "%r";
    case T_FMT_AMPM:
        return "%I:%M:%S %p";
    case AM_STR:
        return "AM";
    case PM_STR:
        return "PM";
    case DAY_1:
    case DAY_2:
    case DAY_3:
    case DAY_4:
    case DAY_5:
    case DAY_6:
    case DAY_7:
        return long_day_names[item - DAY_1].characters_without_null_termination();
    case ABDAY_1:
    case ABDAY_2:
    case ABDAY_3:
    case ABDAY_4:
    case ABDAY_5:
    case ABDAY_6:
    case ABDAY_7:
        return short_day_names[item - ABDAY_1].characters_without_null_termination();
    case MON_1:
    case MON_2:
    case MON_3:
    case MON_4:
    case MON_5:
    case MON_6:
    case MON_7:
    case MON_8:
    case MON_9:
    case MON_10:
    case MON_11:
    case MON_12:
        return long_month_names[item - MON_1].characters_without_null_termination();
    case ABMON_1:
    case ABMON_2:
    case ABMON_3:
    case ABMON_4:
    case ABMON_5:
    case ABMON_6:
    case ABMON_7:
    case ABMON_8:
    case ABMON_9:
    case ABMON_10:
    case ABMON_11:
    case ABMON_12:
        return short_month_names[item - ABMON_1].characters_without_null_termination();
    case RADIXCHAR:
        return ".";
    case THOUSEP:
        return ",";
    case YESEXPR:
        return "^[+1yY]";
    case NOEXPR:
        return "^[-0nN]";
    // en_US does not have ERA.
    case ERA:
    case ERA_D_FMT:
    case ERA_D_T_FMT:
    case ERA_T_FMT:
    // en_US also doesn't have special digit symbols.
    case ALT_DIGITS:
    // Invalid values also return an empty string.
    default:
        return "";
    }
}

extern "C" {

char* nl_langinfo(nl_item item)
{
    // POSIX states that returned strings should not be modified,
    // so this cast is probably fine.
    return const_cast<char*>(__nl_langinfo(item));
}
}