summaryrefslogtreecommitdiff
path: root/src/corefx/System.Globalization.Native/timeZoneInfo.cpp
blob: 0dd28b4b7081ecb8d3e48151a93a84764eaa4594 (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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//

#include <stdint.h>
#include <unistd.h>

#include "icushim.h"
#include "locale.hpp"
#include "holders.h"
#include "errors.h"

/*
Gets the symlink value for the path.
*/
extern "C" int32_t GlobalizationNative_ReadLink(const char* path, char* result, size_t resultCapacity)
{
    ssize_t r = readlink(path, result, resultCapacity - 1); // subtract one to make room for the NULL character

    if (r < 1 || r >= resultCapacity)
        return false;

    result[r] = '\0';
    return true;
}

/*
These values should be kept in sync with the managed Interop.GlobalizationInterop.TimeZoneDisplayNameType enum.
*/
enum TimeZoneDisplayNameType : int32_t
{
    Generic = 0,
    Standard = 1,
    DaylightSavings = 2,
};

/*
Gets the localized display name for the specified time zone.
*/
extern "C" ResultCode GlobalizationNative_GetTimeZoneDisplayName(
    const UChar* localeName, const UChar* timeZoneId, TimeZoneDisplayNameType type, UChar* result, int32_t resultLength)
{
    UErrorCode err = U_ZERO_ERROR;
    char locale[ULOC_FULLNAME_CAPACITY];
    GetLocale(localeName, locale, ULOC_FULLNAME_CAPACITY, false, &err);

    int32_t timeZoneIdLength = -1; // timeZoneId is NULL-terminated
    UCalendar* calendar = ucal_open(timeZoneId, timeZoneIdLength, locale, UCAL_DEFAULT, &err);
    UCalendarHolder calendarHolder(calendar, err);

    // TODO (https://github.com/dotnet/corefx/issues/5741): need to support Generic names, but ICU "C" api
    // has no public option for this. For now, just use the ICU standard name for both Standard and Generic
    // (which is the same behavior on Windows with the mincore TIME_ZONE_INFORMATION APIs).
    ucal_getTimeZoneDisplayName(
        calendar, type == DaylightSavings ? UCAL_DST : UCAL_STANDARD, locale, result, resultLength, &err);

    return GetResultCode(err);
}