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

目录
相关文章
将时间格式转化成时间戳和时间戳转化成时间格式的公式
将时间格式转化成时间戳和时间戳转化成时间格式的公式
|
4月前
Excel中时间戳与标准日期格式的互相转换
Excel中时间戳与标准日期格式的互相转换
62 0
Excel中时间戳与标准日期格式的互相转换
|
6月前
|
JSON JavaScript 前端开发
uniapp时间戳转换成年月日时分秒的格式
uniapp时间戳转换成年月日时分秒的格式
104 1
uniapp时间戳转换成年月日时分秒的格式
|
7月前
时间戳转化成日期
时间戳转化成日期
30 0
|
9月前
计算日期到天数转换
计算日期到天数转换
54 0
|
10月前
|
前端开发 数据格式
前端实现年月日时分秒的转换
前端实现年月日时分秒的转换
108 0
dateTime怎么根据日期(年月日)查找数据
dateTime怎么根据日期(年月日)查找数据
117 0
dateTime怎么根据日期(年月日)查找数据
日期字符串转换成时间戳
日期字符串转换成时间戳
140 0
|
JavaScript Kotlin
时间戳转化为时分秒格式
时间戳转化为时分秒格式
340 0
时间戳转换为日期格式
时间戳转换为日期格式
200 0