-
-
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
Rename Time#epoch to #to_unix #6662
Rename Time#epoch to #to_unix #6662
Conversation
2d60706
to
f78359e
Compare
Please, This is returning time under a new form, not accessing a property of time, so I believe it should be a I never see the need for the millisecond variant, and what about other precisions? I wish we'd kept the |
|
Unix timestamps with millisecond precision are commonly used in many time implementation. It has some practical importance as it is the highest resolution that can represent a substantial time range (> ±300 years) in only 64 bits. For example, it is used in YAML and JSON converters. I'm not sure about the the relevance of floating point value, though. It can easily be calculated from |
I agree that There's an asymmetry in |
|
I'm indecisive about
I think I tend to prefer unix_seconds or a combination with unix for brevity. I'm not sure Unix nanoseconds makes much sense when returning an Int64. It can only represent dates between 1678 and 2262. That probably includes 99% of use cases but it is not defined for dates outside that range. Considering that nanosecond precision is rarely really required I'd say that combining Unix seconds and Maybe Unix timestamp with nanosecond precision could be added at some point when Int128 is fully supported. |
@straight-shoota I just mentioned nanoseconds as a name example in Looking at the table, I'm starting to like the last column. The |
Else a Assuming |
I like the last column too. |
3fafcb1
to
ed4284c
Compare
Yeah, I guess |
Sorry, I got the idea to transform a time to an Unix epoch time, but it's forgetting what the Unix epoch time is really – the time elapsed since a date, conresponding to Rust got it with the We don't have to create all this methods, we want to get time since a date, the one corresponding of the unix epoch. |
ed4284c
to
2a60f9a
Compare
Rebased and ready for review. |
I was thinking about something as simple as: struct Time
UNIX_EPOCH = Time.utc 1970, 1, 1
def to_unix : Time::Span
self - UNIX_EPOCH
end
def self.from_unix(unix_time : Time::Span) : Time
UNIX_EPOCH + unix_time
end
end
Time.utc_now.to_unix.total_seconds Edit: We could also have |
Apart from that tiny tiny nitpick, all good to go from me |
@j8r I like the idea to simplify the API avoiding several methods for different return types. That's definitely a plus. Looking at the use cases in stdlib, the return value is usually used as an integer representing the number of seconds since Unix epoch. In these cases, Getting the number of seconds since Unix epoch as integer is by far the most common use case and it is implemented as a simple integer subtraction. Putting a TIME = Time.utc_now
struct Time
UNIX_EPOCH = Time.utc 1970, 1, 1
def to_unix : Time::Span
self - Time::UNIX_EPOCH
end
end
Benchmark.ips do |bm|
bm.report("int") do
1000.times { TIME.epoch }
end
bm.report("span") do
1000.times { TIME.to_unix.total_seconds }
end
end Maybe a compromise could be to have both, a method for directly getting the number of seconds as int and another method for getting a |
The only purpose of the time in unix is to get an integer that you can pass around to other systems, or store locally. I think getting a time span just makes the API difficult to use for no purpose. |
2a60f9a
to
b129fd8
Compare
Agreed. But there are currently different semantics supported: integer number of seconds, integer number of milliseconds, floating point number of seconds. They currently need three different methods on Obviously, all other use cases besides |
b129fd8
to
092c205
Compare
If everyone strongly wants |
Yes, it's a lot of possibilities from which you'll always choose three in my opinion, so it's better to have those three methods, unless you have real use cases for this. |
|
Right. That doesn't mean we should provide a more complex API for just one or two cases. Just my opinion though, but I don't remember any other API returning a unix time span instead of seconds/milliseconds/etc. |
Rust returns a |
@j8r In #6662 (comment), you probably meant to prefix every property with But |
Disregard all my comments. |
julia and zig are counterexamples, the only language which returns a datatype representing a duration, not an instant, is rust |
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.
I think the history can be squashed for this PR.
I like the new methods, let's hope the time api is coming to a solid point for the users soon.
@RX14 When a number datatype is returned, it also represents a duration, the number of seconds since Unix epoch. It's just debatable if the duration should be expressed as
I think we're getting there! 💯 |
Hi everybody I just want to point out that the above link is to the language reference explaining how to use Documentation Comments (which you can see from the URL) and in no way describes what is available in the Zig Standard Library. In fact, there is no such TimeStamp structure, it does not exist! I have not yet personally audited the time API of Zig, which can be found here: https://github.com/ziglang/zig/blob/master/std/os/time.zig. |
(@andrewrk how did you even find this issue?!?!) |
#!/bin/sh
set -e
s3cmd sync s3://andrewrk/log/ziglang.org/ ziglang.org/
s3cmd del s3://andrewrk/log/ziglang.org/ --recursive
echo -n | gzip >ziglang.org/empty.gz
cat ziglang.org/* | gunzip >>all.txt
rm ziglang.org/*
cat all.txt | cut "--delimiter= " -f10 | sort | uniq >new.txt
touch prev.txt
diff -u prev.txt new.txt | cdiff
mv new.txt prev.txt somebody clicked the link |
The CHANGELOG for 0.27.0 shows that some instance method names are changed in Time class. > - **(breaking-change)** Rename `Time#epoch` to `Time#to_unix`. Also `#epoch_ms` to `#to_unix_ms`, and `#epoch_f` to `#to_unix_f`. ([#6662](#6662), thanks @straight-shoota) Actually, `#epoch_ms` was renamed to `#to_unix_ms`, not `#unix_ms`.
Time.epoch
andTime#epoch
are somewhat misleading because they operate on seconds from Unix epoch (1970-01-01
) but the epoch ofTime
is actually0001-01-01
.A better name forTime#epoch
isunix_seconds
and fits nicely with#total_seconds
(see #6661).Similarly,
#epoch_ms
and#epoch_f
are renamed to#unix_seconds_ms
and#unix_seconds_f
. Maybe the millisecond variant could also be#unix_ms
or#unix_milliseconds
.Time#epoch
is renamed toTime#to_unix
. Similarly,#epoch_ms
and#epoch_f
are renamed to#to_unix_ms
and#to_unix_f
.The supplementing constructors are renamed from
Time.epoch
andTime.epoch_ms
toTime.unix
andTime.unix_ms
. A variant accepting a floating point number does not exist.The following two commits just refactor some argument and variable names in
Time::Location
andTime::Format
to useunix_seconds
instead ofepoch
.See #5346 (comment)