Example #1
0
        /// <summary>
        /// Deprecated.
        /// </summary>
        /// <param name="ifp"></param>
        /// <param name="s"></param>
        /// <param name="dateFmt"></param>
        /// <param name="numFmt"></param>
        /// <returns></returns>
        public static string GetDisplayString(ImardaFormatProvider ifp, string s, string dateFmt, string numFmt)
        {
            if (string.IsNullOrEmpty(s)) return string.Empty;
            string display = s;

            if (s.Length == Formats.DateFmtLength && s[10] == 'T')
            {
                DateTime dt;
                if (DateTime.TryParseExact(s, "s", null, DateTimeStyles.None, out dt))
                {
                    dt = DateTime.SpecifyKind(dt, DateTimeKind.Utc);
                    DateTime? local = ImardaFormatProvider.DateTimeInUserZone(dt, ifp.TimeZoneInfo);
                    if (local.HasValue)
                    {
                        display = string.Format(ifp, dateFmt ?? "{0}", local.Value);
                        //if (dateFmt == null) dateFmt = ifp.GetDateFormat("~");
                        //display = dateFmt != null ? local.Value.ToString(dateFmt, ifp) : local.Value.ToString(ifp);
                        return display;
                    }
                    return "";
                }
            }

            decimal number;
            if (decimal.TryParse(s, NumberStyles.Number, ifp, out number))
            {
                display = numFmt != null ? number.ToString(numFmt, ifp) : number.ToString(ifp);
            }
            else
            {
                int p = s.IndexOf(':');
                if (p != -1)
                {
                    if (s.Length > p + 1)
                    {
                        char c = s[p + 1];
                        if (c == '-' || char.IsDigit(c))
                        {
                            try
                            {
                                display = Measurement.Format(s, ifp.MeasurementFormatProvider);
                            }
                            catch
                            {
                            }
                        }
                    }
                }
            }
            return display;
        }
 /// <summary>
 /// Fill in the template, replace identifiers in the template in angular brackets by $(identifier).
 /// Flatten the arrays in the data to simple identifiers
 /// </summary>
 /// <param name="timeZoneId"></param>
 /// <param name="template"></param>
 /// <param name="locale"></param>
 /// <param name="typedData"></param>
 /// <returns></returns>
 private static string FillInTemplate(string locale, string timeZoneId, string template, string typedData)
 {
     if (String.IsNullOrEmpty(template)) return String.Empty;
     //template = _rxTemplateParameter.Replace(template, @"$$($1)");
     template = template.Replace("<br>", "\r\n").Replace("&nbsp;", " ");
     var culture = new CultureInfo(locale);
     var mfi = new MeasurementFormatInfo(culture.NumberFormat);
     var ifp = new ImardaFormatProvider(culture, mfi, TimeZoneInfo.FindSystemTimeZoneById(timeZoneId));
     IDictionary args = EAHelper.MakeFormattedValues(typedData, ifp);
     string msg = new ConfigTemplate(template).Instantiate(args);
     return msg;
 }
Example #3
0
 internal static string Format(string format, object val, ImardaFormatProvider ifp)
 {
     if (val is DateTime)
     {
         //DateTime dt = (DateTime)val;
         //if (dt.Kind != DateTimeKind.Utc) throw new Exception();
         string s;
         if (ifp.ForceDefaultDateFormat)
         {
             DateTime? dtl = ImardaFormatProvider.DateTimeInUserZone((DateTime)val, ifp.TimeZoneInfo);
             if (dtl.HasValue)
             {
                 string contextFormat = ifp.GetDateFormat("") ?? "g";
                 s = dtl.Value.ToString(contextFormat, ifp.DefaultCulture);
             }
             else s = String.Empty;
         }
         else
         {
             string fmt = format != null ? "{0:" + format + "}" : "{0:z} {0:t}";
             s = String.Format(ifp, fmt, val);
         }
         return s;
     }
     return ifp.Format(format, val, null);
 }
Example #4
0
 public static IDictionary MakeFormattedValues(string s, ImardaFormatProvider ifp)
 {
     var map = new HybridDictionary();
     Deserialize(s, map, ifp);
     return map;
 }
Example #5
0
        /// <summary>
        /// Deserialize the typed value string into a dictionary.
        /// </summary>
        /// <param name="s"></param>
        /// <param name="map"></param>
        /// <param name="ifp"></param>
        public static void Deserialize(string s, IDictionary map, ImardaFormatProvider ifp)
        {
            IDictionary hmap = StringUtils.KeyValueMap(s, ValueFormat.Mix, true);
            foreach (string key in hmap.Keys)
            {
                string name;
                Type type;
                int vari;
                string format;

                AnalyzeKey(key, out name, out type, out vari, out format);

                object val = hmap[key];
                if (vari >= 0)
                {
                    bool placeholderInKey = name.Contains("_");
                    string[] arr = (val is string) ? new[] { (string)val } : (string[])val;
                    int n = Math.Max(arr.Length, vari);
                    var target = ifp != null ? new string[n] : Array.CreateInstance(type, n);
                    for (int i = 0; i < n; i++)
                    {
                        object v0 = (i >= arr.Length) ? GetDefaultValue(type) : Parse(type, arr[i]);
                        if (ifp != null) v0 = Format(format, v0, ifp);
                        target.SetValue(v0, i);
                        if (vari > i)
                        {
                            string num = (i + 1).ToString();
                            string keyi = placeholderInKey ? name.Replace("_", num) : name + "_" + num;
                            map[keyi] = v0;
                        }
                    }
                    map[name] = target;
                }
                else
                {
                    object v0 = Parse(type, (string)val);
                    if (ifp != null) v0 = Format(format, v0, ifp);
                    map[name] = v0;
                }
            }
        }
        private ImardaFormatProvider GetFormatter(Guid personID, Guid companyID, TimeZoneInfo tzi)
        {
            SimpleResponse<string> cultResp;
            var service = ImardaProxyManager.Instance.IImardaConfigurationProxy;
            string preferences = "";
            ChannelInvoker.Invoke(delegate(out IClientChannel channel)
            {
                channel = service as IClientChannel;

                cultResp = service.GetCulturePreferences(new GenericRequest(personID, companyID));
                ErrorHandler.Check(cultResp);
                preferences = cultResp.Value ?? "";
            });

            IDictionary prefMap = preferences.KeyValueMap(ValueFormat.Mix, true);
            string locale = (string)prefMap["Locale"];
            var ci = new CultureInfo(locale); // cannot use CultureInfo.GetCultureInfo() because we have to customize Infinity and NaN
            var mfi = new MeasurementFormatInfo(ci.NumberFormat);
            mfi.SetPreferences(prefMap);
            var ifp = new ImardaFormatProvider(ci, mfi, tzi);
            return ifp;
        }
        /// <summary>
        ///     Further initialize this object.
        ///     Pull information out of the SessionConfigGroup eligible for caching into this ConfiguredSessionObject,
        ///     because the SessionConfigGroup is going to be removed before caching.
        /// </summary>
        public void Initialize(LoginMode mode = LoginMode.Normal)
        {
            var dfltFormatProvider = PreferredCulture.IsNeutralCulture ? new CultureInfo("en-NZ") : PreferredCulture;
            var measurementFormatProvider = new MeasurementFormatInfo(dfltFormatProvider.NumberFormat);
            _preferredMeasurementUnits = _configuration.PreferredMeasurementUnits;
            IDictionary map = _preferredMeasurementUnits.KeyValueMap(ValueFormat.Strings, true);
            measurementFormatProvider.SetPreferences(map);
            _formatProvider = new ImardaFormatProvider(dfltFormatProvider, measurementFormatProvider, PreferredZone);

            if (mode != LoginMode.Mobile
                && mode != LoginMode.IAC)
            {
                RequiresTimeZoneConversion = true;
            }
        }