编程语言
首页 > 编程语言> > c# – 考虑到时区的夏令时,将UTC日期时间转换为将来的时间

c# – 考虑到时区的夏令时,将UTC日期时间转换为将来的时间

作者:互联网

我们有一个代表星期的系统,代表开始和时间的UTC时间.美国/芝加哥时区的结束日期时间.星期六中午时间午夜开始,周五晚上中午时间23:59:59结束,所以他们在数据库中的UTC条目是:

Week 1 - begin: 2015-10-24 05:00:00, end 2015-10-31 04:59:59
Week 2 - begin: 2015-10-31 05:00:00, end 2015-11-07 05:59:59
Week 3 - begin: 2015-11-07 06:00:00, end 2015-11-14 05:59:59
Week 4 - begin: 2015-11-14 06:00:00, end 2015-11-21 05:59:59
Week 5 - begin: 2015-11-21 06:00:00, end 2015-11-28 05:59:59

因此,从上面几周的例子中,您可以看到从白天到标准时间的时间变化反映在10/31和10周之间. 11/7.

我需要从给定的一周返回N周.我们的系统是C#Azure worker& Web角色,并在Azure云中运行(所有计算节点均为UTC).我的逻辑是,采取开始周,并将N周的工作天数添加到一周的开始日期/时间,并要求开始日期大于原始开始日期,并且小于或等于计算的周数未来的约会.

var weeks = repository.Fetch(x => x.BeginDate <= nWeeksAheadUtc && x.BeginDate > week.BeginDate)l=;

除非在最终答案中发生夏令时更改,否则此方法有效.由于时间的变化,基于在第1周的开始日期增加21天而要求从第1周开始的接下来的3周仅产生第2周和第2周. 3被退回,因为计算的未来值是2015-11-14 05:00:00,不包括第4周.

我用以下方式使用Nodatime解决了这个问题:

LocalDateTime localDateTime = LocalDateTime.FromDateTime(week.BeginDate);
ZonedDateTime zonedDateTime = localDateTime.InZoneStrictly(DateTimeZoneProviders.Tzdb["UTC"]);
zonedDateTime = zonedDateTime.WithZone(DateTimeZoneProviders.Tzdb["America/Chicago"]);
DateTime centralDateTime = zonedDateTime.ToDateTimeUnspecified();
DateTime futureDateTime = centralDateTime.Add(TimeSpan.FromDays(weekCount*7));
localDateTime = LocalDateTime.FromDateTime(futureDateTime);
zonedDateTime = localDateTime.InZoneStrictly(DateTimeZoneProviders.Tzdb["America/Chicago"]);
DateTime nWeeksAheadUtc = zonedDateTime.ToDateTimeUtc();

var weeks = repository.Fetch(x => x.BeginDate <= nWeeksAheadUtc && x.BeginDate > week.BeginDate).OrderBy(x => x.RetailerWeekNumber).ToList();

虽然它起作用,但对于跟踪我维护此代码的开发人员来说,它似乎很麻烦且不太直观.有没有更简洁的方法通过Nodatime API或(基本C#日期/时间)我这样做?

添加请求的示例 – 我刚刚为此创建了一个UnitTest项目以及这三个类:

Week.cs

using System;

namespace NodaTimeTest
{
    public class Week
    {
        public int Id { get; set; }
        public DateTime BeginDate { get; set; }
        public DateTime EndDate { get; set; }
    }
}

WeekService.cs

using NodaTime;
using System;
using System.Collections.Generic;
using System.Linq;

namespace NodaTimeTest
{
    public class WeekService
    {
        private readonly List<Week> repository;

        public WeekService()
        {
            this.repository = this.InitWeeks();
        }

        public List<Week> GetNextWeeks(int weekId, int weekCount)
        {
            Week week = this.repository.First(x => x.Id == weekId);

            // the meat - how to do this the right way?
            LocalDateTime localDateTime = LocalDateTime.FromDateTime(week.BeginDate);
            ZonedDateTime zonedDateTime = localDateTime.InZoneStrictly(DateTimeZoneProviders.Tzdb["UTC"]);
            zonedDateTime = zonedDateTime.WithZone(DateTimeZoneProviders.Tzdb["America/Chicago"]);
            DateTime centralDateTime = zonedDateTime.ToDateTimeUnspecified();
            DateTime futureDateTime = centralDateTime.Add(TimeSpan.FromDays(weekCount * 7));
            localDateTime = LocalDateTime.FromDateTime(futureDateTime);
            zonedDateTime = localDateTime.InZoneStrictly(DateTimeZoneProviders.Tzdb["America/Chicago"]);
            DateTime nWeeksAheadUtc = zonedDateTime.ToDateTimeUtc();

            var weeks = repository.Where(x => x.BeginDate <= nWeeksAheadUtc && x.BeginDate > week.BeginDate).OrderBy(x => x.Id).ToList();

            return weeks;
        }

        private List<Week> InitWeeks()
        {
            // sets up our list of 10 example dates in UTC encompassing America/Chicago daylight savings time change on 11/1
            // this means that all weeks are 168 hours long, except week "4", which is 169 hours long.
            var weeks = new List<Week>();
            DateTime beginDate = new DateTime(2015, 10, 10, 5, 0, 0, DateTimeKind.Utc);

            for (int i = 1; i <= 10; i++)
            {
                DateTime endDate = beginDate.AddDays(7).AddSeconds(-1);

                if (endDate.Date == new DateTime(2015, 11, 7, 0, 0, 0, DateTimeKind.Utc))
                {
                    endDate = endDate.AddHours(1);
                }

                weeks.Add(new Week { Id = i, BeginDate = beginDate, EndDate = endDate });

                beginDate = endDate.AddSeconds(1);
            }

            return weeks;
        }
    }
}

WeekServiceTest:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;

namespace NodaTimeTest
{
    [TestClass]
    public class WeekServiceTest
    {
        private readonly WeekService weekService = new WeekService();

        [TestMethod]
        public void TestGetNextThreeWeeksOverDaylightTimeChange()
        {
            var result = this.weekService.GetNextWeeks(2, 3);

            Assert.AreEqual(3, result.ElementAt(0).Id);
            Assert.AreEqual(4, result.ElementAt(1).Id);
            Assert.AreEqual(5, result.ElementAt(2).Id);
        }

        [TestMethod]
        public void TestGetNextThreeWeeksWithNoDaylightTimeChange()
        {
            var result = this.weekService.GetNextWeeks(5, 3);

            Assert.AreEqual(6, result.ElementAt(0).Id);
            Assert.AreEqual(7, result.ElementAt(1).Id);
            Assert.AreEqual(8, result.ElementAt(2).Id);
        }
    }
}

解决方法:

好的,如果我理解正确的话,我想你可能想要这样的东西:

var zone = DateTimeZoneProviders.Tzdb["America/Chicago"];
var instantStart = Instant.FromDateTimeUtc(week.BeginDate);
var chicagoStart = instantStart.InZone(zone);
var localEnd = chicagoStart.LocalDateTime.PlusWeeks(weekCount);
var chicagoEnd = localEnd.InZoneLeniently(zone);
var bclEnd = chicagoEnd.ToDateTimeUtc();

var result = repository
    .Fetch(x => x.BeginDate >= week.BeginDate && x.BeginDate < bclEnd)
    .OrderBy(x => x.RetailerWeekNumber)
    .ToList();

请注意,我已将下限包含在内,而上限独占 – 这通常是最简单的处理方式.

如果你真的想要,你当然可以把很多这个链接起来:

var zone = DateTimeZoneProviders.Tzdb["America/Chicago"];
var bclEnd = Instant.FromDateTimeUtc(week.BeginDate)
    .InZone(zone)
    .LocalDateTime
    .PlusWeeks(weekCount)
    .InZoneLeniently(zone)
    .ToDateTimeUtc();

编辑:以上是您的BeginDate实际上是您想要开始获取数据的瞬间.这听起来像在现实中,你想要开始一周.那时它将是:

var zone = DateTimeZoneProviders.Tzdb["America/Chicago"];
var instantNow = Instant.FromDateTimeUtc(week.BeginDate);
var chicagoNow = instantStart.InZone(zone);
var localStart = chicagoNow.LocalDateTime.PlusWeeks(1);
var localEnd = localEnd(weekCount);
var bclStart = localStart.InZoneLeniently(zone).ToDateTimeUtc();
var bclEnd = localEnd.InZoneLeniently(zone).ToDateTimeUtc();

var result = repository
    .Fetch(x => x.BeginDate >= bclStart && x.BeginDate < bclEnd)
    .OrderBy(x => x.RetailerWeekNumber)
    .ToList();

标签:c,datetime,nodatime
来源: https://codeday.me/bug/20190623/1270028.html