Error:‘GTimeVal‘ is deprecated: Use ‘GDateTime‘

Myth丶恋晨 2022-12-26 04:58 299阅读 0赞

参考链接:
https://tecnocode.co.uk/2019/08/24/gtimeval-deprecation-in-glib-2-61-2/

https://developer.gnome.org/glib/stable/glib-Date-and-Time-Functions.html#g-get-real-time

https://developer.gnome.org/glib/stable/glib-Threads.html#g-cond-wait-until

https://people.gnome.org/~ryanl/glib-docs/glib-Date-and-Time-Functions.html#GTimeVal

https://people.gnome.org/~ryanl/glib-docs/glib-Date-and-Time-Functions.html

https://developer.gnome.org/glib/stable/glib-Basic-Types.html#gint64


GTimeValg_get_current_time() 到2038年是不安全的,该结构体和函数已经过时; 需要使用GDateTimeg_get_real_time()代替。

类似的报错:

  1. 'g_get_current_time' is deprecated: Use 'g_get_real_time' instead [-Werror=deprecated-declarations]
  2. 'g_time_val_to_iso8601' is deprecated: Use 'g_date_time_format' instead [-Werror=deprecated-declarations]

解决方法:

GTimeVal tv:

定义:

  1. typedef struct {
  2. glong tv_sec; //秒
  3. glong tv_usec; //微妙
  4. } GTimeVal;

现状:

代表精确的时间,以秒和微秒为单位。类似于UNIX调用返回的struct timevalgettimeofday()
GLib试图统一使用64位整数来表示微秒精度的时间。因此,此类型将从GLib的将来版本中删除。

修改:

  1. - GTimeVal tv;
  2. + gint64 tv;

g_get_current_time:

使用 g_get_real_time() 代替。

  1. - g_get_current_time(&tv);
  2. - tv = g_get_real_time ();

g_time_val_to_iso8601:

  1. - timestr = g_time_val_to_iso8601(&tv);
  2. + g_autoptr(GDateTime) dt=g_date_time_new_from_unix_local(tv);
  3. + timestr = g_date_time_format_iso8601(dt);

总体情况:

  1. - GTimeVal tv;
  2. + gint64 tv;
  3. gchar *timestr;
  4. if (enable_timestamp_msg && !cur_mon) {
  5. - g_get_current_time(&tv);
  6. - timestr = g_time_val_to_iso8601(&tv);
  7. + tv = g_get_real_time ();
  8. + g_autoptr(GDateTime) dt=g_date_time_new_from_unix_local(tv);
  9. + timestr = g_date_time_format_iso8601(dt);
  10. error_printf("%s ", timestr);
  11. g_free(timestr);
  12. }

g_cond_timed_wait():

  1. gboolean
  2. g_cond_timed_wait (GCond *cond,
  3. GMutex *mutex,
  4. GTimeVal *abs_time);

g_cond_timed_wait2.32版开始已弃用,并且不应在新编写的代码中使用。
使用g_cond_wait_until()代替。

g_cond_wait_until():

  1. gboolean
  2. g_cond_wait_until (GCond *cond,
  3. GMutex *mutex,
  4. gint64 end_time);

发表评论

表情:
评论列表 (有 0 条评论,299人围观)

还没有评论,来说两句吧...

相关阅读