-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Improve #inspect format for Time types #5794
Conversation
I'd expect inspect to include nanoseconds even if nanoseconds was 0. |
IMO it just adds unnecessary clutter. And But I see the point, that it adds uncertainty when a time instance is printed, because it can mean nanoseconds is just omitted (e.g. when calling What do you think about printing only one decimal place ( |
d034325
to
5a77720
Compare
@straight-shoota yes that's fine |
83a1f92
to
762a467
Compare
(this answers a deleted comment) ISO 8601 or RFC 3339 only support fixed offsets. Two instances of If time zone rules don't matter, you should just use |
762a467
to
e907c7a
Compare
My review got lost, but in essence:
Actually, I believe all Time instances should be shifted to localtime —I'm probably wrong, but I never got a use-case where keeping the original location was essential or even useful. The fact that international standards (ISO 8601) don't specify locations is a huge hint to me that what's important is the offset (structural deviation from UTC), not the location (context specific representational concept). |
I now concur with @RX14 that nanoseconds should better be explicit. It's obviously very unlikely that this gets confused with a custom display of a non zero-nanosecond time (for example `time.to_s("%F %T %Z #{time.location}")), but I think we should better be safe than sorry. IMHO the additional noise is acceptable. I'm not sure about I like the idea of specifying a location for formatting, but I would just add it as an argument to What do you mean with "all Time instances should be shifted to localtime"? The location is usually not that important, that's true. But it depends on what you're doing with your |
After thinking about this, I'm pretty sure Following this reasoning, I would rather consider removing the location from the default format than adding nanoseconds. |
nah, we should keep every detail including nanoseconds and zone on |
e907c7a
to
b1169b3
Compare
@RX14's suggestion seems reasonable, |
needs rebase for CI fix |
b1169b3
to
f1cbddf
Compare
Rebased |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docs seem to be out of date.
src/time.cr
Outdated
to_s "%F %T %:z", io | ||
# Prints this `Time` to *io*. | ||
# | ||
# The local date-time is formatted as ISO 8601 date string `YYYY-MM-DD HH:mm:ss.nnnnnnnnn +ZZ:ZZ:ZZ`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that string isn't iso 8601 and the example doesn't include the zone name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why isn't it ISO 8601?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Zone name is mentioned in the next paragraph.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no T, and there's a space between the nanoseconds and the +, and iso 8601 only permits hh:mm offset
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
T
designator can be omitted, but the additional white space is true. The question is: should it be removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, T
can't be omitted in practice. We can talk about the standard details all we want but in practice iso 8601 means rfc3339. We should leave the format as-is but not advertise to_s
as conforming to any spec.
src/time.cr
Outdated
io | ||
end | ||
|
||
# Prints this `Time` to *io*. | ||
# | ||
# The local date-time is formatted as ISO 8601 date string `YYYY-MM-DD HH:mm:ss +ZZ:ZZ:ZZ`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also not iso 8601.
src/time.cr
Outdated
# The local date-time is formatted as ISO 8601 date string `YYYY-MM-DD HH:mm:ss +ZZ:ZZ:ZZ`. | ||
# Nanoseconds are always omitted. Offset seconds are omitted if `0`. | ||
# | ||
# The name of the location is appended unless it is a fixed zone offset. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shouldn't be.
f1cbddf
to
f471ab1
Compare
GTG? |
`#inspect` now prints `nanoseconds` unless it is `0` and uses `Time::Zone#format`. Location name is no longer omitted if `"Local"`, otherwise there would be no distinction between a fixed offset time zone and local time zone because both would just show the offset and no name. `#to_s` still omits nanoseconds and time zone name.
f471ab1
to
96bc5d3
Compare
Merge and milestone? |
* Improve #inspect format for Time::Location types * Add Time::Zone#format and use it in Time::Formatter * Improve format of Time#inspect `#inspect` now prints `nanoseconds` unless it is `0` and uses `Time::Zone#format`. Location name is no longer omitted if `"Local"`, otherwise there would be no distinction between a fixed offset time zone and local time zone because both would just show the offset and no name. `#to_s` still omits nanoseconds and time zone name.
I'm doing some housekeeping to normalize and improve the inspect (and to_s) output of time and location related types.
Time#inspect
now includes nanosecondsunless it is.0
#to_s
still omits them to avoid cluttering the output. When calling#inspect
you typically expect a detailed output that allows to distinguish the difference between two instances. While nanoseconds are usually not relevant, it should still show up in case it is significant.Time::Zone
now includes a#format
method which is used to print the offset as+HH:mm:ss
. This format is added to the#inspect
output and also used byTime::Formatter#time_zone
.