如何在不同的编程语言库处理日期和放大器;时间,时间戳和放大器;持续时间,Leapseconds&安培; -years,DSTS&安培;时区, ...? [英] How do libraries in different programming languages handle Date & Time, Timestamps & Durations, Leapseconds & -years, DSTs & Timezones, ...?

查看:197
本文介绍了如何在不同的编程语言库处理日期和放大器;时间,时间戳和放大器;持续时间,Leapseconds&安培; -years,DSTS&安培;时区, ...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一个标准机构或特定规范的方式与时间相关的事情应该如何的在实践中落实的(如ICU对Unicode相关的任务),或者是这个目前是一个尽力而为这取决于有多少精力,时间和金钱的语言和库的实现要花费?

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?

推荐答案

我会尽力给出一个答案使用Java库,可能成为Java 7的一部分的第二和第三个问题。

这些类JodaTime完全重写试图解决的设计缺陷 util.Date / util.Time 以及JodaTime。

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试图提供日期和时间的综合模式,这是类型安全和自我记录。它与现有的类可互操作的,同时也考虑基于DBMS的XML和使用情况。
的类是最终的,不可变的,线程安全的,施工后不能修改。实例是通过一系列丰富的工厂方法可以缓存在后台创建的东西。

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);



API有一些面向机器的类和一些以人为本的类:

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

即时

Instant

有关的时间,相当于一个Unix或Java的时间戳的点。其实有即时 TAIInstant UTCInstant 这使人们确切地选择他们需要我,其中时间的定义。即天基,线性,没有闰秒等等。

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.

有一个丰富的类处理不同的使用情况,如日期,只是,时间只,时间和日期,有和没有时区,使用和不使用DST集合。

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

DateProvider

OffsetDate LOCALDATE java.sql.Date 兼容性)

TimeProvider

TimeProvider

OffsetTime 本地时间的java.sql.Time 兼容性)

DateTimeProvider

DateTimeProvider

ZonedDateTime OffsetDateTime LocalDateTime 的java.util.GregorianCalendar 兼容性)

InstantProvider

InstantProvider

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

周期

Period

期间表示的时间跨度,如五日,可以添加从日期/时间中减去。

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

匹配器

Matcher

匹配器能够像查询就是这个日期在2006年?或者是这一天,今年的最后一天。

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

解析器允许用户定义,如果一个特定的日期是无效的,如2010年2月31日会发生什么:

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



与时区和夏令时工作数据

有可能的序列化这些类,它可以使用当前的时区数据或时区数据反序列化他们时,他们被序列化。
此外,来自不同的时区规则可以进行比较:它可以找出是否DST规则已经改变,如G。版本2010年和2010F在伦敦或莫斯科,并决定,如果时间是在缝隙或重叠应该做什么日期间。

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.

日历系统

尽管一切都基于 ISO-8601 ,简单日历希伯来语,伊历,日语,ThaiBuddist等提供实时系统。

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

格式和分析

的toString()返回ISO8601,并像在的SimpleDateFormat 和更先进的支持模式。

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

整合


  • 数据库

  • JodaTime

  • 遗留的JDK类(的java.util。*

  • XML

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

参考文献:

References:

  • https://jsr-310.dev.java.net/nonav/doc-2010-06-22/index.html
  • JavaZone 2010 - Stephen Colebourne: Time to fix it! - JSR-310 Date and Time API

这篇关于如何在不同的编程语言库处理日期和放大器;时间,时间戳和放大器;持续时间,Leapseconds&安培; -years,DSTS&安培;时区, ...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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