Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement parsing and formatting for Instant as separate functions #411

Closed
wants to merge 6 commits into from

Conversation

dkhalanskyjb
Copy link
Collaborator

Implement parsing and formatting of ISO strings for instant values separately from the rest of the library.

A stage of fixing #382

This is a draft PR because we probably don't need to actually merge it into the datetime library. However, the code is already written and can be reviewed.

@dkhalanskyjb dkhalanskyjb force-pushed the instant-separate-formatting branch from 71fc711 to fd78536 Compare July 16, 2024 10:34
Comment on lines 173 to 175
total += (y + 3) / 4 - (y + 99) / 100 + (y + 399) / 400
} else {
total -= y / -4 - y / -100 + y / -400
Copy link
Member

@ilya-g ilya-g Oct 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a case for ceilDiv in stdlib :)

@dkhalanskyjb dkhalanskyjb force-pushed the instant-separate-formatting branch from fd78536 to 019c71b Compare November 5, 2024 13:18
@dkhalanskyjb dkhalanskyjb changed the base branch from master to uniform-ranges November 5, 2024 13:18
@dkhalanskyjb dkhalanskyjb requested a review from ilya-g November 5, 2024 13:19
@dkhalanskyjb dkhalanskyjb changed the title Prepare Instant for entering the standard library Implement parsing and formatting for Instant as separate functions Nov 7, 2024
Comment on lines +401 to +409
private fun parseInstant(isoString: String): Instant {
// return Instant.parse(isoString)
return parseIso(isoString)
}

private fun displayInstant(instant: Instant): String {
// return instant.toString()
return formatIso(instant)
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qurbonzoda, here's the logic behind this PR: the newly-added formatIso can replace toString, and the newly-added parseIso can replace Instant.parse in the case when no specific format is passed (which is the only case supported by `kotlin.time.Instant).

@dkhalanskyjb dkhalanskyjb force-pushed the instant-separate-formatting branch from 023b7ac to ece4e11 Compare November 28, 2024 11:45
expect("'T' or 't'", i + 6) { it == 'T' || it == 't' }
expect("':'", i + 9) { it == ':' }
expect("':'", i + 12) { it == ':' }
for (j in listOf(1, 2, 4, 5, 7, 8, 10, 11, 13, 14)) {
Copy link
Member

@ilya-g ilya-g Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to extract such index lists to variables outside the function and (optionally) make them arrays.

override fun toString(): String = "UnboundedLocalDateTime($year-$month-$day $hour:$minute:$second.$nanosecond)"

companion object {
fun fromInstant(instant: Instant, offsetSeconds: Int): UnboundedLocalDateTime {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is only one call to this function, and it passes 0 for offsetSeconds.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Do you see any problems with that?

fun parseFailure(error: String): Nothing {
throw IllegalArgumentException("$error when parsing an Instant from $isoString")
}
inline fun expect(what: String, where: Int, predicate: (Char) -> Boolean) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Local inline functions are not yet supported.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I build the project or open it in the IDE, I receive no such error/warning.

@dkhalanskyjb dkhalanskyjb force-pushed the instant-separate-formatting branch from 4c4e638 to 70469f0 Compare January 22, 2025 12:28
@dkhalanskyjb
Copy link
Collaborator Author

This code is in some form already in Kotlin's master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants