5 | {% if config.copyright %}
6 |
7 | {{ config.copyright }}
8 |
9 | {% endif %}
10 | {% if not config.extra.generator == false %}
11 | Made with
12 |
13 | Material for MkDocs
14 |
15 | | Icons made by
16 |
Freepik
17 | from
18 |
www.flaticon.com
19 | {% endif %}
20 |
21 |
--------------------------------------------------------------------------------
/docs/basics/formatting.md:
--------------------------------------------------------------------------------
1 | # Formatting
2 |
3 | Currently, Island Time lacks the ability to do localized and custom formatting of dates and times in common code. This is in the works and should be available pretty soon. Right now though, it is still possible to access localized text in common code and platform-specific APIs can be used to handle formatting.
4 |
5 | ## Accessing Localized Text
6 |
7 | You can obtain the localized name of a month, day of the week, or time zone in common code like so:
8 |
9 | ```kotlin
10 | // Get the system default locale. We'll assume this is "en_US".
11 | val locale = defaultLocale()
12 |
13 | val shortMonth = FEBRUARY.localizedName(TextStyle.SHORT_STANDALONE, locale)
14 | // Output: "Feb"
15 |
16 | val fullDayOfWeek = TUESDAY.localizedName(TextStyle.FULL_STANDALONE, locale)
17 | // Output: "Tuesday"
18 |
19 | val tz = TimeZone("America/New_York")
20 | val tzName = tz.displayName(TimeZoneTextStyle.DAYLIGHT, locale)
21 | // Output: "Eastern Daylight Time"
22 | ```
23 |
24 | In general, you'll find a `localizedName()` method that returns `null` if text is unavailable for the provided style and locale. And then a `displayName()` method that will instead return a default value if localized text is unavailable, such as the month or day of week number.
25 |
26 | ## `Locale`
27 |
28 | Island Time's `Locale` is simply a `typealias` for `java.util.Locale` or `NSLocale`. The `defaultLocale()` function allows you to access the user's current locale in common code. A specific locale can be used by converting a [language tag](https://tools.ietf.org/html/bcp47), such as "en-US", using the `String.toLocale()` method. For anything more sophisticated, you should use platform-specific code.
29 |
30 | ## Using Platform APIs
31 |
32 | While you can't share all of your formatting-related code when using platform APIs, there are reasons why you may not necessarily want to do that anyway.
33 |
34 | - Even though Island Time only supports the ISO calendar system, using platform APIs, you can still output to the user's preferred calendar
35 |
36 | - You can better guarantee that formatting will be consistent with the user's expectations for the platform
37 |
38 | - You can take advantage of localization features that may not be available or implemented consistently on all platforms
39 |
40 | ### Java/Android
41 |
42 | ```kotlin
43 | val zone = TimeZone("America/New_York")
44 | val instant = Instant.UNIX_EPOCH
45 | val islandZonedDateTime = instant at zone
46 |
47 | // Create a java.time DateTimeFormatter to do the formatting
48 | val formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL)
49 |
50 | println(islandZonedDateTime.toJavaZonedDateTime().format(formatter))
51 | // Output: "Wednesday, December 31, 1969 at 7:00:00 PM Eastern Standard Time"
52 | ```
53 |
54 | ### Apple
55 |
56 | ```kotlin
57 | val zone = TimeZone("America/New_York")
58 | val instant = Instant.UNIX_EPOCH
59 |
60 | // Convert to an NSDate
61 | val nsDate = instant.toNSDate()
62 |
63 | // Create an NSDateFormatter to do the formatting
64 | val formatter = NSDateFormatter().apply {
65 | dateStyle = NSDateFormatterFullStyle
66 | timeStyle = NSDateFormatterFullStyle
67 | timeZone = zone.toNSTimeZone()
68 | }
69 |
70 | println(formatter.stringFromDate(nsDate))
71 | // Output: "Wednesday, December 31, 1969 at 7:00:00 PM Eastern Standard Time"
72 | ```
73 |
--------------------------------------------------------------------------------
/docs/basics/overview.md:
--------------------------------------------------------------------------------
1 | # Overview
2 |
3 | Being heavily inspired by the java.time library, Island Time should look fairly familiar to those acquainted with it and tends to follow many of the same design principles.
4 |
5 | ## General Design
6 |
7 | ### Immutability
8 |
9 | All date-time primitives are immutable and thread-safe. Operations that manipulate a date, time, duration, or interval will always return a new object.
10 |
11 | ### Precision
12 |
13 | Island Time uses integer rather than floating-point values, offering a fixed nanosecond precision across the entire supported time scale. This avoids any surprises that might emerge from the use of floating-point arithmetic and the reduction in precision that occurs when representing larger durations.
14 |
15 | ### Overflow Handling
16 |
17 | When working with dates and times, overflow is almost never a behavior that you want. See [Y2k](https://en.wikipedia.org/wiki/Year_2000_problem) or [Time formatting and storage bugs](https://en.wikipedia.org/wiki/Time_formatting_and_storage_bugs). Island Time uses checked arithmetic throughout to detect overflow and throw exceptions rather than failing silently.
18 |
19 | ### Type-Safety
20 |
21 | In general, Island Time tries to prevent nonsensical operations at compile time rather than runtime. To that end, you'll find that there are a lot more classes than there are in a number of other date-time libraries.
22 |
--------------------------------------------------------------------------------
/docs/basics/parsing.md:
--------------------------------------------------------------------------------
1 | # Parsing
2 |
3 | ## Predefined Parsers
4 |
5 | Out of the box, Island Time can parse the most common ISO-8601 formats for dates, times, durations, and time intervals. The set of included parsers can be found in [`DateTimeParsers`](../api/core/core/io.islandtime.parser/-date-time-parsers/index.md).
6 |
7 | The table below illustrates how the parsers for the various ISO formats are organized within `DateTimeParsers`, using the calendar date format as an example:
8 |
9 | | Iso Format | Parser | Acceptable Input(s) |
10 | | --- | --- | --- |
11 | | Basic | `DateTimeParsers.Basic.CALENDAR_DATE` | `20200101` |
12 | | Extended | `DateTimeParsers.Extended.CALENDAR_DATE` | `2020-01-01` |
13 | | Any | `DateTimeParsers.CALENDAR_DATE` | `20200101` or `2020-01-01` |
14 |
15 | The extended format is — by far — the most common. If you don't specify a parser explicitly when converting a string to an Island Time type, it will look for extended format only. Below are some examples:
16 |
17 | ```kotlin
18 | // Parse an extended format date-time
19 | val extendedDateTime = "2020-12-31T13:45".toDateTime()
20 |
21 | // Parse a basic format date-time
22 | val basicDateTime = "20201231T1345".toDateTime(DateTimeParsers.Basic.DATE_TIME)
23 |
24 | // Parse an ordinal date (year and day of year)
25 | val ordinalDate = "2020-365".toDate(DateTimeParsers.Extended.ORDINAL_DATE)
26 | ```
27 |
28 | ## Custom Parsers
29 |
30 | In an ideal world, non-ISO formats wouldn’t exist, but sometimes they do and you need to parse them. To support that, you can define custom parsers using a DSL.
31 |
32 | ```kotlin
33 | // Define a custom parser
34 | val customParser = dateTimeParser {
35 | monthNumber()
36 | anyOf({ +'/' }, { +'-' })
37 | dayOfMonth()
38 | optional {
39 | anyOf({ +'/' }, { +'-' })
40 | year()
41 | }
42 | }
43 |
44 | // Parse a date using it
45 | try {
46 | val date = "3/17/2020".toDate(customParser)
47 | } catch (e: DateTimeException) {
48 | // ...
49 | }
50 | ```
51 |
52 | When dealing with ranges and intervals, you'll need to define a "grouped" parser, which can handle multiple results.
53 |
54 | ```kotlin
55 | val customGroupedParser = groupedDateTimeParser {
56 | group {
57 | childParser(customParser)
58 | }
59 | +"--"
60 | group {
61 | childParser(customParser)
62 | }
63 | }
64 |
65 | val dateRange = "3/17/2020--4/5/2020".toDateRange(customGroupedParser)
66 | ```
67 |
--------------------------------------------------------------------------------
/docs/basics/serialization.md:
--------------------------------------------------------------------------------
1 | # Serialization
2 |
3 | Island Time includes built-in support for [Kotlin Serialization](https://github.com/Kotlin/kotlinx.serialization). By default, dates, times, durations, and intervals are serialized as ISO-compatible strings.
4 |
5 | ## Serializing to JSON
6 |
7 | For example purposes, let's assume we have a data structure describing an event that we'd like to serialize.
8 |
9 | ```kotlin
10 | @Serializable
11 | data class EventDto(
12 | val name: String,
13 | val dateRange: DateRange,
14 | val createdAt: Instant
15 | )
16 | ```
17 |
18 | By using the `@Serializable` annotation, we instruct the Kotlin Serialization plugin to generate a serializer for the `EventDto` class. Island Time's [DateRange](../api/core/core/io.islandtime.ranges/-date-range/index.md) and [Instant](../api/core/core/io.islandtime/-instant/index.md) classes will be automatically serialized as ISO-8601 strings.
19 |
20 | Now, we can serialize the `EventDto` class to JSON with the following code:
21 |
22 | ```kotlin
23 | fun writeToJson(val event: EventDto): String {
24 | val json = Json { prettyPrint = true }
25 | return json.encodeToString(EventDto.serializer(), event)
26 | }
27 | ```
28 |
29 | Example output might look something like this:
30 |
31 | ```json
32 | {
33 | "name": "KotlinConf 2019",
34 | "dateRange": "2019-12-04/2012-12-06",
35 | "createdAt": "2020-03-14T14:19:03.478Z"
36 | }
37 | ```
38 |
39 | For more information on how to use Kotlin Serialization, consult the [GitHub page](https://github.com/Kotlin/kotlinx.serialization).
40 |
41 | ## Binary Formats
42 |
43 | At the present time, there are no serializers tuned specifically for binary formats. If you have a use case that requires that, feel free to raise an [issue](https://github.com/erikc5000/island-time/issues).
44 |
--------------------------------------------------------------------------------
/docs/extensions/parcelize.md:
--------------------------------------------------------------------------------
1 | #`@Parcelize`
2 |
3 | The `parcelize-extensions` artifact provides a set of parcelers for use with the [Parcelable implementation generator](https://kotlinlang.org/docs/reference/compiler-plugins.html#parcelable-implementations-generator) plugin.
4 |
5 | ## Gradle Setup
6 |
7 | === "Kotlin"
8 | ```kotlin
9 | dependencies {
10 | implementation("io.islandtime:parcelize-extensions:{{ versions.islandtime }}")
11 | }
12 | ```
13 |
14 | === "Groovy"
15 | ```groovy
16 | dependencies {
17 | implementation "io.islandtime:parcelize-extensions:{{ versions.islandtime }}"
18 | }
19 | ```
20 |
21 | ## Usage
22 |
23 | Custom parcelers are available for each of Island Time's date-time primitives, durations, and intervals, allowing you to use them within `Parcelable` classes.
24 |
25 | ```kotlin
26 | @Parcelize
27 | @TypeParceler