Giter Site home page Giter Site logo

nscala-time / nscala-time Goto Github PK

View Code? Open in Web Editor NEW
867.0 27.0 79.0 1.42 MB

A new Scala wrapper for Joda Time based on scala-time

Home Page: https://javadoc.io/doc/com.github.nscala-time/nscala-time_3/latest/api/com/github/nscala_time/time.html

License: Apache License 2.0

Scala 100.00%
scala joda datetime

nscala-time's Introduction

nscala-time

Maven Central scaladoc

A new Scala wrapper for Joda Time. This project forked from scala-time since it seems that scala-time is no longer maintained.

Installation

Add the following to your sbt build:

libraryDependencies += "com.github.nscala-time" %% "nscala-time" % "2.32.0"

if you want to use previous versions, you can find it from here

Release Note

2.18.0

2.16.0

2.14.0

2.12.0

2.10.0

  • adding the flexibility to include format for local date and date time. See scaladoc

2.8.0

2.6.0

2.4.0

2.2.0

2.0.0

1.8.0

1.6.0

1.4.0

  • joda-time 2.4 support

1.2.0

1.0.0

  • Binaries compiled with Scala 2.11.0 are included.
  • #53

0.8.0

Binaries compiled with Scala 2.11.0-M8 are included.

0.6.0

Two major changes has been introduced in 0.6.0:

Although I believe that these changes don't break your code, if you have some problems, don't mind reporting the problem to issues.

Version Numbering Policy

x.y.z, each letter is digit, format is used as version number. Meanings of x is not determined yet.
y means major-version. Note that y is even number in any version of released nscala-time. If y is odd, it's development and/or SNAPSHOT version. z is mainly used for bug fix releases. Currently, the value of z is 0 in most cases.

Usage

This is mostly a convenience wrapper around the Joda Time libraries, adding more pleasant syntax like operators for addition, subtraction, and comparison. Also, most fields usually available as getField are now simply available as field, following the Scala convention. Some instances of asX or toX have also been shortened.

Import

import com.github.nscala_time.time.Imports._

Date/Time Operations

DateTime.now() + 2.months // returns org.joda.time.DateTime = 2009-06-27T13:25:59.195-07:00

DateTime.nextMonth < DateTime.now() + 2.months // returns Boolean = true

DateTime.now() to DateTime.tomorrow  // return org.joda.time.Interval = > 2009-04-27T13:47:14.840/2009-04-28T13:47:14.840

(DateTime.now() to DateTime.nextSecond).millis // returns Long = 1000

2.hours + 45.minutes + 10.seconds
// returns com.github.nscala_time.time.DurationBuilder
// (can be used as a Duration or as a Period)

(2.hours + 45.minutes + 10.seconds).millis
// returns Long = 9910000

2.months + 3.days
// returns Period

Fluent interface

DateTime.now() // returns org.joda.time.DateTime = 2009-04-27T13:25:42.659-07:00

DateTime.now().hour(2).minute(45).second(10) // returns org.joda.time.DateTime = 2009-04-27T02:45:10.313-07:00

Please see Joda Time for full explanation of key concepts and API: https://www.joda.org/joda-time/

Documents

Documentation of joda-time will be also a help.

Motivation

The Java Date and Calendar libraries are largely inadequate. They are mutable, not thread-safe, and very inconvenient to use.

The Joda Time library is a great replacement for Java's Date and Calendar classes. They're immutable by default, have a much richer and nicer API, and can easily be converted to Java's Date and Calendar classes when necessary.

This project provides a thin layer of convenience around the Joda Time libraries, making them more idiomatic to use within Scala.

nscala-time's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nscala-time's Issues

Cannot add period to DateTime

I get an error when trying to add a period to a DateTime using nscala-time 0.4.2 with Scala 2.10.2:

scala> import com.github.nscala_time.time.Imports._
scala> DateTime.now + 2.months
<console>:17: error: type mismatch;
 found   : org.joda.time.DateTime
 required: ?{def +(x$1: ? >: org.joda.time.Period): ?}
Note that implicit conversions are not applicable because they are ambiguous:
 both method dt2rd in package time of type (dt: org.joda.time.DateTime)org.saddle.time.RichDT
 and method RichDateTime in trait JodaImplicits of type (dt: org.joda.time.DateTime)com.github.nscala_time.time.RichDateTime
 are possible conversion functions from org.joda.time.DateTime to ?{def +(x$1: ? >: org.joda.time.Period): ?}
              DateTime.now + 2.months
                       ^
<console>:17: error: value + is not a member of org.joda.time.DateTime
              DateTime.now + 2.months

Release with the latest joda

Hello,

I'd like to know when will there be a new release of the lib? I can see that the latest joda has been added in March but no release has been made since December last year.

I use scalikejdbc and there the dependencies are for 2.9.9.

Can you please make a new release and possibly add that to maven central?

Thank you!

scalaJS support

At the moment ScalaJS allows to use either javascript Date class or java.util.Date classes. That is why for cross scala-scalajs libraries one has to use only terrible default java.util.Date class :-(

Would you publish nscala-time_2.10 insted of nscala-time_2.10.0?

Now, nscala-time_2.10.0-0.2.0 is published.

So, who use nscala-time with sbt0.12.x can not resolve dependency without cross CrossVersion.full.
Insted, sbt0.11.x users can resolve it as README.md

That default usage target sbt0.12.x users basically, regards me as good.
So it should like publish nscala-time for 2.10.0 as binary version 2.10 and follow sbt0.11.x users up by documents.

Typelevel

You guys can be part of type level also?

Should nscala-time be transfered to scalajp ?

  • Now scalajp organization is not utilized enough
  • I think that it maybe better that nscala-time is transfereed to scalajp for better collaboration
    • We can import com.github.scalajp.nscala.time.Imports._ instead of com.github.kmizu.nscala.time.Imports._
  • I want opinions
  • Of course, I maintain nscala-time responsibly whether or not

First and Last Day of Week

Is it possible to get the first and last day of the week using this library?

Ex.
Input: 4/30/15 (Thursday)
Output: 4/26/15 (Sunday) and 5/2/15 (Saturday).

Latest Version publish

Is there any way we could publish the current version, since the latest change could be useful for many...

support for min / max operations

support code like this :
var date1= ...
var date2= ...

date1 max date2'
or maybe
max(date1,date2)

same thing for min operation

millis in RichDateTime

Why there is no the millis equivalent to second in RichDateTime ? That could be nice ^^

Get weird result when I want to find period between two dates and subtract some date by it

I have a problem, when I want to find the period between two dates, and subtract some other date by this period, I will get different answer when I choose different date. For example:


val t1 = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").parseDateTime("2017-6-13 12:00:00")
val t2 = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").parseDateTime("2017-07-30 12:00:00")
val t3 = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").parseDateTime("2017-07-31 12:00:00")

t2 - (t1 to t2).toPeriod // 2017-06-13T12:00:00.000+08:00  It's OK, just give me the time of t1
t3 - (t1 to t3).toPeriod // 2017-06-12T12:00:00.000+08:00  It's weird !?? Why I miss one day !??
 

Imports should be separated into several object as usage

If one big object has too many implicit converions, it sometimes confuses users (including me) and it seems that some of their implicit conversions don't work well.

For example, I experienced that some implicit conversions works well in REPL but doesn't work well in a test (Perhaps, it maybe my error).

See details: #29 (comment)

conflict of implicits with scala.concurrent.duration

import scala.concurrent.duration._
import com.github.nscala_time.time.Imports._

val timeoutx = 5.seconds

leads to
[error] Note that implicit conversions are not applicable because they are ambiguous:
[error] both method DurationInt in package duration of type (n: Int)scala.concurrent.duration.package.DurationInt
[error] and method RichInt in trait IntImplicits of type (n: Int)com.github.nscala_time.time.RichInt
[error] are possible conversion functions from Int(5) to ?{def seconds: ?}
[error] val timeoutx = 5.seconds

Forked or not forked?

It seems sad that this was a fork from scala-time but did not actually fork it (and then rename/move files).

It seems that scala-time is active and it would be good to see the differences between the projects in a network graph.

It would be really cool if:

  1. the original copied scala-time revision was pulled into this repository
  2. renamed (as in the second commiit)
  3. merge the second commit into the new branch, and then merge the current head into the forked branch head.

It'd just be nice to see the differences easily - perhaps nscala-time could commit back - scala-time seems to have a larger community (certainly a larger committer/fork base)

DateTime.parse()

Can't seem to access the DateTime.parse() static method from this lib. It's present in Joda Time as a static method.

Compatibility with scala duration

Hello,

I don't know if it's possible but it's can be greate to have an implicit converter from org.joda.time.Duration to scala.concurrent.duration.Duration

add dayOfYear method

Please add this method to class RichDateTime :

def dayOfYear: DateTime.Property = underlying.dayOfYear

Edit : add also

def withDayOfYear(doy: Int) = underlying.withDayOfYear(doy)

Add method to convert an interval into a collection

I think it would be useful to have a method to convert an interval to immutable collection of dates, like SortedSet, with specified step. The main goal is to be able to utilize Scala collections API out of the box

(start to end).toSeq(step).map(date => ???)

RichLong is missing timezone-aware helpers

Existing toDateTime in RichLong always uses default time zone which is not always what is desired. For using custom time zone it's necessary to do double call like 123.toDateTime.toDateTime(DateTimeZone.UTC) which doesn't look nice.

So my request is to please add more toDateTime helper which also accept DateTimeZone.

Automatic conversion to scala.concurrent.duration

Hi!

I'm using nscala-time with RxScala, so I had to write a simple implicit conversion:

import com.github.nscala_time.time.Imports.{ Duration => NscalaDuration }
import scala.concurrent.duration._

object NscalaDurationImplicits {
   implicit def Dur2Dur(d: NscalaDuration) : Duration = d.getStandardSeconds() seconds 
}

I think it's possible to include this conversion into nscala-time

Scala wrapper for threeten

How about creation of scala wrapper for new java time api (threeten), which will be added in java 8 ?

Conversion from Interval to FiniteDuration

What's the best way is to convert from an Interval to a FiniteDuration (used in the standard library)? Related to that, could there be an implicit conversion for this?

UTC time

Documentation doesn't specify how to obtain UTC time

Drop Scala 2.9.3 support in newer version

  • Since the number of nscala-time_2.9.3 downloads is 0.XX% of the number of (nscala-time_2.10 and nscala-time_2.11) downloads, if Scala 2.9.3 support is dropped over the past year, it's considered to be lightly affected if the support is dropped.
  • To build against Scala 2.9.3, Scala 2.10.X, Scala 2.11.X, and Scala 2.12.X, both Java 7 and Java 8 is required. However, Java 7 is already EOL. Then, it's not required to support build against Scala 2.9.3, which requires Java (x < 8).

24.hour.millis get 86400000 but 1.day.millis get 0

code

import com.github.nscala_time.time.Imports._
24.hour
24.hour.millis

1.day
1.day.millis

results:

import com.github.nscala_time.time.Imports._
res0: com.github.nscala_time.time.DurationBuilder = com.github.nscala_time.time.DurationBuilder@fef68c09
res1: Long = 86400000

res2: org.joda.time.Period = P1D
res3: Int = 0

Rich*** classes should be isolated

  • Currently, Rich*** classes (for pimp my library pattern) are included in com.github.nscala_time.time package
  • These classes should be isolated from the packgage for better readability
    • e.g. com.github.nscala_time.time.ext

Java 8 date and time API support

It could be nice to support Java 8 date and time API which is pretty much a copy of Joda Time.

This also means no external dependencies.

Common date operator overloads

Hi!

I understand this can't exist in Java, but in Scala I found I can define things like:

implicit class DateTimeUtils(val date: DateTime) extends AnyVal
{
def <= (other: DateTime) = date.compareTo(other) <= 0
}

To have common things like <= available for DateTime objects.
That said, I'm very new to Scala and I'm not sure what you guys are planning, so I wanted to ask about this feature.

Make DurationBuilder public

DurationBuilder is a leaky abstraction. Many methods will return a DurationBuilder, yet it is private. This makes adding any sugar on Duration and Period difficult (impossible?).

For example:

  private val adapt = ((_:Any) match {
    case interval: Interval => interval.toDuration
    case period: Period => period.toStandardDuration
    case other => other
  })
  def haveTheSameDurationAs(expected: Duration) = be_==(expected) ^^ adapt
  def haveTheSameDurationAs(expected: Period) = be_==(expected.toStandardDuration) ^^ adapt

  abutting.period must haveTheSameDurationAs(15.hours + 30.minutes) // compile error

Because 15.hours + 30.minutes is a DuratonBuilder. What I need to define is:

  def haveTheSameDurationAs(expected: DurationBuilder) = be_==(expected: Duration) ^^ adapt

But I cannot because DurationBuilder is private.

Another example of leaking:

  val x = 10.hours + 5.hours
  // What type is x? If we let IDEA tell us, we get
  val x: DurationBuilder = 10.hours + 5.hours
  // Which of course will not compile

Possible improvement: DateTime + FiniteDuration syntax

Hi!

Recently I've seen something missing from this library, the ability to add a duration to a date time. I've written an extension class for this myself, but maybe something similar could be added to nscala-time? If you think that's cool, I could probably work in a PR to add this feature.

import scala.concurrent.duration.FiniteDuration

import org.joda.time.DateTime

object DateTimeSyntax {
  implicit class RichDateTime(dateTime: DateTime) {
    def minus(duration: FiniteDuration): DateTime = dateTime.minusMillis(duration.toMillis.toInt)
    def -(duration: FiniteDuration): DateTime     = minus(duration)
    def plus(duration: FiniteDuration): DateTime  = dateTime.plusMillis(duration.toMillis.toInt)
    def +(duration: FiniteDuration): DateTime     = plus(duration)
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.