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
Add timestamp macro #9057
Add timestamp macro #9057
Conversation
I kind of like the idea of having a method which takes a format string, this seems quite specific. But, it's not terrible. |
The idea is good, but the method is not flexible. I propose a
One can say |
I think this macro method is fine. With it you can construct a time instance and format it however you want. There's no need to do the formatting at compile-time. |
@asterite the formatting here is already done, it is only UNIX epoch. it has even less sense on non-UNIX platforms. |
What if we have the macro emit a |
@j8r if you start from a unix timestamp then you can create a Time instance and format however you want. What can't you do with the current approach? |
@j8r That's a legitimate idea, but I really don't think this flexibility is necessary. I'd like to keep the macro interface as simple as possible. Formatting can easily be done at runtime, and even more than that. (Just to clarify: Unix timestamps are a commonly used format and obviously work on windows, too) |
@straight-shoota I am also saying, Here it is not an "UNIX timestamp", it is a UNIX Epoch time in seconds. A better name would be |
The number is the same as what we use for
I don't think that would be better because then you could legitimately ask "time of what"? Maybe an alternative would be to use a constant instead of a method. We could call it |
@straight-shoota Using a constant for that seems to me like a more clear and elegant solution indeed. |
Ideally, it should probably go into |
@straight-shoota please see Unix Time. It is a standard, well known name. |
It also matches the stdlib. There is no |
@j8r IMHO your explanations seem to completely miss the point that it's the build timestamp, not just a time, whatever the format it may be in. and I'm pretty sure that @straight-shoota knows what the unix time is... xD |
@Sija it is both, in fact. The Unix time build date. Univercal Coordinated Time (UTC) build date is also possible is some places. |
Can we pass this value as an environment variable? That way we won't need this at compile time. |
@asterite I don't follow. How & when would you pass this as env var? The idea of SOURCE_DATE_EPOCH etc. is to be compiled into the binary, so it needs to be determined at compile time. |
# some_program.cr
COMPILETIME = {{env("SOURCE_DATE_EPOCH")}} |
Then we pass |
Why would we implement some of the compiler functionality in a wrapper shell script? So far the one shipped by the official packages is just handling environment setup for the choice of those packages to also ship some more libraries. It's perfectly possible to install the crystal binary into /usr/bin if the distro ships those libraries instead, case in point the Archlinux package doing exactly this. Giving this up for a mandatory environment variable that should a different value for each invocation would not be understandable to me. Having this, or rather usages of this, overridable via an environment variable makes sense to facilitate reproducable builds. But making it mandatory, making it impractical to use the compiler binary without a shellscript wrapper, doesn't seem like a wise solution to be honest. |
I think for compiling the rust compiling you need a lot of env variables. In the case of Ruby, a bot script (matzbot) updates the release time every day, see ruby/ruby@fb40495 I think we are trying to be too smart by moving everything we can into compiler macros, when the plain old solution of getting that information externally so we have a more portable program is probably better. |
@asterite I see what you mean and it make sense. I'd fully support your approach if this was only a rare use case for the compiler. But this is useful for other crystal programs as well. So I'm not 100% sold (yet). But the general idea is quite appealing. In fact, the exact time at which a program was compiled is pretty meaningless. I can take an old source code and old compiler release and compile it now. That doesn't make it a "new" program. It may be years old. Reproducible builds should not care when they happen.
So, what if we fully commit to this way of thinking? I actually don't think it would be a huge issue if we don't provide a default when |
Just in case: I'm fine with merging this as is. I think it can be useful to other programs too. I just wonder how many more stuff like this we'll need to move into macros. Maybe this is the last one. |
We're actually doing the exact same thing with crystal/src/compiler/crystal/config.cr Line 28 in b6e157b
It's just empty when the enviornment variable is missing. No big deal. If you want that information available, you need to make sure the env variables are defined. We can iterate on how to improve the tooling. Those values could be pulled from the checked out git commit, for example. I'm not saying the compiler should do this, but it could be some other tool using the compiler and feeding it context information. |
Closing in favour of #9088. |
This adds a new top level macro
timestamp
which returns the current time as unix timestamp.The value is identical throughout each invocation inside a program and represents the start of the compilation process.
A use case is in the compiler which currently shells out to get the timestamp for
SOURCE_DATE_EPOCH
. This is not portable (see #9054 and #9049) and can easily be replaced by a macro feature.crystal/src/compiler/crystal/config.cr
Line 35 in b6e157b