Kotlin / kotlinx-datetime

KotlinX multiplatform date/time library
Apache License 2.0
2.3k stars 92 forks source link

Make Instant.fromEpochMilliseconds available from iOS #78

Open lammertw opened 3 years ago

lammertw commented 3 years ago

Currently using KMM to iOS there is no constructor for Instant. So creating an instant from epoch milliseconds is impossible from iOS unless creating an additional helper function for this purpose in Kotlin.

dkhalanskyjb commented 3 years ago

This is odd. Our tests include code like Instant.fromEpochMilliseconds(Clock.System.now().toEpochMilliseconds()), and they build and run on iOS simulator. Could you please provide of a problematic code snippet and the corresponding error?

lammertw commented 3 years ago

But are those tests written in Swift or Objective-c or in Kotlin. In Kotlin it works. It's just that it's not accessible from Swift or Objective-c. Here is the generated obj-c header of Instant:

__attribute__((objc_subclassing_restricted))
__attribute__((swift_name("Kotlinx_datetimeInstant")))
@interface CommonKotlinx_datetimeInstant : CommonBase <CommonKotlinComparable>
- (int32_t)compareToOther:(CommonKotlinx_datetimeInstant *)other __attribute__((swift_name("compareTo(other:)")));
- (BOOL)isEqual:(id _Nullable)other __attribute__((swift_name("isEqual(_:)")));
- (NSUInteger)hash __attribute__((swift_name("hash()")));
- (CommonKotlinx_datetimeInstant *)minusDuration:(double)duration __attribute__((swift_name("minus(duration:)")));
- (double)minusOther:(CommonKotlinx_datetimeInstant *)other __attribute__((swift_name("minus(other:)")));
- (CommonKotlinx_datetimeInstant *)plusDuration:(double)duration __attribute__((swift_name("plus(duration:)")));
- (int64_t)toEpochMilliseconds __attribute__((swift_name("toEpochMilliseconds()")));
- (NSString *)description __attribute__((swift_name("description()")));
@property (readonly) int64_t epochSeconds __attribute__((swift_name("epochSeconds")));
@property (readonly) int32_t nanosecondsOfSecond __attribute__((swift_name("nanosecondsOfSecond")));
@end;

@interface CommonKotlinx_datetimeInstant (Extensions)
@property (readonly) CommonKotlinx_datetimeLocalDate *localDate __attribute__((swift_name("localDate")));
@end;
lammertw commented 3 years ago

As you can see none of the companion functions are there.

dkhalanskyjb commented 3 years ago

Ah, I see.

It should be noted that this can't be changed in kotlinx-datetime. The issue is with the tooling that generates an Apple framework: when you use a particular class in code that is exported to a framework, only its methods are included in the generated header. Companion objects (and, it seems, some extension methods) are not exported.

There are several possible solutions for this: