Search code examples
c#asp.net-coreswitch-statement.net-6.0

A constant value is expected (switch TimeOnly in C#)


I am totally new in C#, learning from scratch. Now with practicing switch statement I want to compare TimeOnly variable for meeting time in day. But VS returns me "A Constant value is expected".



using System.Globalization;

TimeOnly testTimeOnly = TimeOnly.ParseExact("00:00:00", "HH:mm:ss", CultureInfo.InvariantCulture);
TimeOnly testMorning = TimeOnly.ParseExact("06:00:00", "HH:mm:ss", CultureInfo.InvariantCulture);


switch (testTimeOnly)
{

    case >= TimeOnly.Parse("00:00:00") and < testMorning:
        Console.WriteLine("Its night right now.");
        break;

    default:
        Console.WriteLine("Unknown part of the day!");
        break;
}

I tried some parsing, using hardcoded value or variable, but result is the same. Even declaring variable as constant does not help.

In some other thread I found, that it can occur because value must be compiled, but do not know, how to write values other way.

I want to compare testTimeOnly to some time inverval and get some message.

Can you help me, please?


Solution

  • I tried some parsing, using hardcoded value or variable, but result is the same. Even declaring variable as constant does not help

    Actually, if you check DateTime Struct you would seen Hour property within it which should look like as following:

    enter image description here

    Therefore, when you would pass or evaluate any time comparison you ought to extract the hour from given format for your scenario which is HH:mm:ss. Once you would extract the hour now you could execute your comparison using switch statement.

    Let's check in action what you are trying to achieve.

                TimeOnly testTimeOnly = TimeOnly.ParseExact("00:00:00", "HH:mm:ss", CultureInfo.InvariantCulture);
                TimeOnly testMorning = TimeOnly.ParseExact("06:00:00", "HH:mm:ss", CultureInfo.InvariantCulture);
    
                var checkNight = testTimeOnly.Hour;
                var checkMorning = testMorning.Hour;
                switch (checkNight)
                {
                    case int time when (time >= 6 && time <= 12):
                        Console.WriteLine($"Hello , good morning");
                        break;
                    case int time when (time >= 12 && time <= 17):
                        Console.WriteLine($"Hello , good afternoon");
                        break;
                    case int time when (time >= 0):
                        Console.WriteLine($"Hello ,{testTimeOnly} Its night right now");
                        break;
                    default:
                        Console.WriteLine($"Unknown part of the day!");
                        break;
                       
                }
               
    

    Note: As you can see I am extracting hour from testTimeOnly which will return 0 means over 23.

    Output:

    enter image description here

    Daynamic Comparison:

    However, if you would like to extract and compare dynamic time reagrdless of time zone the elegant way would be as following:

                DateTime dateTime = DateTime.Now;
                DateTime utcTime = dateTime.ToUniversalTime();
                TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("China Standard Time");
    
                DateTime yourLocalTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, cstZone);
                var hour = yourLocalTime.Hour;
    
                switch (hour)
                {
                    case int time when (time >= 0 && time <= 5):
                        Console.WriteLine($"Hello ,Its mid night now");
                        break;
                    case int time when (time >= 6 && time <= 12):
                        Console.WriteLine($"Hello , Its morning now");
                        break;
                    case int time when (time >= 12 && time <= 17):
                        Console.WriteLine($"Hello , Its after noon");
                        break;
                    case int time when (time >= 17 && time <= 19):
                        Console.WriteLine($"Hello , Its evening");
                        break;
                    case int time when (time >= 19 && time <= 23):
                        Console.WriteLine($"Hello , Its Night");
                        break;
                    default:
                        Console.WriteLine($"Hello and welcome!");
                        break;
                }
    

    In addition to this, you can get your required time zone here.

    Note: If you would like to know more details on time conversion you could check our official document here