您的位置:首页 > 运维架构 > Linux

linux--时间编程(5)

2016-07-21 12:03 405 查看

概念

分类:1.标准时间 2.日历时间(从一个时间开始算起)

代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, char **argv)
{
time_t mtime;
struct tm *mytm;

char *file_name = "fff";
struct stat mystat;

stat(file_name, &mystat);
mtime =mystat.st_mtime;
printf("cur time=%ld\n", mtime);

mytm = localtime(&mtime);
printf("sec:%d,min:%d,hour:%d,year:%d\n",
mytm->tm_sec, mytm->tm_min, mytm->tm_hour, mytm->tm_year+1900);
//将TM格式的时间转化为字符串
printf("==time is :%s\n", asctime(mytm));
//将日历时间转化为本地时间字符串
printf("==time is :%s", ctime(&mtime));
return 0;
}


stat

stat函数讲解

表头文件:    #include <sys/stat.h>
#include <unistd.h>
定义函数:    int stat(const char *file_name, struct stat *buf);
函数说明:    通过文件名filename获取文件信息,并保存在buf所指的结构体stat中
返回值:      执行成功则返回0,失败返回-1,错误代码存于errno

错误代码:
ENOENT         参数file_name指定的文件不存在
ENOTDIR        路径中的目录存在但却非真正的目录
ELOOP          欲打开的文件有过多符号连接问题,上限为16符号连接
EFAULT         参数buf为无效指针,指向无法存在的内存空间
EACCESS        存取文件时被拒绝
ENOMEM         核心内存不足
ENAMETOOLONG   参数file_name的路径名称太长
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: