Glib 将纪元时间转换为字符串

发布于 2024-08-30 02:40:06 字数 1740 浏览 8 评论 0原文

我正在使用 glibs 函数将纪元时间转换为字符串,如下所示。
但每次它都给了我一些随机的时间。

//Convert Time in string.
GDate *date = g_date_new_julian(timestampsecs);
gchar date_string[50];
g_date_strftime(date_string, 50, (const gchar*)"%a, %I:%M %p", (const GDate*)date);
printf("Date String [%s]\n", date_string );

为什么会发生这种情况?我错过了什么吗?

谢谢,
PP。

更新:

正如我所说,我尝试使用 setlocale(LC_ALL, ""); 以下是输出,
这与我在使用 setlocale 之前得到的结果相同。

(MyTestApp:7747): GLib-WARNING **:
OOPS julian: 1272279703  computed dmy: 66 4293563716 187
Date String Tik_INDEX[1] [1272279703] --> [Tue, 12:00 AM]

(MyTestApp:7747): GLib-WARNING **:
OOPS julian: 1272279717  computed dmy: 80 4293563716 187
Date String Tik_INDEX[2] [1272279717] --> [Tue, 12:00 AM]

(MyTestApp:7747): GLib-WARNING **:
OOPS julian: 1272279720  computed dmy: 53 4293563717 187
Date String Tik_INDEX[3] [1272279720] --> [Fri, 12:00 AM]

(MyTestApp:7747): GLib-WARNING **:
OOPS julian: 1272279721  computed dmy: 54 4293563717 187
Date String Tik_INDEX[4] [1272279721] --> [Sat, 12:00 AM]

(MyTestApp:7747): GLib-WARNING **:
OOPS julian: 1272279721  computed dmy: 54 4293563717 187
Date String Tik_INDEX[5] [1272279721] --> [Sat, 12:00 AM]

(MyTestApp:7747): GLib-WARNING **:
OOPS julian: 1272279722  computed dmy: 55 4293563717 187
Date String Tik_INDEX[6] [1272279722] --> [Sun, 12:00 AM]

(MyTestApp:7747): GLib-WARNING **:
OOPS julian: 1272279723  computed dmy: 56 4293563717 187
Date String Tik_INDEX[7] [1272279723] --> [Mon, 12:00 AM]

(MyTestApp:7747): GLib-WARNING **:
OOPS julian: 1272279724  computed dmy: 57 4293563717 187
Date String Tik_INDEX[8] [1272279724] --> [Tue, 12:00 AM]

I am using glibs functions to convert epoch time to string as follows.
But each time it is giving me some random time.

//Convert Time in string.
GDate *date = g_date_new_julian(timestampsecs);
gchar date_string[50];
g_date_strftime(date_string, 50, (const gchar*)"%a, %I:%M %p", (const GDate*)date);
printf("Date String [%s]\n", date_string );

Why this might be happening? am i missing anything?

Thanks,
PP.

UPDATE:

As said i tried with setlocale(LC_ALL, ""); following is the output,
It is the same as i was getting before using setlocale.

(MyTestApp:7747): GLib-WARNING **:
OOPS julian: 1272279703  computed dmy: 66 4293563716 187
Date String Tik_INDEX[1] [1272279703] --> [Tue, 12:00 AM]

(MyTestApp:7747): GLib-WARNING **:
OOPS julian: 1272279717  computed dmy: 80 4293563716 187
Date String Tik_INDEX[2] [1272279717] --> [Tue, 12:00 AM]

(MyTestApp:7747): GLib-WARNING **:
OOPS julian: 1272279720  computed dmy: 53 4293563717 187
Date String Tik_INDEX[3] [1272279720] --> [Fri, 12:00 AM]

(MyTestApp:7747): GLib-WARNING **:
OOPS julian: 1272279721  computed dmy: 54 4293563717 187
Date String Tik_INDEX[4] [1272279721] --> [Sat, 12:00 AM]

(MyTestApp:7747): GLib-WARNING **:
OOPS julian: 1272279721  computed dmy: 54 4293563717 187
Date String Tik_INDEX[5] [1272279721] --> [Sat, 12:00 AM]

(MyTestApp:7747): GLib-WARNING **:
OOPS julian: 1272279722  computed dmy: 55 4293563717 187
Date String Tik_INDEX[6] [1272279722] --> [Sun, 12:00 AM]

(MyTestApp:7747): GLib-WARNING **:
OOPS julian: 1272279723  computed dmy: 56 4293563717 187
Date String Tik_INDEX[7] [1272279723] --> [Mon, 12:00 AM]

(MyTestApp:7747): GLib-WARNING **:
OOPS julian: 1272279724  computed dmy: 57 4293563717 187
Date String Tik_INDEX[8] [1272279724] --> [Tue, 12:00 AM]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉生梦死 2024-09-06 02:40:06

您可能需要先调用 setlocale(),“LC_ALL”应该这样做POSIX 应该也可以工作,不确定 C 是否有效。

g_date_strftime () 受语言环境设置的影响。存储 g_date_strftime() 的结果或填充静态缓冲区的任何其他内容也很好。您会想知道实际打印到缓冲区的字节数。

You probably want to call setlocale() first, "LC_ALL" should do it. POSIX should also work, not sure about C.

g_date_strftime() is influenced by the locale setting. Its also good to store the result of g_date_strftime(), or anything else that fills a static buffer. You'll want to know how many bytes were actually printed to the buffer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文