Skip to content

ShortTimePattern does not respect the system configuration when in macOS #61148

Open

Description

@afvieira

Description

Regardless of the used Culture (InstalledUICulture,CurrentCulture,InvariantCulture,CurrentUICulture) the ShortTimePattern is always HH:mm. This is happening only when I run the code in macOS. When running the same code in windows the ShortTimePattern is respecting the system configurations.

You can see an example here. I built a sample project just to show the behavior.

As you can see when running this in windows, the hour format is ok:
2021-11-03 11-13-35 - My Boot Camp(1)

In macOS this is not working:
ezgif com-gif-maker (3)

Reproduction Steps

small project with repro steps: https://github.com/afvieira/tmp-ShortTimePattern-issue

Expected behavior

Respecting the system configurations in macOS. When I have the "24-Hour Time" setting unchecked, the time format should be h:mm tt instead of HH:mm

Actual behavior

When I have the "24-Hour Time" setting unchecked, the time format is ignoring this setting configuration.

Regression?

No response

Known Workarounds

No response

Configuration

  • Which version of .NET is the code running on?
    • .net5.0
  • What OS and version, and what distro if applicable?
    • image

Other information

No response

Activity

  1. ghost added
    untriaged
    on Nov 3, 2021
  2. added and removed
    untriaged
    on Nov 3, 2021
  3. tarekgh commented on Nov 3, 2021

    @tarekgh
    Member

    @afvieira thanks for the report. This is a limitation we currently have in .NET. This is something we are going to look at to support in the future releases. The workaround for that is manually read the setting from MacOS and use it for your formatting.

  4. added this to the 7.0.0 milestone on Nov 3, 2021
  5. afvieira commented on Nov 3, 2021

    @afvieira
    Author

    @afvieira thanks for the report. This is a limitation we currently have in .NET. This is something we are going to look at to support in the future releases. The workaround for that is manually read the setting from MacOS and use it for your formatting.

    Hi @tarekgh, thank you for your quick reply. Can you point me an example to read those settings from macOS?
    Thanks in advance

  6. tarekgh commented on Nov 5, 2021

    @tarekgh
    Member

    @afvieira here is a hacky example you may try. I couldn't find better way for now but when we get into this issue, we should provide a better way to read it. For now, you may try this example.
    Note, I didn't do much testing it, so make sure you test and polish it well before using it in any product.

    using System;
    using System.Globalization;
    using System.Diagnostics;
    using System.Text;
    
    namespace NetCoreApp
    {
        class Program
        {
            private static string NormalizeDateFormat(string dateFormat)
            {
                StringBuilder sb = new StringBuilder();
                int i =0;
                while (i < dateFormat.Length)
                {
                    char c = dateFormat[i];
                    switch (c)
                    {
                        case 'G':
                        case 'g': sb.Append('g'); break;
    
                        case 'y': sb.Append('y'); break;
                        
                        case 'L':
                        case 'M': sb.Append('M'); break;
    
                        case 'd': 
                        case 'E': sb.Append('d'); break;
    
                        case 'a': sb.Append("tt"); break;
    
                        case 'h':
                        case 'k':
                        case 'j': sb.Append('h'); break;
    
                        case 'H':
                        case 'K':
                        case 'J':
                        case 'C': sb.Append('H'); break;
    
                        case 'm': sb.Append('m'); break;
    
                        case 's': sb.Append('s'); break;
    
                        case 'z':
                        case 'Z': sb.Append('z'); break;
    
                        case '\'': 
                            sb.Append('\'');
                            i++;
    
                            while (i < dateFormat.Length) 
                            {
                                if (dateFormat[i] == '\'')
                                {
                                    sb.Append('\'');
                                    if (i < dateFormat.Length - 1 && dateFormat[i + 1] == '\'')
                                    {
                                        sb.Append('\'');
                                        i += 2;
                                        continue;
                                    }
                                    break;
                                }
                                sb.Append(dateFormat[i]);
                                i++;
                            }
                            break;
                        
                        default:
                            if ((c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && c != '\r' && c != '\n')
                            {
                                sb.Append(c);
                            }
                            break;
                    }
                    i++;
                }
                return sb.ToString();
            }
    
            static void Main(string[] args)
            {
                using Process process = new Process();
                process.StartInfo.FileName = "defaults";
                process.StartInfo.Arguments = "read com.apple.menuextra.clock DateFormat";
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.Start();
                string dateFormat = process.StandardOutput.ReadToEnd();
                string normalizedDateFormat = NormalizeDateFormat(dateFormat);
                Console.WriteLine($"{dateFormat} ... {normalizedDateFormat}");
                Console.WriteLine($"{DateTime.Now.ToString(normalizedDateFormat)}");
            }
        }
    }
    
  7. modified the milestones: 7.0.0, Future on Nov 21, 2021
  8. added
    os-macos
    on Nov 21, 2023
  9. danipen commented on Jan 17, 2024

    @danipen

    The same happens for the date. See discussion #97083. Any progress on this?

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions