Comments (3)
@volkert-fastned, since your problem is with system interoperability, the upcoming API for parsing and formatting may solve it: #343
from kotlinx-datetime.
One practical use case for such functionality: the OCPP specification. It mandates that ISO date/time strings don't have more than 3 decimal points:
number of decimal places SHALL NOT exceed the maximum of 3.
In other words: no more than millisecond precision.
While working with kotlinx-datetime
, we actually ran into compatibility problems with vendors that will flat out reject the nanosecond-precise ISO date/time strings that kotlinx.datetime.Instant
gets serialized to by kotlinx.serialization
.
We could use JSR-310 (java.time
) instead, so we could solve it with truncateTo
, or we can write a helper function as suggested on StackOverflow, but I was kind of hoping that there would be an equally convenient built-in multiplatform solution for this.
from kotlinx-datetime.
Update:
I found a single-line workaround for forcibly reducing the precision to millisecond-level in serialized ISO date/time strings, at least for timestamps originating from the system clock:
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
// Will have 6 digits behind the decimal point on Kotlin/JVM, but 3 digits on Kotlin/JS and Kotlin/Native
println(Clock.System.now())
// Will have 3 digits behind the decimal point on Kotlin/JVM, Kotlin/JS and Kotlin/Native
println(Instant.fromEpochMilliseconds(Clock.System.now().toEpochMilliseconds()))
So Kotlin/JVM appears to appears to be the only Kotlin platform that creates Instant
s from the system clock at nanosecond precision, although I haven't tried it with Kotlin for Android yet.
from kotlinx-datetime.
Related Issues (20)
- java.time.Instant.parse and kotlinx.datetime.Instant.parse behave differently HOT 1
- Analyze the number of trailing zeros for fractions of seconds HOT 1
- JS browser app fails to load due to missing js-joda module HOT 3
- Is there a recommendation for how to use the various type with hibernate in a Spring Boot Project? HOT 1
- Consider removing `String.toLocalDate()`, `String.toInstant()`, etc. HOT 1
- Not compliant with ISO-8601 HOT 1
- GMT HOT 1
- API reference improvements HOT 1
- Consider adding parsing and formatting support for `kotlin.time.Duration`
- Provide custom formats for `DateTimePeriod`
- Introduce serializers corresponding to custom formats HOT 3
- Fix the ISO serializers omitting seconds when zero and emitting fractional parts in groups of three signs HOT 4
- Locale-aware formatting (but not parsing)
- Publish the design documents for datetime formats
- Update Kotlin version HOT 3
- ABI incompatible change on `kotlinx.datetime/LocalDateTime.Companion.parse` function in 0.6.0 release HOT 2
- Failed requirement on strange time zone HOT 1
- Potential improvements and input preservation in parsing and formatting error messages HOT 1
- Feature: building instant from DateTimeComponents HOT 8
- Feature: optional padding HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kotlinx-datetime.