Code Monkey home page Code Monkey logo

Comments (12)

dkhalanskyjb avatar dkhalanskyjb commented on June 17, 2024 1

plus(1.days) is incorrect, it ignores DST transitions. Please use plus(1, DateTimeUnit.DAY, zone) instead.

A more robust approach that takes DST transitions near midnight into account:

val tomorrowStart = currentInstant.toLocalDate(zone).date.plus(1, DateTimeUnit.DAY).atStartOfDayIn(zone) - 1.seconds
val tomorrowEnd = currentInstant.toLocalDate(zone).date.plus(2, DateTimeUnit.DAY).atStartOfDayIn(zone) - 1.minutes

from kotlinx-datetime.

wakaztahir avatar wakaztahir commented on June 17, 2024

Instant.atDate would also be good

from kotlinx-datetime.

dkhalanskyjb avatar dkhalanskyjb commented on June 17, 2024

Not in this form, but if you explain your use case, we could potentially add something other than this to help you.

from kotlinx-datetime.

wakaztahir avatar wakaztahir commented on June 17, 2024

I use the copy extension multiple times, I just thought it would be good to have it

Also atTime and atDate are helpful if you are looking for a quick way to modify Instant with date and time

I just think they'd be helpful

from kotlinx-datetime.

dkhalanskyjb avatar dkhalanskyjb commented on June 17, 2024

Examples of use cases:

  • Finding the last time it was Monday to understand if two days are on the same week,
  • Setting the seconds and nanoseconds to zero for printing,
  • Finding the same date/time in a different year.

Essentially, the question is, why do you think they'd be helpful?

from kotlinx-datetime.

wakaztahir avatar wakaztahir commented on June 17, 2024

Everytime I need to only change the day in a LocalDateTime object I must create it completely, meaning I will call the constructor with all parameters specified, This is a lot verbose, just for changing the day

val time : LocalDateTime = ~
val newTime = time.copy(dayOfMonth = 8)

VS

val time : LocalDateTime = ~
val newTime = LocalDateTime(
    year = year,
    monthNumber = monthNumber,
    dayOfMonth = 8,
    hour = hour,
    minute = minute,
    second = second,
    nanosecond = nanosecond
)

from kotlinx-datetime.

dkhalanskyjb avatar dkhalanskyjb commented on June 17, 2024

Why do you need to change the day?

from kotlinx-datetime.

wakaztahir avatar wakaztahir commented on June 17, 2024

because user said he wants, but the question is why does he want to do it ? 😂

from kotlinx-datetime.

dkhalanskyjb avatar dkhalanskyjb commented on June 17, 2024

So, your user asked you: "I like everything, but please, replace these dates with those where the day of the month is 8, 16, and 23 because I simply like these numbers. You don't need to know anything else." Is this what happened?

from kotlinx-datetime.

wakaztahir avatar wakaztahir commented on June 17, 2024

Okay, I was being sarcastic, User asked me to add a reminder, After two days, I added two days to current instant (this is the current date when user created it), User can also select weeks, months, having these extensions made it easier, thought I'd just post it here

from kotlinx-datetime.

dkhalanskyjb avatar dkhalanskyjb commented on June 17, 2024

To add two days, you can do date.plus(2, DateTimeUnit.DAY), there's no need to implement addition manually.

As for "selecting weeks," I still don't understand what you mean.

from kotlinx-datetime.

wakaztahir avatar wakaztahir commented on June 17, 2024

Do you think I could replace this code with something from the library ?

val tomorrowStart = currentInstant.atTime(23, 59, 59, 0)
val tomorrowEnd = currentInstant.plus(1.days).atTime(23, 59, 0, 0)

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.