Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to specify culture #4

Open
mookid8000 opened this issue Jan 22, 2018 · 1 comment
Open

Ability to specify culture #4

mookid8000 opened this issue Jan 22, 2018 · 1 comment

Comments

@mookid8000
Copy link
Member

mookid8000 commented Jan 22, 2018

When formatting stuff to text, the thread's current culture is used e.g. to format decimals, which in Denmark could look like this:

0,18343

In English-speaking countries, it would look like this:

0.18343

and there will be differences in how dates, and maybe other stuff is formatted too.

It would be neat if all Format*** methods would accept a CultureInfo object as an optional argument, which would then be used in all value-to-text formatting methods.

@blackholeearth
Copy link

i have some thing like this.
this will make sure . datetime columns will format as YYYY-MM-DD
you can set the other number formats in the library.

how to use;


            var dt1= new datatable;

            //this will make sure .   datetime columns  will format as   YYYY-MM-DD
            var CHelp = DateTimeFn.NewCultureHelper();
            CHelp.Override_DatetimeToString_AsShortDate();  
            
            //do your table to string operation.
            var sb = new StringBuilder();

            //str-table-rows
            foreach (var dr in dt1.AsEnumerable() )  {
                sb.AppendLine(
                    string.Join(" , ", dr.ItemArray)
                    );
            }

            CHelp.RestoreCulture();

            var tableAsString = sb.ToString();

culturehelper.cs


using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;

namespace ErtasLib
{
    public class CultureHelper
    {
        CultureInfo cCulture_backup=null;
        CultureInfo cUICulture_backup=null;

        public Thread thread=null;

        public CultureHelper( )
        {
            thread = Thread.CurrentThread; 
            BackupCulture();

            ////example
            //CultureInfo standardizedCulture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
            //standardizedCulture.DateTimeFormat.DateSeparator = "-";
            //standardizedCulture.DateTimeFormat.LongDatePattern = "yyyy-MM-dd hh:mm:ss";
            //standardizedCulture.DateTimeFormat.FullDateTimePattern = "yyyy-MM-dd hh:mm:ss";
            //standardizedCulture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";

            //Thread.CurrentThread.CurrentCulture = standardizedCulture;
            //Thread.CurrentThread.CurrentUICulture = standardizedCulture;

        }

        public void Override_DatetimeToString_AsShortDate()
        {
            //example
            CultureInfo standardizedCulture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
            standardizedCulture.DateTimeFormat.DateSeparator = "-";
            //important  tostring() "G" general
            standardizedCulture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";
            standardizedCulture.DateTimeFormat.LongTimePattern = "";

            //Thread.CurrentThread.CurrentCulture = standardizedCulture;
            //Thread.CurrentThread.CurrentUICulture = standardizedCulture;
            thread.CurrentCulture = standardizedCulture;
            thread.CurrentUICulture = standardizedCulture;
        }

        public void BackupCulture(  )
        {     
                cCulture_backup = thread.CurrentCulture;
                cUICulture_backup = thread.CurrentUICulture;
        }

        public void RestoreCulture()
        {
            //backup cultrure
            thread.CurrentCulture = cCulture_backup  ;
            thread.CurrentUICulture = cUICulture_backup;

        }


    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants