不同编程语言的图书馆如何处理Date&时间,时间和时间持续时间,Leapseconds& - 年,DSTs&时区, ...? [英] How do libraries in different programming languages handle Date & Time, Timestamps & Durations, Leapseconds & -years, DSTs & Timezones, ...?

查看:187
本文介绍了不同编程语言的图书馆如何处理Date&时间,时间和时间持续时间,Leapseconds& - 年,DSTs&时区, ...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一个标准的机构或具体的规范方式,如何在实践中实现时间相关的事情(如与ICU相关的Unicode相关任务),或者这是目前的尽力而为取决于多少努力,时间和金钱的语言和图书馆实施者想要花费的时间?



是否有一个具体和完整的实现,可以作为一个例子,如何时间相关应该处理事情?



你认为哪个现有的图书馆是坏的,体面的或者很好的例子?

解决方案

我将尝试使用可能成为Java 7的一部分的Java库来回答第二和第三个问题。



javax.time。*(JSR 310)



这些类是完全重写JodaTime,试图修复设计缺陷 util.Date / util.Time 以及JodaTime。



JSR 310试图为日期提供一个综合模型和时间,这是类型安全和自我记录。它与现有的类可以互操作,但也考虑基于XML和DBMS的用例。
类是最终的,不可变的,线程安全的,在构建后不能被修改。实例通过丰富的Factory方法创建,可以在后台缓存东西。

  LocalDate dateToday = LocalDate.of(2010 ,9,14); 
LocalDate oneMonthLater = dateToday.with(OCTOBER);
LocalDate oneYearLater = dateToday.withYear(2011);

API有一些机器导向类和一些以人为本的类: p>

面向机器



即时 strong>



对于与Unix或Java时间戳相当的时间点。实际上有 Instant TAIInstant UTCInstant 准确地选择他们需要的时间的定义。 e。 天基,线性,无闰秒等。



持续时间



不一定与特定日期或日历相关联的时间范围。



以人为本



有丰富的类处理不同的用例,例如Date-only,Time-only,Time and Date,带有和不带时区,有和没有DST。



DateProvider



OffsetDate LocalDate (, java.sql.Date 兼容性) / p>

TimeProvider



OffsetTime LocalTime (, java.sql.Time 兼容性)



DateTimeProvider



ZonedDateTime OffsetDateTime LocalDateTime (, java.util.GregorianCalendar 兼容性)



InstantProvider



即时 ZonedDateTime OffsetDateTime (, java.util.Date 兼容性)



期间



期间代表一个时间跨度,如5天,可以从日期/时间加减。



Matcher



匹配者启用像2006年这个日期这样的查询?或今天是今年的最后一天。



调整者 / p>

如果您想要进行更复杂的更改,如给我最后一个月的最后一天,调整员来救援。或圣诞节后的第二个星期二,请!。



解析器 p>

解析器允许用户定义如果某个日期无效,应该发生什么,例如2010年2月31日:

  DateResolver previous = DateResolvers.previousValid(); 
LocalDate date = date(2010,2,30,previous);
// date = 2010-02-28

使用时区和DST数据



可以使用当前时区数据或时区数据序列化序列化这些类并对其进行反序列化。
另外,可以比较来自不同时区的规则:可以确定DST规则是否已更改,e。 G。在2010年和2010年之间的版本在伦敦或莫斯科的日期,并决定如果一个时间差距或重叠应该做什么。



日历系统



虽然所有内容都是基于 ISO-8601 ,希伯来语,Hijrah,日语,泰语等等的简单日历。提供时间系统。



格式化和解析



toString()返回ISO8601和类似于 SimpleDateFormat 中的模式,



集成




  • 数据库

  • JodaTime

  • 旧版JDK类( java.util。*

  • XML









Is there a standard body or a specific normative way how time-related things should be implemented in practice (like ICU for Unicode-related tasks) or is this currently a "best-effort", depending on how much effort, time and money language and library implementers want to spend?

Is there a specific and complete implementation which could serve as a example for how time-related things should be handled?

Which existing library would you consider as a bad, a decent or a good example?

解决方案

I'll try to give an answer to the second and third question using the Java library which might become part of Java 7.

javax.time.* (JSR 310)

These classes are a complete rewrite of JodaTime trying to fix the design flaws of util.Date/util.Time as well as JodaTime.

JSR 310 tries to provide a comprehensive model for date and time, which is type-safe and self-documenting. It is interoperable with existing classes, but also considers XML- and DBMS-based use-cases. The classes are final, immutable, thread-safe and cannot be modified after construction. Instances are created via a rich set of Factory methods which can cache things in the background.

LocalDate dateToday     = LocalDate.of(2010, 9, 14);
LocalDate oneMonthLater = dateToday.with(OCTOBER);
LocalDate oneYearLater  = dateToday.withYear(2011);

The API has some "machine-oriented" classes and some "human-oriented" classes:

Machine-oriented

Instant

For a point of time comparable to an Unix or Java timestamp. Actually there are Instant, TAIInstant and UTCInstant which enable people to exactly choose which definition of time they need i. e. "day-based", "linear, without leap seconds" etc.

Duration

A time range not necessarily associated with a specific Date or Calendar.

Human-oriented

There is a rich collection of classes handling different use-cases like Date-only, Time-only, Time and Date, with and without Timezones, with and without DST.

DateProvider

OffsetDate, LocalDate (, java.sql.Date compatibility)

TimeProvider

OffsetTime, LocalTime (, java.sql.Time compatibility)

DateTimeProvider

ZonedDateTime, OffsetDateTime, LocalDateTime (, java.util.GregorianCalendar compatibility)

InstantProvider

Instant, ZonedDateTime, OffsetDateTime (, java.util.Date compatibility)

Period

Periods represent a time span like "5 days" that can be added and subtracted from a date/time.

Matcher

Matchers enable queries like "is this date in the year 2006?" or "is this day the last day of this year".

Adjuster

Adjusters come to the rescue if you have want to make more complex changes, like "Give me the last day of the month!" or "The second Tuesday after Christmas, please!".

Resolver

Resolvers allow users to define what should happen if a certain date is not valid, like February 31st 2010:

DateResolver previous = DateResolvers.previousValid();
LocalDate date = date(2010, 2, 30, previous);
// date = 2010-02-28

Working with Timezone and DST data

It is possible to serialize these classes and deserialize them using either the current timezone data or the timezone data when they were serialized. Additionally, rules from different timezones can be compared: It is possible to find out if DST rules have changed, e. g. between version 2010e and 2010f for Dates in London or Moscow and decide what should be done if a Time is in a gap or overlap.

Calendar systems

Although everything is based on ISO-8601, simple calendars for Hebrew, Hijrah, Japanese, ThaiBuddist, etc. time systems are provided.

Formatting and Parsing

toString() returns ISO8601 and patterns like those in SimpleDateFormat and more advanced are supported.

Integration

  • Databases
  • JodaTime
  • Legacy JDK classes (java.util.*)
  • XML

References:

这篇关于不同编程语言的图书馆如何处理Date&时间,时间和时间持续时间,Leapseconds& - 年,DSTs&时区, ...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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