如何将UTC偏移量添加到当前日期

时间:2019-05-09 22:04:07

标签: c#

目前,我需要确定当前的时区是“上午9点”。我有一个关于时区及其偏移量的表,并考虑了夏令时

偏移量以+/-时间存储在我的数据库中。

例如,洛杉矶/温哥华为void merge(array_t a[], long x, array_t b[], long y, array_t c[]) { //split the a and b arrays with co-ranking int cores = omp_get_max_threads(); long *startsInA = (long *)malloc(sizeof(long) * cores); long *startsInB = (long *)malloc(sizeof(long) * cores); #pragma omp parallel for for(int i = 0; i < cores; i++){ coRank((i*(x+y)/cores),a,x,b,y,&startsInA[i],&startsInB[i]); } //do a sequential merge for each segment-pair #pragma omp parallel private(a,b) { int index = omp_get_thread_num(); long lowerBoundA = startsInA[index]; long lowerBoundB = startsInB[index]; int upperBoundA; int upperBoundB; if(index < cores - 1){ upperBoundA = startsInA[index+1]; upperBoundB = startsInB[index+1]; } else { upperBoundA = x; upperBoundB = y; } seq_merge1((a+lowerBoundA),upperBoundA-lowerBoundA, (b+lowerBoundB),upperBoundB-lowerBoundB,c+lowerBoundA+lowerBoundB); } free(startsInA); free(startsInB); }

我要采取的方法是遍历所有时区,转换偏移量,以便可以将-8:00addhours转换为DateTime.UtcNow,然后检查小时是否为9。唯一的问题我看到的是它无法处理夏令时。

我想知道是否可以使用任何现有的库来实现我的方案,或者对我建议的库有更好的方法?最后,我需要ID列。

时区表

enter image description here

1 个答案:

答案 0 :(得分:1)

使用内置的.NET类TimeZoneInfoDateTime相对容易做到这一点,但是您可能会对结果感到惊讶。

from z in TimeZoneInfo.GetSystemTimeZones()
let timeThere = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, z)
where timeThere.Hour == 9
select new{z.DisplayName, timeThere}

例如,现在我的机器可以识别7个时区,该时间位于上午9点。

enter image description here

相关问题