Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
83 views
in Technique[技术] by (71.8m points)

c# - ASP.Net DateTime drama

This seems so basic but I've been researching this (here, other sites, Microsoft documentation, etc.) and working on it for months. I'm just not getting the problem.

I have a program that uses DateTime a lot. It worked great before DST last fall. Then everything stopped working well - seems like everything's off by an hour now. I compensated with DateTime.Now.AddHours() but that's only a band aid.

I've tried DateTimeOffset.Now, DateTime.ToLocalTime, DateTime.UTCNow, etc. It just won't use system time or compensate.

I'm in Pacific Standard time and nobody will ever use this site outside of this time zone if that's another way to approach it.

Anyone have any thoughts on why this is happening? The whole program is doing it, but I'll give a little snippit of one of the areas.

    <table class="table-bordered" id="table_lunch" style="width:15em;">
        @*populate the table with only those breaks that lack a TimeCleared value*@
        @if (ViewBag.Lunches != null)
        {
            foreach (var item in modelOrdered)
            {
                if (DateTimeOffset.Now.AddHours(-1) >= item.LunchTime.AddMinutes(-1) && DateTimeOffset.Now.AddHours(-1) <= item.LunchTime.AddMinutes(1))
                {
                    if (item.EmpSent == false)
                    {
                        <tr class="listTime">
                            @*Make each name clickable - deletes the lunch*@
                            <td class="LunchIdNumber">
                                @if (item.LongerLunch == false && item.Double == false)
                                {
                                    <input type="hidden" class="hiddenLunchID" value="@item.LunchID" />
                                    <a href="Javascript:;" class="empNameLunch" style="color:black">@item.Employee.DisplayName</a>
question from:https://stackoverflow.com/questions/65875677/asp-net-datetime-drama

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's really hard to answer your question without a minimal, reproducible example, but I will make an attempt.

In your code, by using DateTimeOffset.Now you are using the system's local time zone as a basis of comparison. Instead, you should get the time in a specific time zone before making your comparison:

TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
DateTimeOffset now = TimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow, tzi);

Then you can use now in your comparison and it will reflect the current time in the Pacific time zone, both when DST is in effect and when it is not.

Note that I inferred a few things from your question, comments, and symptoms you described:

  • Your item.LunchTime is a DateTime type. If it's actually a DateTimeOffset type, then the comparison would be made correctly regardless of time zone. In other words, DateTime comparison is based on the date and time as presented while DateTimeOffset comparison is based on the equivalent UTC timestamp.

  • Your server is a Windows server. I believe GoDaddy only uses Plex for it's Windows hosting (from what I could gather from searching their documentation).

  • Because you said everything worked until last fall, I assume you meant it worked until DST ended. In other words, while Pacific Time was UTC-7 the value you got from DateTimeOffset.Now was what you expected, but when Pacific Time returned to UTC-8, you still got values with a UTC-7 offset as before.

If that last point is correct, then likely your server is set for Arizona rather than for Pacific Time. Since GoDaddy is an Arizona-based company, this is plausible.

It may or may not be possible to change the system's time zone depending on how much of the hosting environment GoDaddy has exposed. I can see from Plesk's docs that they do indeed give a Date and Time settings panel - but I'm not sure whether GoDaddy exposes it to you or not.

Again, my recommendation would be to ignore the system-local time zone and not try to change it. Instead, alter your code to use a specific time zone as shown. Put it in a helper function somewhere in your code if that makes it easier to use throughout your applicaiton.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...