大家好、
我使用的是 Tiva TM4C1294kcpdt。 在我的应用中、我使用的是内置的休眠模块 RTC。 但在系统开机时、我从 NTP 服务器读取 RTC、这为我提供了时区 GMT 0的时间。
我希望根据最终用户选择的时区、在从 NTP 服务器读取时间时设置/转换时区。 例如、将时区设置为 GMT:+5.30 (IST)、如何在 c 中执行此操作? 另外、如何使用'TZ'结构在 time.h 文件中设置时区?
感谢您的任何帮助。
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.
大家好、
我使用的是 Tiva TM4C1294kcpdt。 在我的应用中、我使用的是内置的休眠模块 RTC。 但在系统开机时、我从 NTP 服务器读取 RTC、这为我提供了时区 GMT 0的时间。
我希望根据最终用户选择的时区、在从 NTP 服务器读取时间时设置/转换时区。 例如、将时区设置为 GMT:+5.30 (IST)、如何在 c 中执行此操作? 另外、如何使用'TZ'结构在 time.h 文件中设置时区?
感谢您的任何帮助。
您好、Urvi、
我们的示例仅使用 time.h 结构作为 Hibernate API 的框架。 因此、其中内置的 TZ 结构不支持、这些示例不使用特定于 LS/RTS 编译器的 time.h API。
您需要根据获得的输出进行这些调整,作为应用程序修改,或者让用户在开始时输入其时区,并根据时区调整日历设置。 此时、根据时区从接收到的数据中添加/减去小时数和/或分钟仅是数学运算。
尊敬的 Bob Crosby:
谢谢、这对我很有用。 但现在我在 TZ 结构中硬编码了'timeZone=19800'。 但是、如果我想根据用户配置在运行时更改时区、该怎么办?
如何或通过使用哪个函数来设置运行时的时区?
我无法将语句用作:_tz.timezone=19800;它给出了如下错误:
#148声明与"TZ _tz"不兼容(在"C:/ti/ccsv6/tools/compiler/arm_5.1.6/include/time.h "的第114行声明)
66期待一个“;”
这看起来像是 C 语法错误。 这实际上是一个 C 运行时支持问题、而不是 TM4C129x 问题。 您实际上不需要处理 TZ 结构、只需对 time_t 变量应用适当的偏移即可。 以下是在 PC (而不是 TM4C129)上运行的示例代码。
#include
#include
#include
#define IST_OFFSET ((TIME_t)-19800)
int main ()
{
time_t time_tUTCTime;
time_t time_tISTime;
struct tm * ptmUTCTime;
struct tm * ptmISTime;
printf ("Hello world!\n");
time_tUTCTime =时间(&time_tUTCTime);
ptmUTCTime = gmTime (&time_tUTCTime);
printf ("UTC 为:%s"、ascTime (ptmUTCTime));
time_tISTime = time_tUTCTime - IST_offset;
ptmISTime = gmTime (&time_tISTime);
printf ("IST 为:%s"、asctime (ptmISTime));
返回0;
}
在 TM4C129器件上、您将使用 NTP 服务器返回的值替换第15行中的初始化 time_tUTCTime 变量。