timeval 转换为年月日

简介: timeval 转换为年月日
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
char time_string[40];
char *getLocalTime(char *timeStr, int len, struct timeval tv )
{
struct tm *ptm;
// char time_string[40];
    long milliseconds;
 ptm = localtime (&(tv.tv_sec));
 strftime(time_string,len, "%Y-%m-%d %H-%M-%S", ptm );
 milliseconds = tv.tv_usec/1000;
 sprintf(timeStr, "%s.%03ld", time_string,milliseconds );
return time_string;
}
int main(int argc, const char **argv)
{
    char local_time_str[128];
    char *p = NULL;
    struct timeval tv;
    gettimeofday(&tv, NULL);
    p = getLocalTime(local_time_str, sizeof(local_time_str), tv);
    printf("Get local time: \n%s\n", local_time_str);
    return 0;
}

微信图片_20230117180911.png

目录
相关文章
将时间格式转化成时间戳和时间戳转化成时间格式的公式
将时间格式转化成时间戳和时间戳转化成时间格式的公式
|
3月前
|
Java
如何将OffsetDateTime转换为字符串格式的日期
【10月更文挑战第30天】如何将OffsetDateTime转换为字符串格式的日期
103 0
|
9月前
日期字符串转化为年月日
日期字符串转化为年月日
53 0
|
9月前
|
Java
自定义字符串转换为周几和指定日期格式
自定义字符串转换为周几和指定日期格式
53 0
|
9月前
Excel中时间戳与标准日期格式的互相转换
Excel中时间戳与标准日期格式的互相转换
251 0
Excel中时间戳与标准日期格式的互相转换
|
JSON JavaScript 前端开发
uniapp时间戳转换成年月日时分秒的格式
uniapp时间戳转换成年月日时分秒的格式
441 1
uniapp时间戳转换成年月日时分秒的格式
时间戳转化成日期
时间戳转化成日期
82 0
|
前端开发 数据格式
前端实现年月日时分秒的转换
前端实现年月日时分秒的转换
198 0
时间戳转换为日期格式
时间戳转换为日期格式
348 58
日期字符串转换成时间戳
日期字符串转换成时间戳
197 0