转换UTC时间使用Nodatime本地时间 [英] Convert UTC time to local time using Nodatime

查看:187
本文介绍了转换UTC时间使用Nodatime本地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此格式ddMMyyHHmmss提供了时间。我知道时间是UTC格式。我想用NodaTime库将其转换为我的本地时区,但我似乎无法弄清楚。我的本地时区的目标是成为新西兰



下面是我曾尝试:

  VAR模式= LocalDateTimePattern.CreateWithInvariantCulture(ddMMyyHHmmss); 

VAR parseResult = pattern.Parse(utcDateTime);
如果(!parseResult.Success)
{
抛出新InvalidDataException(无效时指定+日期+时间);
}

变种的timeZone = DateTimeZoneProviders.Bcl [新西兰标准时间];

变种区=新ZonedDateTime(
localDateTime,
的时区,
timeZone.GetUtcOffset(SystemClock.Instance.Now));


返回新的datetime(zone.ToInstant()蜱);


解决方案

  //由于您的输入值是UTC,直接解析它作为一个即时。 
VAR模式= InstantPattern.CreateWithInvariantCulture(ddMMyyHHmmss);
变种parseResult = pattern.Parse(150713192900);
如果(!parseResult.Success)
抛出新InvalidDataException(...什么...);
无功瞬间= parseResult.Value;

的Debug.WriteLine(即时); // 2013-07-15T19:29:00Z

//你永远是与TZDB更好,但任何一项都将正常工作。
变种的timeZone = DateTimeZoneProviders.Tzdb [太平洋/奥克兰];
// VAR的timeZone = DateTimeZoneProviders.Bcl [新西兰标准时间];

//将立即到该区域的本地时间
VAR zonedDateTime = instant.InZone(的timeZone);

的Debug.WriteLine(zonedDateTime);
//地方:2013年7月16日上午7时29分00秒偏移:+12区:Pacific /奥克兰

//如果你必须有一个日期时间,得到它像这样
VAR bclDateTime = zonedDateTime.ToDateTimeUnspecified();

的Debug.WriteLine(bclDateTime.ToString(O)); // 2013-07-16T07:29:00.0000000


I have been provided a time in this format "ddMMyyHHmmss". I know the time is in UTC format. I would like to use the NodaTime library to convert this to my local timezone but I can't seem to figure it out. My local timezone target is to be New Zealand.

Here's what I have tried:

 var pattern = LocalDateTimePattern.CreateWithInvariantCulture("ddMMyyHHmmss");

 var parseResult = pattern.Parse(utcDateTime);
 if (!parseResult.Success)
 {
     throw new InvalidDataException("Invalid time specified " + date + time);
 }

 var timeZone = DateTimeZoneProviders.Bcl["New Zealand Standard Time"];

 var zone = new ZonedDateTime(
                  localDateTime, 
                  timeZone, 
                  timeZone.GetUtcOffset(SystemClock.Instance.Now));


 return new DateTime(zone.ToInstant().Ticks);

解决方案

// Since your input value is in UTC, parse it directly as an Instant.
var pattern = InstantPattern.CreateWithInvariantCulture("ddMMyyHHmmss");
var parseResult = pattern.Parse("150713192900");
if (!parseResult.Success)
    throw new InvalidDataException("...whatever...");
var instant = parseResult.Value;

Debug.WriteLine(instant);  // 2013-07-15T19:29:00Z

// You will always be better off with the tzdb, but either of these will work.
var timeZone = DateTimeZoneProviders.Tzdb["Pacific/Auckland"];
//var timeZone = DateTimeZoneProviders.Bcl["New Zealand Standard Time"];

// Convert the instant to the zone's local time
var zonedDateTime = instant.InZone(timeZone);

Debug.WriteLine(zonedDateTime);
  // Local: 7/16/2013 7:29:00 AM Offset: +12 Zone: Pacific/Auckland

// and if you must have a DateTime, get it like this
var bclDateTime = zonedDateTime.ToDateTimeUnspecified();

Debug.WriteLine(bclDateTime.ToString("o"));  // 2013-07-16T07:29:00.0000000

这篇关于转换UTC时间使用Nodatime本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆