通过特定方式将日期时间设置为特定日期
Setting datetime to a specific date by specific way
我有一个带有日期时间的变量,我必须根据这些规则和场景在特定日期设置它:
- 我连接的 API 有每日限制,一旦达到该限制,我必须等到第二天 9:10 AM CEST <= 这非常重要
所以我一直在这样做:
var localTime = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time"));
var tomorrowAt0910 = localTime.AddDays(1).Date + new TimeSpan(9, 10, 0);
而且我已经意识到这段代码有一个错误,因为我可以有以下场景:
- 假设我的申请将于 7 月 30 日下午 15:00 到期,在这种情况下,上面的逻辑将有效
但是
我们有下一个更可能发生的情况:
- 申请将于 7 月 31 日 5:00 上午到期,在这种情况下,此逻辑是错误的,因为更新日期将设置为 8 月 1 日 9:10上午,这很糟糕
如果在第二种情况下应用程序过期,我应该将日期设置为同一天和几个小时的差异(从早上 5 点到早上 9 点)
我该怎么做?
听起来你真正想说的是:
- 查找中欧的当前时间
- 在同一天找到 9:10am
- 如果9:10am在当前时间之后,添加一天
所以像这样:
// No need to do this more than once
private static readonly TimeZoneInfo centralEuropeZone =
TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time")
private static DateTime GetUtcResetTime()
{
// Using UtcNow to make it clear that the system time zone is irrelevant
var centralEuropeNow = TimeZoneInfo.ConvertTime(DateTime.UtcNow, centralEuropeZone);
var centralEuropeResetTime = centralEuropeNow.Date + new TimeSpan(9, 10, 0);
if (centralEuropeResetTime <= centralEuropeNow)
{
centralEuropeResetTime = centralEuropeResetTime.AddDays(1);
}
return TimeZoneInfo.ConvertTimeToUtc(centralEuropeResetTime, centralEuropeZone);
}
我已经将 return 设置为 UTC DateTime
这样其他代码就不需要担心它在哪个区域了。
检查过期日期是否小于当前日期,如果是则加一天。
DateTime expireDate = new DateTime(2018, 7, 30, 22, 0, 0); //for testing
DateTime tomorrowAt0910 = DateTime.Now.Date.AddHours(9).AddMinutes(10);
if (expireDate.Date < DateTime.Now.Date)
{
tomorrowAt0910.AddDays(1);
}
我有一个带有日期时间的变量,我必须根据这些规则和场景在特定日期设置它:
- 我连接的 API 有每日限制,一旦达到该限制,我必须等到第二天 9:10 AM CEST <= 这非常重要
所以我一直在这样做:
var localTime = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time"));
var tomorrowAt0910 = localTime.AddDays(1).Date + new TimeSpan(9, 10, 0);
而且我已经意识到这段代码有一个错误,因为我可以有以下场景:
- 假设我的申请将于 7 月 30 日下午 15:00 到期,在这种情况下,上面的逻辑将有效
但是
我们有下一个更可能发生的情况:
- 申请将于 7 月 31 日 5:00 上午到期,在这种情况下,此逻辑是错误的,因为更新日期将设置为 8 月 1 日 9:10上午,这很糟糕
如果在第二种情况下应用程序过期,我应该将日期设置为同一天和几个小时的差异(从早上 5 点到早上 9 点)
我该怎么做?
听起来你真正想说的是:
- 查找中欧的当前时间
- 在同一天找到 9:10am
- 如果9:10am在当前时间之后,添加一天
所以像这样:
// No need to do this more than once
private static readonly TimeZoneInfo centralEuropeZone =
TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time")
private static DateTime GetUtcResetTime()
{
// Using UtcNow to make it clear that the system time zone is irrelevant
var centralEuropeNow = TimeZoneInfo.ConvertTime(DateTime.UtcNow, centralEuropeZone);
var centralEuropeResetTime = centralEuropeNow.Date + new TimeSpan(9, 10, 0);
if (centralEuropeResetTime <= centralEuropeNow)
{
centralEuropeResetTime = centralEuropeResetTime.AddDays(1);
}
return TimeZoneInfo.ConvertTimeToUtc(centralEuropeResetTime, centralEuropeZone);
}
我已经将 return 设置为 UTC DateTime
这样其他代码就不需要担心它在哪个区域了。
检查过期日期是否小于当前日期,如果是则加一天。
DateTime expireDate = new DateTime(2018, 7, 30, 22, 0, 0); //for testing
DateTime tomorrowAt0910 = DateTime.Now.Date.AddHours(9).AddMinutes(10);
if (expireDate.Date < DateTime.Now.Date)
{
tomorrowAt0910.AddDays(1);
}