将字符串从 __DATE__ 转换为 time_t
我正在尝试将从 __DATE__
宏生成的字符串转换为 time_t
。我不需要一个成熟的日期/时间解析器,只处理 __DATE__
宏格式的东西就很好了。
预处理器方法会很漂亮,但函数也同样有效。如果相关的话,我正在使用 MSVC。
I'm trying to convert the string produced from the __DATE__
macro into a time_t
. I don't need a full-blown date/time parser, something that only handles the format of the __DATE__
macro would be great.
A preprocessor method would be nifty, but a function would work just as well. If it's relevant, I'm using MSVC.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
编辑:更正后的函数应如下所示:
Edit: the corrected function should look something like this:
根据 gcc.gnu.org 给出的描述进行构建可以使用以下宏在编译时获取日期。
Basing on the description given at gcc.gnu.org the build date can be obtain at the compilation time using following macros.
杰瑞的功能看起来很棒。这是我的尝试..我也加入了 __TIME__ 。
Jerry's function looks great. Here's my attempt.. I threw in the __TIME__ as well.
我不知道其他 Arduino 黑客是否会偶然发现这个问题,但我发现 @JerryCoffin 的答案对于解决我的项目的这个问题非常有帮助。这是一个完整的示例,您可以粘贴到 Arduino 中。它使用此处引用的时间库。
以下是串行终端结果...
如果您只想使用
__DATE__
并且不需要time_t
或tmElements_t
对象,代码可以简单得多。这是串行终端结果...
I don't know if any other Arduino hackers will stumble upon this question, but I found @JerryCoffin's answer to be quite helpful in solving this problem for my project. Here is a complete example you can paste into Arduino. It uses the Time lib referenced here.
Here are the Serial Terminal results...
If all you want is to use the
__DATE__
and you don't need atime_t
ortmElements_t
object, the code can be much simpler.Here are the Serial Terminal results...
以下是我如何修改您的示例,以便与 C++ 中的 Arm32 微控制器的 mbed 一起使用。
请参阅:https://developer.mbed.org/users/joeata2wh/code/compile_time_to_system_time/ 获取完整代码。另请参阅 https: //developer.mbed.org/users/joeata2wh/code/xj-Init-clock-to-compile-time-if-not-alr/ 是我如何使用它来初始化基于时钟芯片的示例在编译时。
Here is how I modified your sample to use with mbed for Arm32 micro controllers in C++.
See: https://developer.mbed.org/users/joeata2wh/code/compile_time_to_system_time/ for complete code. Also see https://developer.mbed.org/users/joeata2wh/code/xj-Init-clock-to-compile-time-if-not-alr/ for an example of how I use it to initialize a clock chip based on the compile time.
Bruno 的回复对我的 Arduino 项目非常有用。这是一个同时包含 __DATE__ 和 __TIME__ 的版本
Bruno's response was very useful for my Arduino Project. Here is a version with both __DATE__ and __TIME__
对于月份,我想出了以下 2 个宏:
支持除“???”之外的所有日期:
支持包括“???”在内的所有日期:
这是如何工作的?日期的第三个字符有 10 个唯一的 ASCII 值,将第二个字符添加到其中会产生 12 个唯一值。我尝试了不同的求和组合、按位或、按位与和按位异或。添加第二个和第三个字符被证明是唯一具有必要的 12/13 唯一值的字符。使用位移位、掩码和其他运算符可能会产生产生 12 个唯一值的其他组合。
模运算符和减法将范围缩小到 0 到 14 或 0 到 22(在循环中搜索时发现)。
然后得到的整数用作查找表中的索引(存储为字符串)。我映射了日期“???”到 13 ('n'-'a'),但如果需要,您可以将其映射到另一个值。
我复制的宏的重置:
为了将值存储在 time_t 结构中,您需要添加偏移量:
对于 __TIME__ 我使用这些:
For month I came up with the following 2 macros:
Supports all dates except "???":
Supports all dates including"???":
How this works? The third character of date has 10 unique ascii values, adding the second character to it results in 12 unique values. I tried different combinations of sums, bitwise OR, bitwise AND and bitwise XOR. Adding second and third character proved to be the only one with the necessary 12/13 unique values. Using bitshifting, masking and other operators may result in other combinations that produce 12 unique values.
The modulo operator and the subtraction reduces the range to 0 to 14 or to 0 to 22 (found with searching in a loop).
Then the resulting integer is used as an index in the lookup table (stored as a string). I mapped the date "???" to 13 ('n'-'a'), but you can map it to another value if you want.
The reset of the macros I copied:
In order to store the values in a time_t struct you need to add the offsets:
for __TIME__ I use these:
在此处回答。
DATE 格式的规范位于此处。
Answer here.
Spec for the format of DATE is here.