getTimezoneByName

Takes a string representation of a timezone (e.g. Europe/Stockholm) and returns a TimeZone that corresponds to it, if one was found.

version(WithTimePlugin)
private
getTimezoneByName
(
const string specified
)

Parameters

specified string

Timezone identification string.

Return Value

Type: auto

A TimeZone that matches the passed specified identification string, or null if none was found.

Examples

import std.exception : assertThrown;
import core.time : TimeException;

// core.time.TimeException@std/datetime/timezone.d(2096): /usr/share/zoneinfo is not a file.
// As above

void assertMatches(const string specified, const string expected)
{
    version(Posix)
    {
        import std.datetime.timezone : TZ = PosixTimeZone;
    }
    else version(Windows)
    {
        import std.datetime.timezone : TZ = WindowsTimeZone;
    }

    immutable actual = getTimezoneByName(specified);
    immutable result = TZ.getTimeZone(expected);
    assert((actual.name == result.name), result.name);
}

version(Posix)
{
    assertMatches("Stockholm", "Europe/Stockholm");
    assertMatches("CET", "CET");
    assertMatches("Tokyo", "Asia/Tokyo");
    assertThrown!TimeException(assertMatches("Nangijala", string.init));
}
else version(Windows)
{
    assertMatches("CET", "Central European Standard Time");
    assertMatches("Central", "Central Standard Time");
    assertMatches("Tokyo", "Tokyo Standard Time");
    assertMatches("UTC", "UTC");
    assertThrown!TimeException(assertMatches("Nangijala", string.init));
}