
本文共 841 字,大约阅读时间需要 2 分钟。
<int SetSystemTime(char *dt);void main(){ system("date"); //原始系统时间 SetSystemTime("2006-4-20 20:30:30"); system("date"); //修改后的系统时间 }
/* 设置系统时间功能说明 .dt的数据格式为"YYYY-MM-DD HH:mm:ss" 使用方法示例: char *pt = "2006-4-20 20:30:30"; SetSystemTime(pt); 返回0表示设置成功,错误则返回-1并提示错误信息 */
int SetSystemTime(char dt){ struct tm t6100; //采用 struct tm 定义时间struct struct timeval tv; int year, month, day, hour, min, sec; //将字符串解析为各部分时间 sscanf(dt, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &min, &sec); t6100.tm_year = year - 1900; //年份转换 t6100.tm_mon = month - 1; //月份转换,0月=1月 t6100.tm_mday = day; //设置天 t6100.tm_hour = hour; t6100.tm_min = min; t6100.tm_sec = sec; //计算时间戳 tv.tv_sec = mktime(&t6100); tv.tv_usec = 0; //设置系统时间 if (settimeofday(&tv, (struct timezone)0) < 0) { printf("设置系统时间失败!\n"); return -1; } return 0; //返回0表示成功 } //编译命令:gcc hello.c -o hello
发表评论
最新留言
关于作者
