File: libtcmu_time.c

package info (click to toggle)
tcmu 1.5.4-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,104 kB
  • sloc: ansic: 15,893; sh: 83; makefile: 18; xml: 18
file content (41 lines) | stat: -rw-r--r-- 891 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
 * Copyright 2017 China Mobile, Inc.
 *
 * This file is licensed to you under your choice of the GNU Lesser
 * General Public License, version 2.1 or any later version (LGPLv2.1 or
 * later), or the Apache License 2.0.
 */

#include <sys/time.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#include "libtcmu_time.h"

int time_string_now(char* buf)
{
	struct tm *tm;
	struct timeval tv;

	if (gettimeofday (&tv, NULL) < 0)
		return -1;

	/* The value maybe changed in multi-thread*/
	tm = localtime(&tv.tv_sec);
	if (tm == NULL)
		return -1;

	tm->tm_year += 1900;
	tm->tm_mon += 1;

	if (snprintf(buf, TCMU_TIME_STRING_BUFLEN,
	    "%4d-%02d-%02d %02d:%02d:%02d.%03d",
	    tm->tm_year, tm->tm_mon, tm->tm_mday,
	    tm->tm_hour, tm->tm_min, tm->tm_sec,
	    (int) (tv.tv_usec / 1000ull % 1000)) >= TCMU_TIME_STRING_BUFLEN)
		return ERANGE;

	return 0;
}