This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] MSP430F6720:在哪里可以找到 localtime()和 gmtime()的源代码大概位于 time.c 中?

Guru**** 2383300 points
请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/msp-low-power-microcontrollers-group/msp430/f/msp-low-power-microcontroller-forum/994574/msp430f6720-where-can-i-find-the-source-code-for-localtime-and-gmtime-presumably-in-time-c

器件型号:MSP430F6720

您好!

我正在尝试将 Unix epoch 时间转换为 CCS V10/TI v20.x.y 编译器中 MSP430使用的时间。 大概 TI 编译器使用从1900-01-01 00:00:00开始的 epoch 时间、至少以下代码指示了这种情况。

#include "time.h"

// Unix epoch time entered via UART console
uint32_t unixEpochTime = (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
unixEpochTime += (uint32_t)2208988800; /*CCS use time since 1900-1-1 00:00:00*/

struct tm* calendarTime = gmtime(&unixEpochTime);

uint8_t debugInfo[48];
sprintf(debugInfo, "%04d-%02d-%02d %02d:%02d:%02d, ", calendarTime->tm_year + 1900, calendarTime->tm_mon + 1, calendarTime->tm_mday, calendarTime->tm_hour, calendarTime->tm_min, calendarTime->tm_sec);
sendCmdArray((uint8_t *)debugInfo, 21); // Output via UART console

如果我输入0x00000001 1秒钟(从 epoch 开始)或00000E10持续1小时(从 epoch 开始)、我将按预期获得以下日历时间:

1970-01-01 00:00:01 // 1 second since epoch, as expected
1970-01-01 01:00:00 // 1 hour since epoch, as expected

但是、如果我在 epoch 之后的1天内输入0x00015180、我会得到一个不同于我预期的输出:

1970-01-01 05:47:44   // It should be 1970-01-02 00:00:00

我在这里错过了什么?

谢谢。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
    [引用 userid="192864" URL"~/support/microcontrollers/msp430/f/msp-low-power-microcontroller-forum/994574/msp430f6720-where-can-i-find-the-source-code-for-localtime-and-gmtime-presumably-in-time-c "]但是,如果我输入0x00015180 1天(从 epoch 开始),我会得到一个不同于我预期的输出:

    值0x00005180表示1970-01-01 05:47:44、这是您得到的输出。

    在 MSP430上、int 为16位。

    在以下代码中、我认为 bytes[]的索引将提升为16位的无符号整型值、因此"(bytes[0]<< 24)"和"(bytes[1]<< 16)"将在以下代码中以零结尾:

    uint32_t unixEpochTime = (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];

    尝试将代码更改为:

    uint32_t unixEpochTime = ((uint32_t) bytes[0] << 24) | ((uint32_t) bytes[1] << 16) | ((uint32_t) bytes[2] << 8) | (uint32_t) bytes[3];

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好、Gillon 先生、

    感谢您花时间识别我在输入中产生的误差。 我应该自己注意到这个部分。

    下一行中的类型解决了我的实际问题。 我认为这是在实施本地时间/本地时间方面的事情。

    uint32_t unixEpochTime = ((uint32_t) bytes[0] << 24) | ((uint32_t) bytes[1] << 16) | ((uint32_t) bytes[2] << 8) | (uint32_t) bytes[3];

    我想、为 bytes[0]<< 24和 bytes[1]<< 16创建了一个默认类型为 int 的时间变量、该变量为16位。 我没有这么多的思考,但显然这引起了几个小时的痛苦。