野田时代入门 [英] Getting Started with Noda Time

查看:135
本文介绍了野田时代入门的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找使用Noda时间的一个相当简单的应用程序,但是我很难找到任何文档来处理一个非常基本的用例:

I am looking to use Noda time for a fairly simple application, however I am struggling to find any documentation to handle a very basic use case:

我有一个登录用户,并将在设置中存储他们首选的时区。来自客户端的任何日期/时间使用已知的时区ID(例如欧洲/伦敦)以已知的文本格式(例如dd / MM / yyyy HH:mm)。我正计划将这些时间转换为UTC / Noda Instants,以防止需要在数据库中存储每个日期的时区信息。

I have a logged in user, and will be storing their preferred timezone in the settings. Any date/times that come from the client come in a known text format (e.g. "dd/MM/yyyy HH:mm"), with a known time zone id (e.g. "Europe/London"). I was planning on converting these times to UTC/Noda Instants to prevent the need to store the time zone info with each date in the database.

首先,这听起来像一个明智的做法我可以自由地改变任何事情,所以很乐意成为一个更好/更明智的课程。数据库是MongoDb,使用C#驱动程序。

Firstly, does this sound like a sensible approach? I am free to change pretty much anything, so would be happy to be set on a better/more sensible course. The database is MongoDb, using the C# driver.

我尝试过的是这些行,但是努力克服第一步!

What I have tried is along these lines, but struggling to get over the first step!

var userSubmittedDateTimeString = "2013/05/09 10:45";
var userFormat = "yyyy/MM/dd HH:mm";
var userTimeZone = "Europe/London";

//noda code here to convert to UTC


//Then back again:

我知道有人会问你尝试了什么,我所拥有的是各种失败的转换。很高兴指出开始使用野田时间页面!

I know someone will ask "what have you tried", to which all i have is various failed conversions. Happy to be pointed to an "Getting started with Noda time" page!

推荐答案


我正在计划将这些时间转换为UTC / Noda Instants,以防止存储所有时区信息与数据库中的每个日期。

I was planning on converting these times to UTC/Noda Instants to prevent the need to store all the time zone info with each date in the database.

如果您不需要以后知道原来的时区。 (例如,如果用户更改时区,但仍希望在原始时区重复出现某些事情)。

That's fine if you don't need to know the original time zone later on. (e.g. if the user changes time zone, but still wants something recurring in the original time zone).

无论如何,我将其分为三个步骤:

Anyway, I would separate this out into three steps:


  • 解析为 LocalDateTime

  • 将其转换为一个 ZonedDateTime

  • 将其转换为即时

  • Parsing into a LocalDateTime
  • Converting that into a ZonedDateTime
  • Converting that into an Instant

如下所示:

// TODO: Are you sure it *will* be in the invariant culture? No funky date
// separators?
// Note that if all users have the same pattern, you can make this a private
// static readonly field somewhere
var pattern = LocalDateTimePattern.CreateWithInvariantCulture("yyyy/MM/dd HH:mm");

var parseResult = pattern.Parse(userSubmittedDateTimeString);
if (!parseResult.Success)
{
    // throw an exception or whatever you want to do
}

var localDateTime = parseResult.Value;

var timeZone = DateTimeZoneProviders.Tzdb[userTimeZone];

// TODO: Consider how you want to handle ambiguous or "skipped" local date/time
// values. For example, you might want InZoneStrictly, or provide your own custom
// handler to InZone.
var zonedDateTime = localDateTime.InZoneLeniently(timeZone);

var instant = zonedDateTime.ToInstant();

这篇关于野田时代入门的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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