Error:‘GTimeVal‘ is deprecated: Use ‘GDateTime‘
参考链接:
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
GTimeVal
和g_get_current_time()
到2038年是不安全的,该结构体和函数已经过时; 需要使用GDateTime
和g_get_real_time()
代替。
类似的报错:
'g_get_current_time' is deprecated: Use 'g_get_real_time' instead [-Werror=deprecated-declarations]
'g_time_val_to_iso8601' is deprecated: Use 'g_date_time_format' instead [-Werror=deprecated-declarations]
解决方法:
GTimeVal tv:
定义:
typedef struct {
glong tv_sec; //秒
glong tv_usec; //微妙
} GTimeVal;
现状:
代表精确的时间,以秒和微秒为单位。类似于UNIX调用返回的
struct timevalgettimeofday()
。GLib
试图统一使用64
位整数来表示微秒精度的时间。因此,此类型将从GLib
的将来版本中删除。
修改:
- GTimeVal tv;
+ gint64 tv;
g_get_current_time:
使用 g_get_real_time()
代替。
- g_get_current_time(&tv);
- tv = g_get_real_time ();
g_time_val_to_iso8601:
- timestr = g_time_val_to_iso8601(&tv);
+ g_autoptr(GDateTime) dt=g_date_time_new_from_unix_local(tv);
+ timestr = g_date_time_format_iso8601(dt);
总体情况:
- GTimeVal tv;
+ gint64 tv;
gchar *timestr;
if (enable_timestamp_msg && !cur_mon) {
- g_get_current_time(&tv);
- timestr = g_time_val_to_iso8601(&tv);
+ tv = g_get_real_time ();
+ g_autoptr(GDateTime) dt=g_date_time_new_from_unix_local(tv);
+ timestr = g_date_time_format_iso8601(dt);
error_printf("%s ", timestr);
g_free(timestr);
}
g_cond_timed_wait():
gboolean
g_cond_timed_wait (GCond *cond,
GMutex *mutex,
GTimeVal *abs_time);
g_cond_timed_wait
从2.32
版开始已弃用,并且不应在新编写的代码中使用。
使用g_cond_wait_until()
代替。
g_cond_wait_until():
gboolean
g_cond_wait_until (GCond *cond,
GMutex *mutex,
gint64 end_time);
还没有评论,来说两句吧...