1、strftime
函数的功能是把一个tm结构的时间序列化为一个字符串
#include <time.h>
size_t strftime(char *s, size_t max, const char *format,
const struct tm *tm);
参数:
char *s: 字符串指针 输出
size_t max: 字符串最大长度 输入
const char *format: 输出格式
const struct tm *tm: 时间 输入
返回值:
成功:处理结果字符串str中字符的个数
失败:0
格式如下:
函数的功能是把一个字符串序列化为一个tm结构的时间
#define _XOPEN_SOURCE /* glibc2 needs this */
#include <time.h>
char *strptime(const char *s, const char *format, struct tm *tm);
参数:
const char *s: 字符串 输入
const char *format: 输入的格式
tm: 时间 输出
返回值:
成功:*s的最后位置
失败:NULL
3、stime
设置本地系统时间
#include <time.h>
int stime(time_t *t);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
stime(): _SVID_SOURCE
参数:
t: 日历时间 输入
返回值:
成功: 0
失败:-1
4、代码示例
设置本地系统时间
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
void SyncDateTime(const char *time)
{
struct tm temp;
printf("need to syncDateTime: %s\n", time);
if (strptime(time, "%Y-%m-%d %H:%M:%S", &temp) == NULL)
{
perror("strptime failed.\n");
}
time_t t = mktime(&temp);
if (stime(&t) == -1)
{
perror("stime failed.\n");
}
return ;
}
int main()
{
const char buf[] = "2015-03-05 10:11:50";
SyncDateTime(buf);
return 0;
}
1万+



被折叠的 条评论
为什么被折叠?



