Code Monkey home page Code Monkey logo

Comments (8)

dkhalanskyjb avatar dkhalanskyjb commented on September 27, 2024

Please provide some code that you think is cumbersome to write without DateTimeComponents but is easy with it. Maybe we'll figure out a better way.

from kotlinx-datetime.

kunyavskiy avatar kunyavskiy commented on September 27, 2024

Basically, the example I had was building an instant from a date manually parsed by regex.

I've now found, that there is a constructor of LocalDate from components, which I missed and it mostly cover the problem. So probably, probably, the main usecase I have in mind is unfing code between formatting and different kotlinx.datetime class construction from some external format, where the only thing you need is an extention function filling DateTimeComponents.

But that doesn't sound so important.

from kotlinx-datetime.

dkhalanskyjb avatar dkhalanskyjb commented on September 27, 2024

unfing code between formatting and different kotlinx.datetime class construction from some external format, where the only thing you need is an extention function filling DateTimeComponents

So, the feature request is actually for something like this?

LocalDate.fromComponents { // receiver is LocalDateComponents
  year = someYear
  month = someMonth
  dayOfMonth = someDayOfMonth
}

This would be more in line with working with DateTimeComponents, but what are the benefits compared to just using the constructor? For DateTimeComponents, not having a constructor is a necessity, because the set of fields may increase in the future and the fields are optional, which is not the case for something like LocalDate. Having the compiler ensure that you provided all the necessary fields seems good.

a date manually parsed by regex

By the way, having to use regexes to parse dates is also probably a bug/missing feature in our library, so if it's us who made you go to regexes, please file another issue request.

from kotlinx-datetime.

kunyavskiy avatar kunyavskiy commented on September 27, 2024

It's not much useful for LocalDate, because it is basically subset of components.

While

Instant.fromComponents {
   year = someYear
   month = someMonth
   dayOfMonth = someDayOfMonth
   ...
}

makes more sense, as otherwise you would need exponential number of constructor in Instant (e.g. TimeZone vs UtcOffset, month vs monthNumber, year+month+dayOfMonth vs LocalDate).

from kotlinx-datetime.

dkhalanskyjb avatar dkhalanskyjb commented on September 27, 2024
LocalDateTime(someYear, someMonth, someDayOfMonth, someHour, someMinute).toInstant(TimeZone.of("Europe/Berlin"))
LocalDate(someYear, someMonth, someDayOfMonth).atTime(time).toInstant(UtcOffset.ZERO)

etc.

from kotlinx-datetime.

dkhalanskyjb avatar dkhalanskyjb commented on September 27, 2024

If there's still some interest in this, please let us know, and we'll reopen the issue.

from kotlinx-datetime.

kunyavskiy avatar kunyavskiy commented on September 27, 2024

Well, I argree that the options you suggested above already provides ability of doing what I have described. Although I still think mine are more convenient. But as it is pretty easy to implement it yourself, avoiding API duplication is fine.

from kotlinx-datetime.

dkhalanskyjb avatar dkhalanskyjb commented on September 27, 2024

It's not the API duplication that's the problem but making DateTimeComponents anything more than a thing for parsing and formatting. See, it's an unpleasant thing in general: it's mutable, non-thread-safe, people may be tempted to do invalid datetime arithmetic using its fields, etc. We'd rather not give DateTimeComponents any more power than it already has.

from kotlinx-datetime.

Related Issues (20)

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.