Linux 获取当前系统时间(year-month-day h:m:s weekday )
2012-09-27 13:08
651 查看
#include <stdio.h> #include <time.h> #include <string.h> int weekday(int w , char *buff) { switch(w) { case 0: sprintf(buff , "%s" , "星期一"); break; case 1: sprintf(buff , "%s" , "星期二"); break; case 2: sprintf(buff , "%s" , "星期三"); break; case 3: sprintf(buff , "%s" , "星期四"); break; case 4: sprintf(buff , "%s" , "星期五"); break; case 5: sprintf(buff , "%s" , "星期六"); break; case 6: sprintf(buff , "%s" , "星期日"); break; } return 0; } /* *get local time (year-month-day hour-min-sec weekday) * */ int getCurTime(char *strTime , int type) { time_t t; char buff[10]; struct tm *tm = NULL; t = time(NULL); if(t == -1) { return -1; } tm = localtime(&t); weekday(tm->tm_wday , buff); if(tm == NULL) { return -1; } if(type == 0) { sprintf(strTime , "%d年%d月%d日 %d:%d:%d %s\n" , tm->tm_year + 1900 , tm->tm_mon + 1, tm->tm_mday , tm->tm_hour , tm->tm_min , tm->tm_sec , buff); } else if(type == 1 ) { sprintf(strTime , "%d-%d-%d %d:%d:%d %s\n" , tm->tm_year + 1900 , tm->tm_mon + 1, tm->tm_mday , tm->tm_hour , tm->tm_min , tm->tm_sec, buff); } else if(type == 2) { sprintf(strTime , "%d_%d_%d %d:%d:%d %s\n" , tm->tm_year + 1900 , tm->tm_mon + 1, tm->tm_mday , tm->tm_hour , tm->tm_min , tm->tm_sec, buff); } return 0; } int main(int argc , char **argv) { char buff[60]; getCurTime(buff , 0); printf("%s \n " , buff); return 0; }
本文出自 “Record” 博客,请务必保留此出处http://mjrao.blog.51cto.com/6086668/1008049
相关文章推荐
- django 获取系统当前时间 和linux 系统当前时间不一致 问题处理。
- linux java/jsp 获取系统当前时间问题
- Linux下用C获取当前系统时间
- Linux获取系统当前时间(精确到毫秒)
- Linux获取系统当前时间(精确到毫秒)
- 【Linux学习】 写一个简单的Makefile编译源码获取当前系统时间
- linux下使用gettimeofday获取当前时间的注意事项
- Linux下获取系统当前时间函数实现
- linux获取系统当前时间
- MDX:获取当前日期及Year,Month,Day
- django 获取系统当前时间 和linux 系统当前时间不一致 问题处理。
- Linux获取系统当前时间(精确到毫秒)
- (笔记)linux c获取当前时间的函数gettimeofday(毫秒微秒)
- 关于linux下获取系统当前时间的方法汇总
- linux中获取系统时间 gettimeofday函数
- linux 获取系统当前时间
- linux获取系统当前时间
- linux获取当前系统时间与启动时间
- linux驱动层获取当前的系统时间
- Linux获取系统当前时间(精确到毫秒)