Skip to content

Make indexstoredb_symbol_location_timestamp and indexstoredb_timestamp_of_latest_unit_for_file return nanoseconds instead of seconds #192

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

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/IndexStoreDB/IndexStoreDB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ public final class IndexStoreDB {
}

/// Returns the latest modification date of a unit that contains the given source file.
///
///
/// If no unit containing the given source file exists, returns `nil`.
public func dateOfLatestUnitFor(filePath: String) -> Date? {
let timestamp = filePath.withCString { filePathCString in
Expand All @@ -460,7 +460,7 @@ public final class IndexStoreDB {
if timestamp == 0 {
return nil
}
return Date(timeIntervalSince1970: timestamp)
return Date(timeIntervalSince1970: Double(timestamp) / 1_000_000_000)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/IndexStoreDB/SymbolLocation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extension SymbolLocation: CustomStringConvertible {
extension SymbolLocation {
internal init(_ loc: indexstoredb_symbol_location_t) {
path = String(cString: indexstoredb_symbol_location_path(loc))
timestamp = Date(timeIntervalSince1970: indexstoredb_symbol_location_timestamp(loc))
timestamp = Date(timeIntervalSince1970: Double(indexstoredb_symbol_location_timestamp(loc)) / 1_000_000_000)
moduleName = String(cString: indexstoredb_symbol_location_module_name(loc))
isSystem = indexstoredb_symbol_location_is_system(loc)
line = Int(indexstoredb_symbol_location_line(loc))
Expand Down
12 changes: 6 additions & 6 deletions include/CIndexStoreDB/CIndexStoreDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ indexstoredb_index_related_symbol_occurrences_by_usr(
_Nonnull indexstoredb_symbol_occurrence_receiver_t);

/// Iterates over all the symbols contained in \p path
///
///
/// The symbol passed to the receiver is only valid for the duration of the
/// receiver call.
INDEXSTOREDB_PUBLIC bool
Expand Down Expand Up @@ -375,10 +375,10 @@ INDEXSTOREDB_PUBLIC
const char * _Nonnull
indexstoredb_symbol_location_path(_Nonnull indexstoredb_symbol_location_t);

/// Returns a Unix timestamp (seconds since 1/1/1970) at which the unit file that contains a symbol has last been
/// Returns a Unix timestamp (nanoseconds since 1/1/1970) at which the unit file that contains a symbol has last been
/// modified.
INDEXSTOREDB_PUBLIC
double
uint64_t
indexstoredb_symbol_location_timestamp(_Nonnull indexstoredb_symbol_location_t loc);

/// Returns the module name of the given symbol location.
Expand Down Expand Up @@ -577,10 +577,10 @@ indexstoredb_index_unit_tests(
_Nonnull indexstoredb_symbol_occurrence_receiver_t receiver
);

/// Returns a Unix timestamp (seconds since 1/1/1970) of the latest unit that contains the given source file.
///
/// Returns a Unix timestamp (nanoseconds since 1/1/1970) of the latest unit that contains the given source file.
///
/// If no unit containing the given source file exists, returns 0.
INDEXSTOREDB_PUBLIC double
INDEXSTOREDB_PUBLIC uint64_t
indexstoredb_timestamp_of_latest_unit_for_file(
_Nonnull indexstoredb_index_t index,
const char *_Nonnull fileName
Expand Down
10 changes: 4 additions & 6 deletions lib/CIndexStoreDB/CIndexStoreDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,15 +445,14 @@ indexstoredb_symbol_location_path(indexstoredb_symbol_location_t loc) {
return obj->getPath().getPathString().c_str();
}

double
uint64_t
indexstoredb_symbol_location_timestamp(indexstoredb_symbol_location_t loc) {
auto obj = (SymbolLocation *)loc;
// Up until C++20 the reference date of time_since_epoch is undefined but according to
// https://en.cppreference.com/w/cpp/chrono/system_clock most implementations use Unix Time.
// Since C++20, system_clock is defined to measure time since 1/1/1970.
// We rely on `time_since_epoch` always returning the nanoseconds since 1/1/1970.
auto nanosecondsSinceEpoch = obj->getPath().getModificationTime().time_since_epoch().count();
return static_cast<double>(nanosecondsSinceEpoch) / 1000 / 1000 / 1000;
return obj->getPath().getModificationTime().time_since_epoch().count();
}

const char *
Expand Down Expand Up @@ -668,7 +667,7 @@ indexstoredb_index_unit_tests(
});
}

INDEXSTOREDB_PUBLIC double
INDEXSTOREDB_PUBLIC uint64_t
indexstoredb_timestamp_of_latest_unit_for_file(
_Nonnull indexstoredb_index_t index,
const char *_Nonnull fileName
Expand All @@ -680,8 +679,7 @@ indexstoredb_timestamp_of_latest_unit_for_file(
// https://en.cppreference.com/w/cpp/chrono/system_clock most implementations use Unix Time.
// Since C++20, system_clock is defined to measure time since 1/1/1970.
// We rely on `time_since_epoch` always returning the nanoseconds since 1/1/1970.
auto nanosecondsSinceEpoch = timePoint->time_since_epoch().count();
return static_cast<double>(nanosecondsSinceEpoch) / 1000 / 1000 / 1000;
return timePoint->time_since_epoch().count();
}
return 0;
}
Expand Down