Skip to content

Commit 85cd1cc

Browse files
committed
migration: use GDateTime for formatting timestamp in snapshot names
The GDateTime APIs provided by GLib avoid portability pitfalls, such as some platforms where 'struct timeval.tv_sec' field is still 'long' instead of 'time_t'. When combined with automatic cleanup, GDateTime often results in simpler code too. Reviewed-by: Dr. David Alan Gilbert <[email protected]> Signed-off-by: Daniel P. Berrangé <[email protected]>
1 parent 99be1ac commit 85cd1cc

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

migration/savevm.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2775,8 +2775,7 @@ bool save_snapshot(const char *name, bool overwrite, const char *vmstate,
27752775
QEMUFile *f;
27762776
int saved_vm_running;
27772777
uint64_t vm_state_size;
2778-
qemu_timeval tv;
2779-
struct tm tm;
2778+
g_autoptr(GDateTime) now = g_date_time_new_now_local();
27802779
AioContext *aio_context;
27812780

27822781
if (migration_is_blocked(errp)) {
@@ -2836,9 +2835,8 @@ bool save_snapshot(const char *name, bool overwrite, const char *vmstate,
28362835
memset(sn, 0, sizeof(*sn));
28372836

28382837
/* fill auxiliary fields */
2839-
qemu_gettimeofday(&tv);
2840-
sn->date_sec = tv.tv_sec;
2841-
sn->date_nsec = tv.tv_usec * 1000;
2838+
sn->date_sec = g_date_time_to_unix(now);
2839+
sn->date_nsec = g_date_time_get_microsecond(now) * 1000;
28422840
sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
28432841
if (replay_mode != REPLAY_MODE_NONE) {
28442842
sn->icount = replay_get_current_icount();
@@ -2849,9 +2847,8 @@ bool save_snapshot(const char *name, bool overwrite, const char *vmstate,
28492847
if (name) {
28502848
pstrcpy(sn->name, sizeof(sn->name), name);
28512849
} else {
2852-
/* cast below needed for OpenBSD where tv_sec is still 'long' */
2853-
localtime_r((const time_t *)&tv.tv_sec, &tm);
2854-
strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2850+
g_autofree char *autoname = g_date_time_format(now, "vm-%Y%m%d%H%M%S");
2851+
pstrcpy(sn->name, sizeof(sn->name), autoname);
28552852
}
28562853

28572854
/* save the VM state */

0 commit comments

Comments
 (0)