123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187
/*******************************************************************************

        copyright:      Copyright (c) 2005 John Chapman. All rights reserved

        license:        BSD style: $(LICENSE)

        version:        Initial release: 2005

        author:         John Chapman

        Contains classes that provide information about locales, such as 
        the language and calendars, as well as cultural conventions used 
        for formatting dates, currency and numbers. Use these classes when 
        writing applications for an international audience.

******************************************************************************/

/**
 * $(MEMBERTABLE
 * $(TR
 * $(TH Interface)
 * $(TH Description)
 * )
 * $(TR
 * $(TD $(LINK2 #IFormatService, IFormatService))
 * $(TD Retrieves an object to control formatting.)
 * )
 * )
 *
 * $(MEMBERTABLE
 * $(TR
 * $(TH Class)
 * $(TH Description)
 * )
 * $(TR
 * $(TD $(LINK2 #Calendar, Calendar))
 * $(TD Represents time in week, month and year divisions.)
 * )
 * $(TR
 * $(TD $(LINK2 #Culture, Culture))
 * $(TD Provides information about a culture, such as its name, calendar and date and number format patterns.)
 * )
 * $(TR
 * $(TD $(LINK2 #DateTimeFormat, DateTimeFormat))
 * $(TD Determines how $(LINK2 #Time, Time) values are formatted, depending on the culture.)
 * )
 * $(TR
 * $(TD $(LINK2 #DaylightSavingTime, DaylightSavingTime))
 * $(TD Represents a period of daylight-saving time.)
 * )
 * $(TR
 * $(TD $(LINK2 #Gregorian, Gregorian))
 * $(TD Represents the Gregorian calendar.)
 * )
 * $(TR
 * $(TD $(LINK2 #Hebrew, Hebrew))
 * $(TD Represents the Hebrew calendar.)
 * )
 * $(TR
 * $(TD $(LINK2 #Hijri, Hijri))
 * $(TD Represents the Hijri calendar.)
 * )
 * $(TR
 * $(TD $(LINK2 #Japanese, Japanese))
 * $(TD Represents the Japanese calendar.)
 * )
 * $(TR
 * $(TD $(LINK2 #Korean, Korean))
 * $(TD Represents the Korean calendar.)
 * )
 * $(TR
 * $(TD $(LINK2 #NumberFormat, NumberFormat))
 * $(TD Determines how numbers are formatted, according to the current culture.)
 * )
 * $(TR
 * $(TD $(LINK2 #Region, Region))
 * $(TD Provides information about a region.)
 * )
 * $(TR
 * $(TD $(LINK2 #Taiwan, Taiwan))
 * $(TD Represents the Taiwan calendar.)
 * )
 * $(TR
 * $(TD $(LINK2 #ThaiBuddhist, ThaiBuddhist))
 * $(TD Represents the Thai Buddhist calendar.)
 * )
 * )
 *
 * $(MEMBERTABLE
 * $(TR
 * $(TH Struct)
 * $(TH Description)
 * )
 * $(TR
 * $(TD $(LINK2 #Time, Time))
 * $(TD Represents time expressed as a date and time of day.)
 * )
 * $(TR
 * $(TD $(LINK2 #TimeSpan, TimeSpan))
 * $(TD Represents a time interval.)
 * )
 * )
 */

module tango.text.locale.Core;

private import  tango.core.Exception;

private import  tango.text.locale.Data;

private import  tango.time.Time;

private import  tango.time.chrono.Hijri,
                tango.time.chrono.Korean,
                tango.time.chrono.Taiwan,
                tango.time.chrono.Hebrew,
                tango.time.chrono.Calendar,
                tango.time.chrono.Japanese,
                tango.time.chrono.Gregorian,
                tango.time.chrono.ThaiBuddhist;
        
version (Windows)
         private import tango.text.locale.Win32;

version (Posix)
         private import tango.text.locale.Posix;


// Initializes an array.
private template arrayOf(T) {
  private T[] arrayOf(T[] params ...) {
    return params.dup;
  }
}


/**
 * Defines the types of cultures that can be retrieved from Culture.getCultures.
 */
public enum CultureTypes {
  Neutral = 1,             /// Refers to cultures that are associated with a language but not specific to a country or region.
  Specific = 2,            /// Refers to cultures that are specific to a country or region.
  All = Neutral | Specific /// Refers to all cultures.
}


/**
 * $(ANCHOR _IFormatService)
 * Retrieves an object to control formatting.
 * 
 * A class implements $(LINK2 #IFormatService_getFormat, getFormat) to retrieve an object that provides format information for the implementing type.
 * Remarks: IFormatService is implemented by $(LINK2 #Culture, Culture), $(LINK2 #NumberFormat, NumberFormat) and $(LINK2 #DateTimeFormat, DateTimeFormat) to provide locale-specific formatting of
 * numbers and date and time values.
 */
public interface IFormatService {

  /**
   * $(ANCHOR IFormatService_getFormat)
   * Retrieves an object that supports formatting for the specified _type.
   * Returns: The current instance if type is the same _type as the current instance; otherwise, null.
   * Params: type = An object that specifies the _type of formatting to retrieve.
   */
  Object getFormat(TypeInfo type);

}

/**
 * $(ANCHOR _Culture)
 * Provides information about a culture, such as its name, calendar and date and number format patterns.
 * Remarks: tango.text.locale adopts the RFC 1766 standard for culture names in the format <language>"-"<region>. 
 * <language> is a lower-case two-letter code defined by ISO 639-1. <region> is an upper-case 
 * two-letter code defined by ISO 3166. For example, "en-GB" is UK English.
 * $(BR)$(BR)There are three types of culture: invariant, neutral and specific. The invariant culture is not tied to
 * any specific region, although it is associated with the English language. A neutral culture is associated with
 * a language, but not with a region. A specific culture is associated with a language and a region. "es" is a neutral 
 * culture. "es-MX" is a specific culture.
 * $(BR)$(BR)Instances of $(LINK2 #DateTimeFormat, DateTimeFormat) and $(LINK2 #NumberFormat, NumberFormat) cannot be created for neutral cultures.
 * Examples:
 * ---
 * import tango.io.Stdout, tango.text.locale.Core;
 *
 * void main() {
 *   Culture culture = new Culture("it-IT");
 *
 *   Stdout.formatln("englishName: {}", culture.englishName);
 *   Stdout.formatln("nativeName: {}", culture.nativeName);
 *   Stdout.formatln("name: {}", culture.name);
 *   Stdout.formatln("parent: {}", culture.parent.name);
 *   Stdout.formatln("isNeutral: {}", culture.isNeutral);
 * }
 *
 * // Produces the following output:
 * // englishName: Italian (Italy)
 * // nativeName: italiano (Italia)
 * // name: it-IT
 * // parent: it
 * // isNeutral: false
 * ---
 */
public class Culture : IFormatService {

  private enum int LCID_INVARIANT = 0x007F;

  private static Culture[immutable(char)[]] namedCultures;
  private static Culture[int] idCultures;
  private static Culture[immutable(char)[]] ietfCultures;

  private static Culture currentCulture_;
  private static Culture userDefaultCulture_; // The user's default culture (GetUserDefaultLCID).
  private static Culture invariantCulture_; // The invariant culture is associated with the English language.
  private Calendar calendar_;
  private Culture parent_;
  private const(CultureData)* cultureData_;
  private bool isReadOnly_;
  private NumberFormat numberFormat_;
  private DateTimeFormat dateTimeFormat_;

  shared static this() {
    invariantCulture_ = new Culture(LCID_INVARIANT);
    invariantCulture_.isReadOnly_ = true;

    userDefaultCulture_ = new Culture(nativeMethods.getUserCulture());
    if (userDefaultCulture_ is null)
      // Fallback
      userDefaultCulture_ = invariantCulture;
    else
      userDefaultCulture_.isReadOnly_ = true;
  }

  static ~this() {
    namedCultures = null;
    idCultures = null;
    ietfCultures = null;
  }

  /**
   * Initializes a new Culture instance from the supplied name.
   * Params: cultureName = The name of the Culture.
   */
  public this(const(char)[] cultureName) {
    cultureData_ = CultureData.getDataFromCultureName(cultureName);
  }

  /**
   * Initializes a new Culture instance from the supplied culture identifier.
   * Params: cultureID = The identifer (LCID) of the Culture.
   * Remarks: Culture identifiers correspond to a Windows LCID.
   */
  public this(int cultureID) {
    cultureData_ = CultureData.getDataFromCultureID(cultureID);
  }

  /**
   * Retrieves an object defining how to format the specified type.
   * Params: type = The TypeInfo of the resulting formatting object.
   * Returns: If type is typeid($(LINK2 #NumberFormat, NumberFormat)), the value of the $(LINK2 #Culture_numberFormat, numberFormat) property. If type is typeid($(LINK2 #DateTimeFormat, DateTimeFormat)), the
   * value of the $(LINK2 #Culture_dateTimeFormat, dateTimeFormat) property. Otherwise, null.
   * Remarks: Implements $(LINK2 #IFormatService_getFormat, IFormatService.getFormat).
   */
  public Object getFormat(TypeInfo type) {
    if (type is typeid(NumberFormat))
      return numberFormat;
    else if (type is typeid(DateTimeFormat))
      return dateTimeFormat;
    return null;
  }

version (Clone)
{
  /**
   * Copies the current Culture instance.
   * Returns: A copy of the current Culture instance.
   * Remarks: The values of the $(LINK2 #Culture_numberFormat, numberFormat), $(LINK2 #Culture_dateTimeFormat, dateTimeFormat) and $(LINK2 #Culture_calendar, calendar) properties are copied also.
   */
  public Object clone() {
    Culture culture = cast(Culture)cloneObject(this);
    if (!culture.isNeutral) {
      if (dateTimeFormat_ !is null)
        culture.dateTimeFormat_ = cast(DateTimeFormat)dateTimeFormat_.clone();
      if (numberFormat_ !is null)
        culture.numberFormat_ = cast(NumberFormat)numberFormat_.clone();
    }
    if (calendar_ !is null)
      culture.calendar_ = cast(Calendar)calendar_.clone();
    return culture;
  }
}

  /**
   * Returns a read-only instance of a culture using the specified culture identifier.
   * Params: cultureID = The identifier of the culture.
   * Returns: A read-only culture instance.
   * Remarks: Instances returned by this method are cached.
   */
  public static Culture getCulture(int cultureID) {
    Culture culture = getCultureInternal(cultureID, null);

version (Posix) {
    if (culture is null)
        error ("Culture not found - if this was not tried set by the application, Tango\n"
            ~ "will expect that a locale is set via environment variables LANG or LC_ALL.");
}

    return culture;
  }

  /**
   * Returns a read-only instance of a culture using the specified culture name.
   * Params: cultureName = The name of the culture.
   * Returns: A read-only culture instance.
   * Remarks: Instances returned by this method are cached.
   */
  public static Culture getCulture(const(char)[] cultureName) {
    if (cultureName is null)
       error("Value cannot be null.");
    Culture culture = getCultureInternal(0, cultureName);
    if (culture is null)
      error("Culture name " ~ cultureName.idup ~ " is not supported.");
    return culture;
  }

  /**
    * Returns a read-only instance using the specified name, as defined by the RFC 3066 standard and maintained by the IETF.
    * Params: name = The name of the language.
    * Returns: A read-only culture instance.
    */
  public static Culture getCultureFromIetfLanguageTag(const(char)[] name) {
    if (name is null)
      error("Value cannot be null.");
    Culture culture = getCultureInternal(-1, name);
    if (culture is null)
      error("Culture IETF name " ~ name.idup ~ " is not a known IETF name.");
    return culture;
  }

  private static Culture getCultureInternal(int cultureID, const(char)[] cname) {
    // If cultureID is - 1, name is an IETF name; if it's 0, name is a culture name; otherwise, it's a valid LCID.
    const(char)[] name = cname;
    char[] temp_name;
    foreach (i, c; cname)
       if (c is '_') {
         temp_name = cname.dup;
         temp_name[i] = '-';
         name = temp_name;
         break;
       }

    // Look up tables first.
    if (cultureID == 0) {
      if (Culture* culture = name in namedCultures)
        return *culture;
    }
    else if (cultureID > 0) {
      if (Culture* culture = cultureID in idCultures)
        return *culture;
    }
    else if (cultureID == -1) {
      if (Culture* culture = name in ietfCultures)
        return *culture;
    }

    // Nothing found, create a new instance.
    Culture culture;

    try {
      if (cultureID == -1) {
        name = CultureData.getCultureNameFromIetfName(name);
        if (name is null)
          return null;
      }
      else if (cultureID == 0)
        culture = new Culture(name);
      else if (userDefaultCulture_ !is null && userDefaultCulture_.id == cultureID) {
        culture = userDefaultCulture_;
      }
      else
        culture = new Culture(cultureID);
    }
    catch (LocaleException) {
      return null;
    }

    culture.isReadOnly_ = true;

    // Now cache the new instance in all tables.
    ietfCultures[culture.ietfLanguageTag] = culture;
    namedCultures[culture.name] = culture;
    idCultures[culture.id] = culture;

    return culture;
  }

  /**
   * Returns a list of cultures filtered by the specified $(LINK2 constants.html#CultureTypes, CultureTypes).
   * Params: types = A combination of CultureTypes.
   * Returns: An array of Culture instances containing cultures specified by types.
   */
  public static Culture[] getCultures(CultureTypes types) {
    bool includeSpecific = (types & CultureTypes.Specific) != 0;
    bool includeNeutral = (types & CultureTypes.Neutral) != 0;

    int[] cultures;
    for (int i = 0; i < CultureData.cultureDataTable.length; i++) {
      if ((CultureData.cultureDataTable[i].isNeutral && includeNeutral) || (!CultureData.cultureDataTable[i].isNeutral && includeSpecific))
        cultures ~= CultureData.cultureDataTable[i].lcid;
    }

    Culture[] result = new Culture[cultures.length];
    foreach (int i, int cultureID; cultures)
      result[i] = new Culture(cultureID);
    return result;
  }

  /**
   * Returns the name of the Culture.
   * Returns: A string containing the name of the Culture in the format &lt;language&gt;"-"&lt;region&gt;.
   */
  public override immutable(char)[] toString() {
    return cultureData_.name.idup;
  }

  public override bool opEquals(Object obj) {
    if (obj is this)
      return true;
    Culture other = cast(Culture)obj;
    if (other is null)
      return false;
    return other.name == name; // This needs to be changed so it's culturally aware.
  }

  /**
   * $(ANCHOR Culture_current)
   * $(I Property.) Retrieves the culture of the current user.
   * Returns: The Culture instance representing the user's current culture.
   */
  @property public static Culture current() {
    if (currentCulture_ !is null)
      return currentCulture_;

    if (userDefaultCulture_ !is null) {
      // If the user has changed their locale settings since last we checked, invalidate our data.
      if (userDefaultCulture_.id != nativeMethods.getUserCulture())
        userDefaultCulture_ = null;
    }
    if (userDefaultCulture_ is null) {
      userDefaultCulture_ = new Culture(nativeMethods.getUserCulture());
      if (userDefaultCulture_ is null)
        userDefaultCulture_ = invariantCulture;
      else
        userDefaultCulture_.isReadOnly_ = true;
    }

    return userDefaultCulture_;
  }
  /**
   * $(I Property.) Assigns the culture of the _current user.
   * Params: value = The Culture instance representing the user's _current culture.
   * Examples:
   * The following examples shows how to change the _current culture.
   * ---
   * import tango.io.Print, tango.text.locale.Common;
   *
   * void main() {
   *   // Displays the name of the current culture.
   *   Println("The current culture is %s.", Culture.current.englishName);
   *
   *   // Changes the current culture to el-GR.
   *   Culture.current = new Culture("el-GR");
   *   Println("The current culture is now %s.", Culture.current.englishName);
   * }
   *
   * // Produces the following output:
   * // The current culture is English (United Kingdom).
   * // The current culture is now Greek (Greece).
   * ---
   */
  @property public static void current(Culture value) {
    checkNeutral(value);
    nativeMethods.setUserCulture(value.id);
    currentCulture_ = value;
  }

  /**
   * $(I Property.) Retrieves the invariant Culture.
   * Returns: The Culture instance that is invariant.
   * Remarks: The invariant culture is culture-independent. It is not tied to any specific region, but is associated
   * with the English language.
   */
  @property public static Culture invariantCulture() {
    return invariantCulture_;
  }

  /**
   * $(I Property.) Retrieves the identifier of the Culture.
   * Returns: The culture identifier of the current instance.
   * Remarks: The culture identifier corresponds to the Windows locale identifier (LCID). It can therefore be used when 
   * interfacing with the Windows NLS functions.
   */
  @property public const int id() {
    return cultureData_.lcid;
  }

  /**
   * $(ANCHOR Culture_name)
   * $(I Property.) Retrieves the name of the Culture in the format &lt;language&gt;"-"&lt;region&gt;.
   * Returns: The name of the current instance. For example, the name of the UK English culture is "en-GB".
   */
  @property public const const(char)[] name() {
    return cultureData_.name;
  }

  /**
   * $(I Property.) Retrieves the name of the Culture in the format &lt;languagename&gt; (&lt;regionname&gt;) in English.
   * Returns: The name of the current instance in English. For example, the englishName of the UK English culture 
   * is "English (United Kingdom)".
   */
  @property public const const(char)[] englishName() {
    return cultureData_.englishName;
  }

  /**
   * $(I Property.) Retrieves the name of the Culture in the format &lt;languagename&gt; (&lt;regionname&gt;) in its native language.
   * Returns: The name of the current instance in its native language. For example, if Culture.name is "de-DE", nativeName is 
   * "Deutsch (Deutschland)".
   */
  @property public const const(char)[] nativeName() {
    return cultureData_.nativeName;
  }

  /**
   * $(I Property.) Retrieves the two-letter language code of the culture.
   * Returns: The two-letter language code of the Culture instance. For example, the twoLetterLanguageName for English is "en".
   */
  @property public const const(char)[] twoLetterLanguageName() {
    return cultureData_.isoLangName;
  }

  /**
   * $(I Property.) Retrieves the three-letter language code of the culture.
   * Returns: The three-letter language code of the Culture instance. For example, the threeLetterLanguageName for English is "eng".
   */
  @property public const const(char)[] threeLetterLanguageName() {
    return cultureData_.isoLangName2;
  }

  /**
   * $(I Property.) Retrieves the RFC 3066 identification for a language.
   * Returns: A string representing the RFC 3066 language identification.
   */
  @property public const final const(char)[] ietfLanguageTag() {
    return cultureData_.ietfTag;
  }

  /**
   * $(I Property.) Retrieves the Culture representing the parent of the current instance.
   * Returns: The Culture representing the parent of the current instance.
   */
  @property public Culture parent() {
    if (parent_ is null) {
      try {
        int parentCulture = cultureData_.parent;
        if (parentCulture == LCID_INVARIANT)
          parent_ = invariantCulture;
        else
          parent_ = new Culture(parentCulture);
      }
      catch {
        parent_ = invariantCulture;
      }
    }
    return parent_;
  }

  /**
   * $(I Property.) Retrieves a value indicating whether the current instance is a neutral culture.
   * Returns: true is the current Culture represents a neutral culture; otherwise, false.
   * Examples:
   * The following example displays which cultures using Chinese are neutral.
   * ---
   * import tango.io.Print, tango.text.locale.Common;
   *
   * void main() {
   *   foreach (c; Culture.getCultures(CultureTypes.All)) {
   *     if (c.twoLetterLanguageName == "zh") {
   *       Print(c.englishName);
   *       if (c.isNeutral)
   *         Println("neutral");
   *       else
   *         Println("specific");
   *     }
   *   }
   * }
   *
   * // Produces the following output:
   * // Chinese (Simplified) - neutral
   * // Chinese (Taiwan) - specific
   * // Chinese (People's Republic of China) - specific
   * // Chinese (Hong Kong S.A.R.) - specific
   * // Chinese (Singapore) - specific
   * // Chinese (Macao S.A.R.) - specific
   * // Chinese (Traditional) - neutral
   * ---
   */
  @property public const bool isNeutral() {
    return cultureData_.isNeutral;
  }

  /**
   * $(I Property.) Retrieves a value indicating whether the instance is read-only.
   * Returns: true if the instance is read-only; otherwise, false.
   * Remarks: If the culture is read-only, the $(LINK2 #Culture_dateTimeFormat, dateTimeFormat) and $(LINK2 #Culture_numberFormat, numberFormat) properties return 
   * read-only instances.
   */
  @property public const final bool isReadOnly() {
    return isReadOnly_;
  }

  /**
   * $(ANCHOR Culture_calendar)
   * $(I Property.) Retrieves the calendar used by the culture.
   * Returns: A Calendar instance respresenting the calendar used by the culture.
   */
  @property public Calendar calendar() {
    if (calendar_ is null) {
      calendar_ = getCalendarInstance(cultureData_.calendarType, isReadOnly_);
    }
    return calendar_;
  }

  /**
   * $(I Property.) Retrieves the list of calendars that can be used by the culture.
   * Returns: An array of type Calendar representing the calendars that can be used by the culture.
   */
  @property public Calendar[] optionalCalendars() {
    Calendar[] cals = new Calendar[cultureData_.optionalCalendars.length];
    foreach (int i, int calID; cultureData_.optionalCalendars)
      cals[i] = getCalendarInstance(calID);
    return cals;
  }

  /**
   * $(ANCHOR Culture_numberFormat)
   * $(I Property.) Retrieves a NumberFormat defining the culturally appropriate format for displaying numbers and currency.
   * Returns: A NumberFormat defining the culturally appropriate format for displaying numbers and currency.
  */
  @property public NumberFormat numberFormat() {
    checkNeutral(this);
    if (numberFormat_ is null) {
      numberFormat_ = new NumberFormat(cultureData_);
      numberFormat_.isReadOnly_ = isReadOnly_;
    }
    return numberFormat_;
  }
  /**
   * $(I Property.) Assigns a NumberFormat defining the culturally appropriate format for displaying numbers and currency.
   * Params: values = A NumberFormat defining the culturally appropriate format for displaying numbers and currency.
   */
  @property public void numberFormat(NumberFormat value) {
    checkReadOnly();
    numberFormat_ = value;
  }

  /**
   * $(ANCHOR Culture_dateTimeFormat)
   * $(I Property.) Retrieves a DateTimeFormat defining the culturally appropriate format for displaying dates and times.
   * Returns: A DateTimeFormat defining the culturally appropriate format for displaying dates and times.
   */
  @property public DateTimeFormat dateTimeFormat() {
    checkNeutral(this);
    if (dateTimeFormat_ is null) {
      dateTimeFormat_ = new DateTimeFormat(cultureData_, calendar);
      dateTimeFormat_.isReadOnly_ = isReadOnly_;
    }
    return dateTimeFormat_;
  }
  /**
   * $(I Property.) Assigns a DateTimeFormat defining the culturally appropriate format for displaying dates and times.
   * Params: values = A DateTimeFormat defining the culturally appropriate format for displaying dates and times.
   */
  @property public void dateTimeFormat(DateTimeFormat value) {
    checkReadOnly();
    dateTimeFormat_ = value;
  }

  private static void checkNeutral(Culture culture) {
    if (culture.isNeutral)
      error("Culture '" ~ culture.name.idup ~ "' is a neutral culture. It cannot be used in formatting and therefore cannot be set as the current culture.");
  }

  private void checkReadOnly() {
    if (isReadOnly_)
      error("Instance is read-only.");
  }

  private static Calendar getCalendarInstance(int calendarType, bool readOnly=false) {
    switch (calendarType) {
      case Calendar.JAPAN:
        return new Japanese();
      case Calendar.TAIWAN:
        return new Taiwan();
      case Calendar.KOREA:
        return new Korean();
      case Calendar.HIJRI:
        return new Hijri();
      case Calendar.THAI:
        return new ThaiBuddhist();
      case Calendar.HEBREW:
        return new Hebrew;
      case Calendar.GREGORIAN_US:
      case Calendar.GREGORIAN_ME_FRENCH:
      case Calendar.GREGORIAN_ARABIC:
      case Calendar.GREGORIAN_XLIT_ENGLISH:
      case Calendar.GREGORIAN_XLIT_FRENCH:
        return new Gregorian(cast(Gregorian.Type) calendarType);
      default:
        break;
    }
    return new Gregorian();
  }

}

/**
 * $(ANCHOR _Region)
 * Provides information about a region.
 * Remarks: Region does not represent user preferences. It does not depend on the user's language or culture.
 * Examples:
 * The following example displays some of the properties of the Region class:
 * ---
 * import tango.io.Print, tango.text.locale.Common;
 *
 * void main() {
 *   Region region = new Region("en-GB");
 *   Println("name:              %s", region.name);
 *   Println("englishName:       %s", region.englishName);
 *   Println("isMetric:          %s", region.isMetric);
 *   Println("currencySymbol:    %s", region.currencySymbol);
 *   Println("isoCurrencySymbol: %s", region.isoCurrencySymbol);
 * }
 *
 * // Produces the following output.
 * // name:              en-GB
 * // englishName:       United Kingdom
 * // isMetric:          true
 * // currencySymbol:    £
 * // isoCurrencySymbol: GBP
 * ---
 */
public class Region {

  private const(CultureData)* cultureData_;
  private static Region currentRegion_;
  private const(char)[] name_;

  /**
   * Initializes a new Region instance based on the region associated with the specified culture identifier.
   * Params: cultureID = A culture indentifier.
   * Remarks: The name of the Region instance is set to the ISO 3166 two-letter code for that region.
   */
  public this(int cultureID) {
    cultureData_ = CultureData.getDataFromCultureID(cultureID);
    if (cultureData_.isNeutral)
        error ("Cannot use a neutral culture to create a region.");
    name_ = cultureData_.regionName;
  }

  /**
   * $(ANCHOR Region_ctor_name)
   * Initializes a new Region instance based on the region specified by name.
   * Params: name = A two-letter ISO 3166 code for the region. Or, a culture $(LINK2 #Culture_name, _name) consisting of the language and region.
   */
  public this(const(char)[] name) {
    cultureData_ = CultureData.getDataFromRegionName(name);
    name_ = name;
    if (cultureData_.isNeutral)
        error ("The region name " ~ name.idup ~ " corresponds to a neutral culture and cannot be used to create a region.");
  }

  package this(const(CultureData)* cultureData) {
    cultureData_ = cultureData;
    name_ = cultureData.regionName;
  }

  /**
   * $(I Property.) Retrieves the Region used by the current $(LINK2 #Culture, Culture).
   * Returns: The Region instance associated with the current Culture.
   */
  @property public static Region current() {
    if (currentRegion_ is null)
      currentRegion_ = new Region(Culture.current.cultureData_);
    return currentRegion_;
  }

  /**
   * $(I Property.) Retrieves a unique identifier for the geographical location of the region.
   * Returns: An $(B int) uniquely identifying the geographical location.
   */
  @property public const int geoID() {
    return cultureData_.geoId;
  }

  /**
   * $(ANCHOR Region_name)
   * $(I Property.) Retrieves the ISO 3166 code, or the name, of the current Region.
   * Returns: The value specified by the name parameter of the $(LINK2 #Region_ctor_name, Region(char[])) constructor.
   */
  @property public const const(char)[] name() {
    return name_;
  }

  /**
   * $(I Property.) Retrieves the full name of the region in English.
   * Returns: The full name of the region in English.
   */
  @property public const const(char)[] englishName() {
    return cultureData_.englishCountry;
  }

  /**
   * $(I Property.) Retrieves the full name of the region in its native language.
   * Returns: The full name of the region in the language associated with the region code.
   */
  @property public const const(char)[] nativeName() {
    return cultureData_.nativeCountry;
  }

  /**
   * $(I Property.) Retrieves the two-letter ISO 3166 code of the region.
   * Returns: The two-letter ISO 3166 code of the region.
   */
  @property public const const(char)[] twoLetterRegionName() {
    return cultureData_.regionName;
  }

  /**
   * $(I Property.) Retrieves the three-letter ISO 3166 code of the region.
   * Returns: The three-letter ISO 3166 code of the region.
   */
  @property public const const(char)[] threeLetterRegionName() {
    return cultureData_.isoRegionName;
  }

  /**
   * $(I Property.) Retrieves the currency symbol of the region.
   * Returns: The currency symbol of the region.
   */
  @property public const const(char)[] currencySymbol() {
    return cultureData_.currency;
  }

  /**
   * $(I Property.) Retrieves the three-character currency symbol of the region.
   * Returns: The three-character currency symbol of the region.
   */
  @property public const const(char)[] isoCurrencySymbol() {
    return cultureData_.intlSymbol;
  }

  /**
   * $(I Property.) Retrieves the name in English of the currency used in the region.
   * Returns: The name in English of the currency used in the region.
   */
  @property public const const(char)[] currencyEnglishName() {
    return cultureData_.englishCurrency;
  }

  /**
   * $(I Property.) Retrieves the name in the native language of the region of the currency used in the region.
   * Returns: The name in the native language of the region of the currency used in the region.
   */
  @property public const const(char)[] currencyNativeName() {
    return cultureData_.nativeCurrency;
  }

  /**
   * $(I Property.) Retrieves a value indicating whether the region uses the metric system for measurements.
   * Returns: true is the region uses the metric system; otherwise, false.
   */
  @property public const bool isMetric() {
    return cultureData_.isMetric;
  }

  /**
   * Returns a string containing the ISO 3166 code, or the $(LINK2 #Region_name, name), of the current Region.
   * Returns: A string containing the ISO 3166 code, or the name, of the current Region.
   */
  public override immutable(char)[] toString() {
    return name_.idup;
  }

}

/**
 * $(ANCHOR _NumberFormat)
 * Determines how numbers are formatted, according to the current culture.
 * Remarks: Numbers are formatted using format patterns retrieved from a NumberFormat instance.
 * This class implements $(LINK2 #IFormatService_getFormat, IFormatService.getFormat).
 * Examples:
 * The following example shows how to retrieve an instance of NumberFormat for a Culture
 * and use it to display number formatting information.
 * ---
 * import tango.io.Print, tango.text.locale.Common;
 *
 * void main(char[][] args) {
 *   foreach (c; Culture.getCultures(CultureTypes.Specific)) {
 *     if (c.twoLetterLanguageName == "en") {
 *       NumberFormat fmt = c.numberFormat;
 *       Println("The currency symbol for %s is '%s'", 
 *         c.englishName, 
 *         fmt.currencySymbol);
 *     }
 *   }
 * }
 *
 * // Produces the following output:
 * // The currency symbol for English (United States) is '$'
 * // The currency symbol for English (United Kingdom) is '£'
 * // The currency symbol for English (Australia) is '$'
 * // The currency symbol for English (Canada) is '$'
 * // The currency symbol for English (New Zealand) is '$'
 * // The currency symbol for English (Ireland) is '€'
 * // The currency symbol for English (South Africa) is 'R'
 * // The currency symbol for English (Jamaica) is 'J$'
 * // The currency symbol for English (Caribbean) is '$'
 * // The currency symbol for English (Belize) is 'BZ$'
 * // The currency symbol for English (Trinidad and Tobago) is 'TT$'
 * // The currency symbol for English (Zimbabwe) is 'Z$'
 * // The currency symbol for English (Republic of the Philippines) is 'Php'
 *---
 */
public class NumberFormat : IFormatService {

  package bool isReadOnly_;
  private static NumberFormat invariantFormat_;

  private int numberDecimalDigits_;
  private int numberNegativePattern_;
  private int currencyDecimalDigits_;
  private int currencyNegativePattern_;
  private int currencyPositivePattern_;
  private const(int)[] numberGroupSizes_;
  private const(int)[] currencyGroupSizes_;
  private const(char)[] numberGroupSeparator_;
  private const(char)[] numberDecimalSeparator_;
  private const(char)[] currencyGroupSeparator_;
  private const(char)[] currencyDecimalSeparator_;
  private const(char)[] currencySymbol_;
  private const(char)[] negativeSign_;
  private const(char)[] positiveSign_;
  private const(char)[] nanSymbol_;
  private const(char)[] negativeInfinitySymbol_;
  private const(char)[] positiveInfinitySymbol_;
  private const(char[])[] nativeDigits_;

  /**
   * Initializes a new, culturally independent instance.
   *
   * Remarks: Modify the properties of the new instance to define custom formatting.
   */
  public this() {
    this(null);
  }

  package this(const(CultureData)* cultureData) {
    // Initialize invariant data.
    numberDecimalDigits_ = 2;
    numberNegativePattern_ = 1;
    currencyDecimalDigits_ = 2;
    numberGroupSizes_ = arrayOf!(int)(3);
    currencyGroupSizes_ = arrayOf!(int)(3);
    numberGroupSeparator_ = ",";
    numberDecimalSeparator_ = ".";
    currencyGroupSeparator_ = ",";
    currencyDecimalSeparator_ = ".";
    currencySymbol_ = "\u00A4";
    negativeSign_ = "-";
    positiveSign_ = "+";
    nanSymbol_ = "NaN";
    negativeInfinitySymbol_ = "-Infinity";
    positiveInfinitySymbol_ = "Infinity";
    nativeDigits_ = arrayOf!(const(char)[])("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");

    if (cultureData !is null && cultureData.lcid != Culture.LCID_INVARIANT) {
      // Initialize culture-specific data.
      numberDecimalDigits_ = cultureData.digits;
      numberNegativePattern_ = cultureData.negativeNumber;
      currencyDecimalDigits_ = cultureData.currencyDigits;
      currencyNegativePattern_ = cultureData.negativeCurrency;
      currencyPositivePattern_ = cultureData.positiveCurrency;
      numberGroupSizes_ = cultureData.grouping;
      currencyGroupSizes_ = cultureData.monetaryGrouping;
      numberGroupSeparator_ = cultureData.thousand;
      numberDecimalSeparator_ = cultureData.decimal;
      currencyGroupSeparator_ = cultureData.monetaryThousand;
      currencyDecimalSeparator_ = cultureData.monetaryDecimal;
      currencySymbol_ = cultureData.currency;
      negativeSign_ = cultureData.negativeSign;
      positiveSign_ = cultureData.positiveSign;
      nanSymbol_ = cultureData.nan;
      negativeInfinitySymbol_ = cultureData.negInfinity;
      positiveInfinitySymbol_ = cultureData.posInfinity;
      nativeDigits_ = cultureData.nativeDigits;
    }
  }

  /**
   * Retrieves an object defining how to format the specified type.
   * Params: type = The TypeInfo of the resulting formatting object.
   * Returns: If type is typeid($(LINK2 #NumberFormat, NumberFormat)), the current NumberFormat instance. Otherwise, null.
   * Remarks: Implements $(LINK2 #IFormatService_getFormat, IFormatService.getFormat).
   */
  public Object getFormat(TypeInfo type) {
    return (type is typeid(NumberFormat)) ? this : null;
  }

version (Clone)
{
  /**
   * Creates a copy of the instance.
   */
  public Object clone() {
    NumberFormat copy = cast(NumberFormat)cloneObject(this);
    copy.isReadOnly_ = false;
    return copy;
  }
}

  /**
   * Retrieves the NumberFormat for the specified $(LINK2 #IFormatService, IFormatService).
   * Params: formatService = The IFormatService used to retrieve NumberFormat.
   * Returns: The NumberFormat for the specified IFormatService.
   * Remarks: The method calls $(LINK2 #IFormatService_getFormat, IFormatService.getFormat) with typeof(NumberFormat). If formatService is null,
   * then the value of the current property is returned.
   */
  public static NumberFormat getInstance(IFormatService formatService) {
    Culture culture = cast(Culture)formatService;
    if (culture !is null) {
      if (culture.numberFormat_ !is null)
        return culture.numberFormat_;
      return culture.numberFormat;
    }
    if (NumberFormat numberFormat = cast(NumberFormat)formatService)
      return numberFormat;
    if (formatService !is null) {
      if (NumberFormat numberFormat = cast(NumberFormat)(formatService.getFormat(typeid(NumberFormat))))
        return numberFormat;
    }
    return current;
  }

  /**
   * $(I Property.) Retrieves a read-only NumberFormat instance from the current culture.
   * Returns: A read-only NumberFormat instance from the current culture.
   */
  @property public static NumberFormat current() {
    return Culture.current.numberFormat;
  }

  /**
   * $(ANCHOR NumberFormat_invariantFormat)
   * $(I Property.) Retrieves the read-only, culturally independent NumberFormat instance.
   * Returns: The read-only, culturally independent NumberFormat instance.
   */
  public static NumberFormat invariantFormat() {
    if (invariantFormat_ is null) {
      invariantFormat_ = new NumberFormat;
      invariantFormat_.isReadOnly_ = true;
    }
    return invariantFormat_;
  }

  /**
   * $(I Property.) Retrieves a value indicating whether the instance is read-only.
   * Returns: true if the instance is read-only; otherwise, false.
   */
  @property public final const bool isReadOnly() {
    return isReadOnly_;
  }

  /**
   * $(I Property.) Retrieves the number of decimal places used for numbers.
   * Returns: The number of decimal places used for numbers. For $(LINK2 #NumberFormat_invariantFormat, invariantFormat), the default is 2.
   */
  @property public final const int numberDecimalDigits() {
    return numberDecimalDigits_;
  }
  /**
   * Assigns the number of decimal digits used for numbers.
   * Params: value = The number of decimal places used for numbers.
   * Throws: Exception if the property is being set and the instance is read-only.
   * Examples:
   * The following example shows the effect of changing numberDecimalDigits.
   * ---
   * import tango.io.Print, tango.text.locale.Common;
   *
   * void main() {
   *   // Get the NumberFormat from the en-GB culture.
   *   NumberFormat fmt = (new Culture("en-GB")).numberFormat;
   *
   *   // Display a value with the default number of decimal digits.
   *   int n = 5678;
   *   Println(Formatter.format(fmt, "{0:N}", n));
   *
   *   // Display the value with six decimal digits.
   *   fmt.numberDecimalDigits = 6;
   *   Println(Formatter.format(fmt, "{0:N}", n));
   * }
   *
   * // Produces the following output:
   * // 5,678.00
   * // 5,678.000000
   * ---
   */
  @property public final void numberDecimalDigits(int value) {
    checkReadOnly();
    numberDecimalDigits_ = value;
  }

  /**
   * $(I Property.) Retrieves the format pattern for negative numbers.
   * Returns: The format pattern for negative numbers. For invariantFormat, the default is 1 (representing "-n").
   * Remarks: The following table shows valid values for this property.
   *
   * <table class="definitionTable">
   * <tr><th>Value</th><th>Pattern</th></tr>
   * <tr><td>0</td><td>(n)</td></tr>
   * <tr><td>1</td><td>-n</td></tr>
   * <tr><td>2</td><td>- n</td></tr>
   * <tr><td>3</td><td>n-</td></tr>
   * <tr><td>4</td><td>n -</td></tr>
   * </table>
   */
  @property public final const int numberNegativePattern() {
    return numberNegativePattern_;
  }
  /**
   * $(I Property.) Assigns the format pattern for negative numbers.
   * Params: value = The format pattern for negative numbers.
   * Examples:
   * The following example shows the effect of the different patterns.
   * ---
   * import tango.io.Print, tango.text.locale.Common;
   *
   * void main() {
   *   NumberFormat fmt = new NumberFormat;
   *   int n = -5678;
   *
   *   // Display the default pattern.
   *   Println(Formatter.format(fmt, "{0:N}", n));
   *
   *   // Display all patterns.
   *   for (int i = 0; i <= 4; i++) {
   *     fmt.numberNegativePattern = i;
   *     Println(Formatter.format(fmt, "{0:N}", n));
   *   }
   * }
   *
   * // Produces the following output:
   * // (5,678.00)
   * // (5,678.00)
   * // -5,678.00
   * // - 5,678.00
   * // 5,678.00-
   * // 5,678.00 -
   * ---
   */
  @property public final void numberNegativePattern(int value) {
    checkReadOnly();
    numberNegativePattern_ = value;
  }

  /**
   * $(I Property.) Retrieves the number of decimal places to use in currency values.
   * Returns: The number of decimal digits to use in currency values.
   */
  @property public const final int currencyDecimalDigits() {
    return currencyDecimalDigits_;
  }
  /**
   * $(I Property.) Assigns the number of decimal places to use in currency values.
   * Params: value = The number of decimal digits to use in currency values.
   */
  @property public final void currencyDecimalDigits(int value) {
    checkReadOnly();
    currencyDecimalDigits_ = value;
  }

  /**
   * $(I Property.) Retrieves the formal pattern to use for negative currency values.
   * Returns: The format pattern to use for negative currency values.
   */
  @property public const final int currencyNegativePattern() {
    return currencyNegativePattern_;
  }
  /**
   * $(I Property.) Assigns the formal pattern to use for negative currency values.
   * Params: value = The format pattern to use for negative currency values.
   */
  @property public final void currencyNegativePattern(int value) {
    checkReadOnly();
    currencyNegativePattern_ = value;
  }

  /**
   * $(I Property.) Retrieves the formal pattern to use for positive currency values.
   * Returns: The format pattern to use for positive currency values.
   */
  @property public const final int currencyPositivePattern() {
    return currencyPositivePattern_;
  }
  /**
   * $(I Property.) Assigns the formal pattern to use for positive currency values.
   * Returns: The format pattern to use for positive currency values.
   */
  @property public final void currencyPositivePattern(int value) {
    checkReadOnly();
    currencyPositivePattern_ = value;
  }

  /**
   * $(I Property.) Retrieves the number of digits int each group to the left of the decimal place in numbers.
   * Returns: The number of digits int each group to the left of the decimal place in numbers.
   */
  @property public const final const(int)[] numberGroupSizes() {
    return numberGroupSizes_;
  }
  /**
   * $(I Property.) Assigns the number of digits int each group to the left of the decimal place in numbers.
   * Params: value = The number of digits int each group to the left of the decimal place in numbers.
   */
  @property public final void numberGroupSizes(const(int)[] value) {
    checkReadOnly();
    numberGroupSizes_ = value;
  }

  /**
   * $(I Property.) Retrieves the number of digits int each group to the left of the decimal place in currency values.
   * Returns: The number of digits int each group to the left of the decimal place in currency values.
   */
  @property public const final const(int)[] currencyGroupSizes() {
    return currencyGroupSizes_;
  }
  /**
   * $(I Property.) Assigns the number of digits int each group to the left of the decimal place in currency values.
   * Params: value = The number of digits int each group to the left of the decimal place in currency values.
   */
  @property public final void currencyGroupSizes(const(int)[] value) {
    checkReadOnly();
    currencyGroupSizes_ = value;
  }

  /**
   * $(I Property.) Retrieves the string separating groups of digits to the left of the decimal place in numbers.
   * Returns: The string separating groups of digits to the left of the decimal place in numbers. For example, ",".
   */
  @property public const final const(char)[] numberGroupSeparator() {
    return numberGroupSeparator_;
  }
  /**
   * $(I Property.) Assigns the string separating groups of digits to the left of the decimal place in numbers.
   * Params: value = The string separating groups of digits to the left of the decimal place in numbers.
   */
  @property public final void numberGroupSeparator(const(char)[] value) {
    checkReadOnly();
    numberGroupSeparator_ = value;
  }

  /**
   * $(I Property.) Retrieves the string used as the decimal separator in numbers.
   * Returns: The string used as the decimal separator in numbers. For example, ".".
   */
  @property public const final const(char)[] numberDecimalSeparator() {
    return numberDecimalSeparator_;
  }
  /**
   * $(I Property.) Assigns the string used as the decimal separator in numbers.
   * Params: value = The string used as the decimal separator in numbers.
   */
  @property public final void numberDecimalSeparator(const(char)[] value) {
    checkReadOnly();
    numberDecimalSeparator_ = value;
  }

  /**
   * $(I Property.) Retrieves the string separating groups of digits to the left of the decimal place in currency values.
   * Returns: The string separating groups of digits to the left of the decimal place in currency values. For example, ",".
   */
  @property public const final const(char)[] currencyGroupSeparator() {
    return currencyGroupSeparator_;
  }
  /**
   * $(I Property.) Assigns the string separating groups of digits to the left of the decimal place in currency values.
   * Params: value = The string separating groups of digits to the left of the decimal place in currency values.
   */
  @property public final void currencyGroupSeparator(const(char)[] value) {
    checkReadOnly();
    currencyGroupSeparator_ = value;
  }

  /**
   * $(I Property.) Retrieves the string used as the decimal separator in currency values.
   * Returns: The string used as the decimal separator in currency values. For example, ".".
   */
  @property public const final const(char)[] currencyDecimalSeparator() {
    return currencyDecimalSeparator_;
  }
  /**
   * $(I Property.) Assigns the string used as the decimal separator in currency values.
   * Params: value = The string used as the decimal separator in currency values.
   */
  @property public final void currencyDecimalSeparator(const(char)[] value) {
    checkReadOnly();
    currencyDecimalSeparator_ = value;
  }

  /**
   * $(I Property.) Retrieves the string used as the currency symbol.
   * Returns: The string used as the currency symbol. For example, "£".
   */
  @property public const final const(char)[] currencySymbol() {
    return currencySymbol_;
  }
  /**
   * $(I Property.) Assigns the string used as the currency symbol.
   * Params: value = The string used as the currency symbol.
   */
  @property public final void currencySymbol(const(char)[] value) {
    checkReadOnly();
    currencySymbol_ = value;
  }

  /**
   * $(I Property.) Retrieves the string denoting that a number is negative.
   * Returns: The string denoting that a number is negative. For example, "-".
   */
  @property public const final const(char)[] negativeSign() {
    return negativeSign_;
  }
  /**
   * $(I Property.) Assigns the string denoting that a number is negative.
   * Params: value = The string denoting that a number is negative.
   */
  @property public final void negativeSign(const(char)[] value) {
    checkReadOnly();
    negativeSign_ = value;
  }

  /**
   * $(I Property.) Retrieves the string denoting that a number is positive.
   * Returns: The string denoting that a number is positive. For example, "+".
   */
  @property public const final const(char)[] positiveSign() {
    return positiveSign_;
  }
  /**
   * $(I Property.) Assigns the string denoting that a number is positive.
   * Params: value = The string denoting that a number is positive.
   */
  @property public final void positiveSign(const(char)[] value) {
    checkReadOnly();
    positiveSign_ = value;
  }

  /**
   * $(I Property.) Retrieves the string representing the NaN (not a number) value.
   * Returns: The string representing the NaN value. For example, "NaN".
   */
  @property public const final const(char)[] nanSymbol() {
    return nanSymbol_;
  }
  /**
   * $(I Property.) Assigns the string representing the NaN (not a number) value.
   * Params: value = The string representing the NaN value.
   */
  @property public final void nanSymbol(const(char)[] value) {
    checkReadOnly();
    nanSymbol_ = value;
  }

  /**
   * $(I Property.) Retrieves the string representing negative infinity.
   * Returns: The string representing negative infinity. For example, "-Infinity".
   */
  @property public const final const(char)[] negativeInfinitySymbol() {
    return negativeInfinitySymbol_;
  }
  /**
   * $(I Property.) Assigns the string representing negative infinity.
   * Params: value = The string representing negative infinity.
   */
  @property public final void negativeInfinitySymbol(const(char)[] value) {
    checkReadOnly();
    negativeInfinitySymbol_ = value;
  }

  /**
   * $(I Property.) Retrieves the string representing positive infinity.
   * Returns: The string representing positive infinity. For example, "Infinity".
   */
  @property public const final const(char)[] positiveInfinitySymbol() {
    return positiveInfinitySymbol_;
  }
  /**
   * $(I Property.) Assigns the string representing positive infinity.
   * Params: value = The string representing positive infinity.
   */
  @property public final void positiveInfinitySymbol(const(char)[] value) {
    checkReadOnly();
    positiveInfinitySymbol_ = value;
  }

  /**
   * $(I Property.) Retrieves a string array of native equivalents of the digits 0 to 9.
   * Returns: A string array of native equivalents of the digits 0 to 9.
   */
  @property public const final const(char[])[] nativeDigits() {
    return nativeDigits_;
  }
  /**
   * $(I Property.) Assigns a string array of native equivalents of the digits 0 to 9.
   * Params: value = A string array of native equivalents of the digits 0 to 9.
   */
  @property public final void nativeDigits(const(char[])[] value) {
    checkReadOnly();
    nativeDigits_ = value;
  }

  @property private const void checkReadOnly() {
    if (isReadOnly_)
        error("NumberFormat instance is read-only.");
  }

}

/**
 * $(ANCHOR _DateTimeFormat)
 * Determines how $(LINK2 #Time, Time) values are formatted, depending on the culture.
 * Remarks: To create a DateTimeFormat for a specific culture, create a $(LINK2 #Culture, Culture) for that culture and
 * retrieve its $(LINK2 #Culture_dateTimeFormat, dateTimeFormat) property. To create a DateTimeFormat for the user's current 
 * culture, use the $(LINK2 #Culture_current, current) property.
 */
public class DateTimeFormat : IFormatService {

  private enum const(char)[] rfc1123Pattern_ = "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'";
  private enum const(char)[] sortableDateTimePattern_ = "yyyy'-'MM'-'dd'T'HH':'mm':'ss";
  private enum const(char)[] universalSortableDateTimePattern_ = "yyyy'-'MM'-'dd' 'HH':'mm':'ss'Z'";
  private enum const(char)[] allStandardFormats = [ 'd', 'D', 'f', 'F', 'g', 'G', 'm', 'M', 'r', 'R', 's', 't', 'T', 'u', 'U', 'y', 'Y' ];


  package bool isReadOnly_;
  private static DateTimeFormat invariantFormat_;
  private const(CultureData)* cultureData_;

  private Calendar calendar_;
  private const(int)[] optionalCalendars_;
  private int firstDayOfWeek_ = -1;
  private int calendarWeekRule_ = -1;
  private const(char)[] dateSeparator_;
  private const(char)[] timeSeparator_;
  private const(char)[] amDesignator_;
  private const(char)[] pmDesignator_;
  private const(char)[] shortDatePattern_;
  private const(char)[] shortTimePattern_;
  private const(char)[] longDatePattern_;
  private const(char)[] longTimePattern_;
  private const(char)[] monthDayPattern_;
  private const(char)[] yearMonthPattern_;
  private const(char[])[] abbreviatedDayNames_;
  private const(char[])[] dayNames_;
  private const(char[])[] abbreviatedMonthNames_;
  private const(char[])[] monthNames_;

  private const(char)[] fullDateTimePattern_;
  private const(char)[] generalShortTimePattern_;
  private const(char)[] generalLongTimePattern_;

  private const(char[])[] shortTimePatterns_;
  private const(char[])[] shortDatePatterns_;
  private const(char[])[] longTimePatterns_;
  private const(char[])[] longDatePatterns_;
  private const(char[])[] yearMonthPatterns_;

  /**
   * $(ANCHOR DateTimeFormat_ctor)
   * Initializes an instance that is writable and culture-independent.
   */
  package this() {
    // This ctor is used by invariantFormat so we can't set the calendar property.
    cultureData_ = Culture.invariantCulture.cultureData_;
    calendar_ = Gregorian.generic;
    initialize();
  }

  package this(const(CultureData)* cultureData, Calendar calendar) {
    cultureData_ = cultureData;
    this.calendar = calendar;
  }

  /**
   * $(ANCHOR DateTimeFormat_getFormat)
   * Retrieves an object defining how to format the specified type.
   * Params: type = The TypeInfo of the resulting formatting object.
   * Returns: If type is typeid(DateTimeFormat), the current DateTimeFormat instance. Otherwise, null.
   * Remarks: Implements $(LINK2 #IFormatService_getFormat, IFormatService.getFormat).
   */
  public Object getFormat(TypeInfo type) {
    return (type is typeid(DateTimeFormat)) ? this : null;
  }

version(Clone)
{
  /**
   */
  public Object clone() {
    DateTimeFormat other = cast(DateTimeFormat)cloneObject(this);
    other.calendar_ = cast(Calendar)calendar.clone();
    other.isReadOnly_ = false;
    return other;
  }
}

  @property package const(char)[][] shortTimePatterns() {
    if (shortTimePatterns_ is null)
      shortTimePatterns_ = cultureData_.shortTimes;
    return shortTimePatterns_.dup;
  }

  @property package const(char)[][] shortDatePatterns() {
    if (shortDatePatterns_ is null)
      shortDatePatterns_ = cultureData_.shortDates;
    return shortDatePatterns_.dup;
  }

  @property package const(char)[][] longTimePatterns() {
    if (longTimePatterns_ is null)
      longTimePatterns_ = cultureData_.longTimes;
    return longTimePatterns_.dup;
  }

  @property package const(char)[][] longDatePatterns() {
    if (longDatePatterns_ is null)
      longDatePatterns_ = cultureData_.longDates;
    return longDatePatterns_.dup;
  }

  @property package const(char[])[] yearMonthPatterns() {
    if (yearMonthPatterns_ is null)
      yearMonthPatterns_ = cultureData_.yearMonths;
    return yearMonthPatterns_;
  }

  /**
   * $(ANCHOR DateTimeFormat_getAllDateTimePatterns)
   * Retrieves the standard patterns in which Time values can be formatted.
   * Returns: An array of strings containing the standard patterns in which Time values can be formatted.
   */
  public final const(char)[][] getAllDateTimePatterns() {
    const(char)[][] result;
    foreach (char format; DateTimeFormat.allStandardFormats)
      result ~= getAllDateTimePatterns(format);
    return result;
  }

  /**
   * $(ANCHOR DateTimeFormat_getAllDateTimePatterns_char)
   * Retrieves the standard patterns in which Time values can be formatted using the specified format character.
   * Returns: An array of strings containing the standard patterns in which Time values can be formatted using the specified format character.
   */
  public final const(char)[][] getAllDateTimePatterns(char format) {

    const(char)[][] combinePatterns(const(char)[][] patterns1, const(char)[][] patterns2) {
      const(char)[][] result = new const(char)[][patterns1.length * patterns2.length];
      for (int i = 0; i < patterns1.length; i++) {
        for (int j = 0; j < patterns2.length; j++)
          result[i * patterns2.length + j] = patterns1[i] ~ " " ~ patterns2[j];
      }
      return result;
    }

    // format must be one of allStandardFormats.
    const(char)[][] result;
    switch (format) {
      case 'd':
        result ~= shortDatePatterns;
        break;
      case 'D':
        result ~= longDatePatterns;
        break;
      case 'f':
        result ~= combinePatterns(longDatePatterns, shortTimePatterns);
        break;
      case 'F':
        result ~= combinePatterns(longDatePatterns, longTimePatterns);
        break;
      case 'g':
        result ~= combinePatterns(shortDatePatterns, shortTimePatterns);
        break;
      case 'G':
        result ~= combinePatterns(shortDatePatterns, longTimePatterns);
        break;
      case 'm':
      case 'M':
        result ~= monthDayPattern;
        break;
      case 'r':
      case 'R':
        result ~= rfc1123Pattern_;
        break;
      case 's':
        result ~= sortableDateTimePattern_;
        break;
      case 't':
        result ~= shortTimePatterns;
        break;
      case 'T':
        result ~= longTimePatterns;
        break;
      case 'u':
        result ~= universalSortableDateTimePattern_;
        break;
      case 'U':
        result ~= combinePatterns(longDatePatterns, longTimePatterns);
        break;
      case 'y':
      case 'Y':
        result ~= yearMonthPatterns;
        break;
      default:
        error("The specified format was not valid.");
    }
    return result;
  }

  /**
   * $(ANCHOR DateTimeFormat_getAbbreviatedDayName)
   * Retrieves the abbreviated name of the specified day of the week based on the culture of the instance.
   * Params: dayOfWeek = A DayOfWeek value.
   * Returns: The abbreviated name of the day of the week represented by dayOfWeek.
   */
  public final const(char)[] getAbbreviatedDayName(Calendar.DayOfWeek dayOfWeek) {
    return abbreviatedDayNames[cast(int)dayOfWeek];
  }

  /**
   * $(ANCHOR DateTimeFormat_getDayName)
   * Retrieves the full name of the specified day of the week based on the culture of the instance.
   * Params: dayOfWeek = A DayOfWeek value.
   * Returns: The full name of the day of the week represented by dayOfWeek.
   */
  public final const(char)[] getDayName(Calendar.DayOfWeek dayOfWeek) {
    return dayNames[cast(int)dayOfWeek];
  }

  /**
   * $(ANCHOR DateTimeFormat_getAbbreviatedMonthName)
   * Retrieves the abbreviated name of the specified month based on the culture of the instance.
   * Params: month = An integer between 1 and 13 indicating the name of the _month to return.
   * Returns: The abbreviated name of the _month represented by month.
   */
  public final const(char)[] getAbbreviatedMonthName(int month) {
    return abbreviatedMonthNames[month - 1];
  }

  /**
   * $(ANCHOR DateTimeFormat_getMonthName)
   * Retrieves the full name of the specified month based on the culture of the instance.
   * Params: month = An integer between 1 and 13 indicating the name of the _month to return.
   * Returns: The full name of the _month represented by month.
   */
  public final const(char)[] getMonthName(int month) {
    return monthNames[month - 1];
  }

  /**
   * $(ANCHOR DateTimeFormat_getInstance)
   * Retrieves the DateTimeFormat for the specified IFormatService.
   * Params: formatService = The IFormatService used to retrieve DateTimeFormat.
   * Returns: The DateTimeFormat for the specified IFormatService.
   * Remarks: The method calls $(LINK2 #IFormatService_getFormat, IFormatService.getFormat) with typeof(DateTimeFormat). If formatService is null,
   * then the value of the current property is returned.
   */
  public static DateTimeFormat getInstance(IFormatService formatService) {
    Culture culture = cast(Culture)formatService;
    if (culture !is null) {
      if (culture.dateTimeFormat_ !is null)
        return culture.dateTimeFormat_;
      return culture.dateTimeFormat;
    }
    if (DateTimeFormat dateTimeFormat = cast(DateTimeFormat)formatService)
      return dateTimeFormat;
    if (formatService !is null) {
      if (DateTimeFormat dateTimeFormat = cast(DateTimeFormat)(formatService.getFormat(typeid(DateTimeFormat))))
        return dateTimeFormat;
    }
    return current;
  }

  /**
   * $(ANCHOR DateTimeFormat_current)
   * $(I Property.) Retrieves a read-only DateTimeFormat instance from the current culture.
   * Returns: A read-only DateTimeFormat instance from the current culture.
   */
  @property public static DateTimeFormat current() {
    return Culture.current.dateTimeFormat;
  }

  /**
   * $(ANCHOR DateTimeFormat_invariantFormat)
   * $(I Property.) Retrieves a read-only DateTimeFormat instance that is culturally independent.
   * Returns: A read-only DateTimeFormat instance that is culturally independent.
   */
  public static DateTimeFormat invariantFormat() {
    if (invariantFormat_ is null) {
      invariantFormat_ = new DateTimeFormat;
      invariantFormat_.calendar = new Gregorian();
      invariantFormat_.isReadOnly_ = true;
    }
    return invariantFormat_;
  }

  /**
   * $(ANCHOR DateTimeFormat_isReadOnly)
   * $(I Property.) Retrieves a value indicating whether the instance is read-only.
   * Returns: true is the instance is read-only; otherwise, false.
   */
  @property public final bool isReadOnly() {
    return isReadOnly_;
  }

  /**
   * $(I Property.) Retrieves the calendar used by the current culture.
   * Returns: The Calendar determining the calendar used by the current culture. For example, the Gregorian.
   */
  @property public final Calendar calendar() {
    assert(calendar_ !is null);
    return calendar_;
  }
  /**
   * $(ANCHOR DateTimeFormat_calendar)
   * $(I Property.) Assigns the calendar to be used by the current culture.
   * Params: value = The Calendar determining the calendar to be used by the current culture.
   * Exceptions: If value is not valid for the current culture, an Exception is thrown.
   */
  @property public final void calendar(Calendar value) {
    checkReadOnly();
    if (value !is calendar_) {
      for (int i = 0; i < optionalCalendars.length; i++) {
        if (optionalCalendars[i] == value.id) {
          if (calendar_ !is null) {
            // Clear current properties.
            shortDatePattern_ = null;
            longDatePattern_ = null;
            shortTimePattern_ = null;
            yearMonthPattern_ = null;
            monthDayPattern_ = null;
            generalShortTimePattern_ = null;
            generalLongTimePattern_ = null;
            fullDateTimePattern_ = null;
            shortDatePatterns_ = null;
            longDatePatterns_ = null;
            yearMonthPatterns_ = null;
            abbreviatedDayNames_ = null;
            abbreviatedMonthNames_ = null;
            dayNames_ = null;
            monthNames_ = null;
          }
          calendar_ = value;
          initialize();
          return;
        }
      }
      error("Not a valid calendar for the culture.");
    }
  }

  /**
   * $(ANCHOR DateTimeFormat_firstDayOfWeek)
   * $(I Property.) Retrieves the first day of the week.
   * Returns: A DayOfWeek value indicating the first day of the week.
   */
  @property public final Calendar.DayOfWeek firstDayOfWeek() {
    return cast(Calendar.DayOfWeek)firstDayOfWeek_;
  }
  /**
   * $(I Property.) Assigns the first day of the week.
   * Params: valie = A DayOfWeek value indicating the first day of the week.
   */
  @property public final void firstDayOfWeek(Calendar.DayOfWeek value) {
    checkReadOnly();
    firstDayOfWeek_ = value;
  }

  /**
   * $(ANCHOR DateTimeFormat_calendarWeekRule)
   * $(I Property.) Retrieves the _value indicating the rule used to determine the first week of the year.
   * Returns: A CalendarWeekRule _value determining the first week of the year.
   */
  @property public final Calendar.WeekRule calendarWeekRule() {
    return cast(Calendar.WeekRule) calendarWeekRule_;
  }
  /**
   * $(I Property.) Assigns the _value indicating the rule used to determine the first week of the year.
   * Params: value = A CalendarWeekRule _value determining the first week of the year.
   */
  @property public final void calendarWeekRule(Calendar.WeekRule value) {
    checkReadOnly();
    calendarWeekRule_ = value;
  }

  /**
   * $(ANCHOR DateTimeFormat_nativeCalendarName)
   * $(I Property.) Retrieves the native name of the calendar associated with the current instance.
   * Returns: The native name of the calendar associated with the current instance.
   */
  @property public final const(char)[] nativeCalendarName() {
    return cultureData_.nativeCalName;
  }

  /**
   * $(ANCHOR DateTimeFormat_dateSeparator)
   * $(I Property.) Retrieves the string separating date components.
   * Returns: The string separating date components.
   */
  @property public final const(char)[] dateSeparator() {
    if (dateSeparator_ is null)
      dateSeparator_ = cultureData_.date;
    return dateSeparator_;
  }
  /**
   * $(I Property.) Assigns the string separating date components.
   * Params: value = The string separating date components.
   */
  @property public final void dateSeparator(const(char)[] value) {
    checkReadOnly();
    dateSeparator_ = value;
  }

  /**
   * $(ANCHOR DateTimeFormat_timeSeparator)
   * $(I Property.) Retrieves the string separating time components.
   * Returns: The string separating time components.
   */
  @property public final const(char)[] timeSeparator() {
    if (timeSeparator_ is null)
      timeSeparator_ = cultureData_.time;
    return timeSeparator_;
  }
  /**
   * $(I Property.) Assigns the string separating time components.
   * Params: value = The string separating time components.
   */
  @property public final void timeSeparator(const(char)[] value) {
    checkReadOnly();
    timeSeparator_ = value;
  }

  /**
   * $(ANCHOR DateTimeFormat_amDesignator)
   * $(I Property.) Retrieves the string designator for hours before noon.
   * Returns: The string designator for hours before noon. For example, "AM".
   */
  @property public final const(char)[] amDesignator() {
    assert(amDesignator_ !is null);
    return amDesignator_;
  }
  /**
   * $(I Property.) Assigns the string designator for hours before noon.
   * Params: value = The string designator for hours before noon.
   */
  @property public final void amDesignator(const(char)[] value) {
    checkReadOnly();
    amDesignator_ = value;
  }

  /**
   * $(ANCHOR DateTimeFormat_pmDesignator)
   * $(I Property.) Retrieves the string designator for hours after noon.
   * Returns: The string designator for hours after noon. For example, "PM".
   */
  @property public final const(char)[] pmDesignator() {
    assert(pmDesignator_ !is null);
    return pmDesignator_;
  }
  /**
   * $(I Property.) Assigns the string designator for hours after noon.
   * Params: value = The string designator for hours after noon.
   */
  @property public final void pmDesignator(const(char)[] value) {
    checkReadOnly();
    pmDesignator_ = value;
  }

  /**
   * $(ANCHOR DateTimeFormat_shortDatePattern)
   * $(I Property.) Retrieves the format pattern for a short date value.
   * Returns: The format pattern for a short date value.
   */
  @property public final const(char)[] shortDatePattern() {
    assert(shortDatePattern_ !is null);
    return shortDatePattern_;
  }
  /**
   * $(I Property.) Assigns the format pattern for a short date _value.
   * Params: value = The format pattern for a short date _value.
   */
  @property public final void shortDatePattern(const(char)[] value) {
    checkReadOnly();
    if (shortDatePatterns_ !is null)
      shortDatePatterns_ = [value];
    shortDatePattern_ = value;
    generalLongTimePattern_ = null;
    generalShortTimePattern_ = null;
  }

  /**
   * $(ANCHOR DateTimeFormat_shortTimePattern)
   * $(I Property.) Retrieves the format pattern for a short time value.
   * Returns: The format pattern for a short time value.
   */
  @property public final const(char)[] shortTimePattern() {
    if (shortTimePattern_ is null)
      shortTimePattern_ = cultureData_.shortTime;
    return shortTimePattern_;
  }
  /**
   * $(I Property.) Assigns the format pattern for a short time _value.
   * Params: value = The format pattern for a short time _value.
   */
  @property public final void shortTimePattern(const(char)[] value) {
    checkReadOnly();
    shortTimePattern_ = value;
    generalShortTimePattern_ = null;
  }

  /**
   * $(ANCHOR DateTimeFormat_longDatePattern)
   * $(I Property.) Retrieves the format pattern for a long date value.
   * Returns: The format pattern for a long date value.
   */
  @property public final const(char)[] longDatePattern() {
    assert(longDatePattern_ !is null);
    return longDatePattern_;
  }
  /**
   * $(I Property.) Assigns the format pattern for a long date _value.
   * Params: value = The format pattern for a long date _value.
   */
  @property public final void longDatePattern(const(char)[] value) {
    checkReadOnly();
    if (longDatePatterns_ !is null)
      longDatePatterns_ = [value];
    longDatePattern_ = value;
    fullDateTimePattern_ = null;
  }

  /**
   * $(ANCHOR DateTimeFormat_longTimePattern)
   * $(I Property.) Retrieves the format pattern for a long time value.
   * Returns: The format pattern for a long time value.
   */
  @property public final const(char)[] longTimePattern() {
    assert(longTimePattern_ !is null);
    return longTimePattern_;
  }
  /**
   * $(I Property.) Assigns the format pattern for a long time _value.
   * Params: value = The format pattern for a long time _value.
   */
  @property public final void longTimePattern(const(char)[] value) {
    checkReadOnly();
    longTimePattern_ = value;
    fullDateTimePattern_ = null;
  }

  /**
   * $(ANCHOR DateTimeFormat_monthDayPattern)
   * $(I Property.) Retrieves the format pattern for a month and day value.
   * Returns: The format pattern for a month and day value.
   */
  @property public final const(char)[] monthDayPattern() {
    if (monthDayPattern_ is null)
      monthDayPattern_ = cultureData_.monthDay;
    return monthDayPattern_;
  }
  /**
   * $(I Property.) Assigns the format pattern for a month and day _value.
   * Params: value = The format pattern for a month and day _value.
   */
  @property public final void monthDayPattern(const(char)[] value) {
    checkReadOnly();
    monthDayPattern_ = value;
  }

  /**
   * $(ANCHOR DateTimeFormat_yearMonthPattern)
   * $(I Property.) Retrieves the format pattern for a year and month value.
   * Returns: The format pattern for a year and month value.
   */
  @property public final const(char)[] yearMonthPattern() {
    assert(yearMonthPattern_ !is null);
    return yearMonthPattern_;
  }
  /**
   * $(I Property.) Assigns the format pattern for a year and month _value.
   * Params: value = The format pattern for a year and month _value.
   */
  @property public final void yearMonthPattern(const(char)[] value) {
    checkReadOnly();
    yearMonthPattern_ = value;
  }

  /**
   * $(ANCHOR DateTimeFormat_abbreviatedDayNames)
   * $(I Property.) Retrieves a string array containing the abbreviated names of the days of the week.
   * Returns: A string array containing the abbreviated names of the days of the week. For $(LINK2 #DateTimeFormat_invariantFormat, invariantFormat),
   *   this contains "Sun", "Mon", "Tue", "Wed", "Thu", "Fri" and "Sat".
   */
  @property public final const(char)[][] abbreviatedDayNames() {
    if (abbreviatedDayNames_ is null)
      abbreviatedDayNames_ = cultureData_.abbrevDayNames;
    return abbreviatedDayNames_.dup;
  }
  /**
   * $(I Property.) Assigns a string array containing the abbreviated names of the days of the week.
   * Params: value = A string array containing the abbreviated names of the days of the week.
   */
  @property public final void abbreviatedDayNames(const(char)[][] value) {
    checkReadOnly();
    abbreviatedDayNames_ = value;
  }

  /**
   * $(ANCHOR DateTimeFormat_dayNames)
   * $(I Property.) Retrieves a string array containing the full names of the days of the week.
   * Returns: A string array containing the full names of the days of the week. For $(LINK2 #DateTimeFormat_invariantFormat, invariantFormat),
   *   this contains "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" and "Saturday".
   */
  @property public final const(char)[][] dayNames() {
    if (dayNames_ is null)
      dayNames_ = cultureData_.dayNames;
    return dayNames_.dup;
  }
  /**
   * $(I Property.) Assigns a string array containing the full names of the days of the week.
   * Params: value = A string array containing the full names of the days of the week.
   */
  @property public final void dayNames(const(char)[][] value) {
    checkReadOnly();
    dayNames_ = value;
  }

  /**
   * $(ANCHOR DateTimeFormat_abbreviatedMonthNames)
   * $(I Property.) Retrieves a string array containing the abbreviated names of the months.
   * Returns: A string array containing the abbreviated names of the months. For $(LINK2 #DateTimeFormat_invariantFormat, invariantFormat),
   *   this contains "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" and "".
   */
  @property public final const(char)[][] abbreviatedMonthNames() {
    if (abbreviatedMonthNames_ is null)
      abbreviatedMonthNames_ = cultureData_.abbrevMonthNames;
    return abbreviatedMonthNames_.dup;
  }
  /**
   * $(I Property.) Assigns a string array containing the abbreviated names of the months.
   * Params: value = A string array containing the abbreviated names of the months.
   */
  @property public final void abbreviatedMonthNames(const(char)[][] value) {
    checkReadOnly();
    abbreviatedMonthNames_ = value;
  }

  /**
   * $(ANCHOR DateTimeFormat_monthNames)
   * $(I Property.) Retrieves a string array containing the full names of the months.
   * Returns: A string array containing the full names of the months. For $(LINK2 #DateTimeFormat_invariantFormat, invariantFormat),
   *   this contains "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" and "".
   */
  @property public final const(char)[][] monthNames() {
    if (monthNames_ is null)
      monthNames_ = cultureData_.monthNames;
    return monthNames_.dup;
  }
  /**
   * $(I Property.) Assigns a string array containing the full names of the months.
   * Params: value = A string array containing the full names of the months.
   */
  @property public final void monthNames(const(char)[][] value) {
    checkReadOnly();
    monthNames_ = value;
  }

  /**
   * $(ANCHOR DateTimeFormat_fullDateTimePattern)
   * $(I Property.) Retrieves the format pattern for a long date and a long time value.
   * Returns: The format pattern for a long date and a long time value.
   */
  @property public final const(char)[] fullDateTimePattern() {
    if (fullDateTimePattern_ is null)
      fullDateTimePattern_ = longDatePattern ~ " " ~ longTimePattern;
    return fullDateTimePattern_;
  }
  /**
   * $(I Property.) Assigns the format pattern for a long date and a long time _value.
   * Params: value = The format pattern for a long date and a long time _value.
   */
  @property public final void fullDateTimePattern(const(char)[] value) {
    checkReadOnly();
    fullDateTimePattern_ = value;
  }

  /**
   * $(ANCHOR DateTimeFormat_rfc1123Pattern)
   * $(I Property.) Retrieves the format pattern based on the IETF RFC 1123 specification, for a time value.
   * Returns: The format pattern based on the IETF RFC 1123 specification, for a time value.
   */
  @property public const final const(char)[] rfc1123Pattern() {
    return rfc1123Pattern_;
  }

  /**
   * $(ANCHOR DateTimeFormat_sortableDateTimePattern)
   * $(I Property.) Retrieves the format pattern for a sortable date and time value.
   * Returns: The format pattern for a sortable date and time value.
   */
  @property public const final const(char)[] sortableDateTimePattern() {
    return sortableDateTimePattern_;
  }

  /**
   * $(ANCHOR DateTimeFormat_universalSortableDateTimePattern)
   * $(I Property.) Retrieves the format pattern for a universal date and time value.
   * Returns: The format pattern for a universal date and time value.
   */
  @property public const final const(char)[] universalSortableDateTimePattern() {
    return universalSortableDateTimePattern_;
  }

  @property package const(char)[] generalShortTimePattern() {
    if (generalShortTimePattern_ is null)
      generalShortTimePattern_ = shortDatePattern ~ " " ~ shortTimePattern;
    return generalShortTimePattern_;
  }

  @property package const(char)[] generalLongTimePattern() {
    if (generalLongTimePattern_ is null)
      generalLongTimePattern_ = shortDatePattern ~ " " ~ longTimePattern;
    return generalLongTimePattern_;
  }

  @property private const void checkReadOnly() {
    if (isReadOnly_)
        error("DateTimeFormat instance is read-only.");
  }

  private void initialize() {
    if (longTimePattern_ is null)
      longTimePattern_ = cultureData_.longTime;
    if (shortDatePattern_ is null)
      shortDatePattern_ = cultureData_.shortDate;
    if (longDatePattern_ is null)
      longDatePattern_ = cultureData_.longDate;
    if (yearMonthPattern_ is null)
      yearMonthPattern_ = cultureData_.yearMonth;
    if (amDesignator_ is null)
      amDesignator_ = cultureData_.am;
    if (pmDesignator_ is null)
      pmDesignator_ = cultureData_.pm;
    if (firstDayOfWeek_ is -1)
      firstDayOfWeek_ = cultureData_.firstDayOfWeek;
    if (calendarWeekRule_ == -1)
      calendarWeekRule_ = cultureData_.firstDayOfYear;
  }

  @property private const(int)[] optionalCalendars() {
    if (optionalCalendars_ is null)
      optionalCalendars_ = cultureData_.optionalCalendars;
    return optionalCalendars_;
  }

  private const void error(immutable(char)[] msg) {
     throw new LocaleException (msg);
  }

}