在minix2.0源代码中,有相当经典的时间转换函数实现(src\ src\ lib\ ansi\ asctime.c),今天我们就来分析一下asctime.c中的源码

首先引入几个相关的头文件:

1、time.h 主要的结构体与相关定义:

  1. struct tm {
  2. int tm_sec; /* 分钟后面的秒[0, 59] */
  3. int tm_min; /* 小时后面的分钟[0, 59] */
  4. int tm_hour; /* 距离凌晨00:00点的小时数[0, 23] */
  5. int tm_mday; /* 月中的某一天[1, 31] */
  6. int tm_mon; /* 某一个月份[0, 11] */
  7. int tm_year; /* 与1900相隔的年数 */
  8. int tm_wday; /* 离星期日的天数 [0, 6] */
  9. int tm_yday; /* 从一月开始的天数 [0, 365] */
  10. int tm_isdst; /* Daylight Saving Time flag */
  11. };
  12. char *asctime(const struct tm *_timeptr)

2、loc_time.h

  1. #define YEAR0 1900 /*第一年*/
  2. #define EPOCH_YR 1970 /* EPOCH = Jan 1 1970 00:00:00 */
  3. #define SECS_DAY (24L * 60L * 60L)
  4. #define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) % 400)))
  5. #define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
  6. #define FIRSTSUNDAY(timp) (((timp)->tm_yday - (timp)->tm_wday + 420) % 7)
  7. #define FIRSTDAYOF(timp) (((timp)->tm_wday - (timp)->tm_yday + 420) % 7)
  8. #define TIME_MAX ULONG_MAX
  9. #define ABB_LEN 3
  10.  
  11. extern const int _ytab[][];
  12. extern const char *_days[];
  13. extern const char *_months[];
  14.  
  15. void _tzset(void);
  16. unsigned _dstget(struct tm *timep);
  17.  
  18. extern long _timezone;
  19. extern long _dst_off;
  20. extern int _daylight;
  21. extern char *_tzname[];

3、asctime.c

  1. #include <string.h>
  2. #include <time.h>
  3. #include "loc_time.h"
  4.  
  5. #define DATE_STR "??? ??? ?? ??:??:?? ????\n" //时间格式
  6.  
  7. static char * two_digits(register char *pb, int i, int nospace)
  8. {
  9. //将两位数字转化为字符形式并存储在pb所指向的地址空间中
  10. *pb = (i / ) % + '';
  11. if (!nospace && *pb == '') *pb = ' ';
  12. pb++;
  13. *pb++ = (i % ) + '';
  14. return ++pb;
  15. }
  16.  
  17. static char * four_digits(register char *pb, int i)
  18. {
  19. //将四位数字转化为字符形式并存储在pb所指向的地址空间中
  20. i %= ;
  21. *pb++ = (i / ) + '';
  22. i %= ;
  23. *pb++ = (i / ) + '';
  24. i %= ;
  25. *pb++ = (i / ) + '';
  26. *pb++ = (i % ) + '';
  27. return ++pb;
  28. }
  29.  
  30. char *asctime(const struct tm *timeptr) //把timeptr指向的tm结构体中储存的时间转换为字符串格式返回。
  31. { // 格式为:Www Mmm dd hh:mm:ss yyyy。
  32. //其中Www为星期;Mmm为月份;dd为日;hh为时;mm为分;ss为秒;yyyy为年份。
  33. static char buf[];
  34. register char *pb = buf;
  35. register const char *ps;
  36. register int n;
  37.  
  38. strcpy(pb, DATE_STR); //对buf进行标准格式初始化: pb-> ??? ??? ?? ??:??:?? ????\n
  39. ps = _days[timeptr->tm_wday]; //extern const char *_days[];
  40. n = ABB_LEN; //#define ABB_LEN 3
  41. while(--n >= ) *pb++ = *ps++;
  42. pb++;
  43. ps = _months[timeptr->tm_mon];
  44. n = ABB_LEN;
  45. while(--n >= ) *pb++ = *ps++;
  46. pb++;
  47. pb = two_digits(
  48. two_digits(
  49. two_digits(two_digits(pb, timeptr->tm_mday, )
  50. , timeptr->tm_hour, )
  51. , timeptr->tm_min, )
  52. , timeptr->tm_sec, );
  53.  
  54. four_digits(pb, timeptr->tm_year + );
  55. return buf;
  56. }

minix中时间转换的实现(asctime.c)的更多相关文章

  1. ie浏览器中时间转换

    var begintime = $("#start").val(); var lastLoginTimeStart =new Date(begintime).getTime();/ ...

  2. vue中插值表达式中时间转换yyyy-MM-dd HH:mm:ss

    vue插值表达式中将时间转换两种方式:一.定义方法 <div id="app">当前实时时间:{{dateFormat(date)}}</div> //时间 ...

  3. php中时间转换函数

    date("Y-m-d H:i",$unixtime)  1.php中获得今天零点的时间戳 要获得零点的unix时间戳,可以使用 $todaytime=strtotime(“tod ...

  4. C++中时间转换

    所需头文件 #include <chrono> #include <time.h> auto now = std::chrono::system_clock::now(); s ...

  5. python中时间格式

    问题:通过MySQLdb查询datetime字段,然后通过浏览器显示出来,得到的格式是:         'Thu, 19 Feb 2009 16:00:07 GMT'   (http呈现出来的格式) ...

  6. js中的时间转换—毫秒转换成日期时间

    转自:http://www.javascript100.com/?p=181 前几天,在项目中遇到js时间增加问题,要将js毫秒时间转换成日期时间 var oldTime = (new Date(&q ...

  7. sql点滴42—mysql中的时间转换

    原文:sql点滴42-mysql中的时间转换 UNIX时间戳转换为日期用函数: FROM_UNIXTIME() select FROM_UNIXTIME(1156219870); 日期转换为UNIX时 ...

  8. JAVA中时间格式(SimpleDateFormat)和数字格式(DecimalFormat)转换详解(转)

    时间格式转换SimpleDateFormat: //定义日期的格式 SimpleDateFormat format =new SimpleDateFormat("yyMMdd"); ...

  9. python中时间的转换和使用datetime

    模块 一个完整大型的python程序是由模块和包的形式组织起来的,可见模块在python中的重要性.模块是一种组织型式,它许多有关联(关系)的代码组织放到单独的独立文件中.简单的说,可以把模块理解为一 ...

随机推荐

  1. 基于struts2的ajaxfileupload异步上传插件的使用

    服务器端采用struts2来处理文件上传. 所需环境: jquery.js ajaxfileupload.js struts2所依赖的jar包 及struts2-json-plugin-2.1.8.1 ...

  2. lrzsz离线安装方法

    lrzsz离线安装方法 到网上下载lrzsz安装包,这里以lrzsz-0.12.20.tar.gz为例 2 打开终端 cd 到安装包所在目录 tar zxvf lrzsz-0.12.20.tar.gz ...

  3. 文件传输协议(FTP,SFTP,SCP)(修改中)

    FTP(File Transfer Protocol):是TCP/IP网络上两台计算机传送文件的协议,FTP是在TCP/IP网络和INTERNET上最早使用的协议之一,它属于网络协议组的应用层.FTP ...

  4. Python爬虫-豆瓣电影 Top 250

    爬取的网页地址为:https://movie.douban.com/top250 打开网页后,可观察到:TOP250的电影被分成了10个页面来展示,每个页面有25个电影. 那么要爬取所有电影的信息,就 ...

  5. jsp新建项目

    1.在原有项目的基础上新建一个文件夹 在文件夹内新建一个jsp文件 取名 JSP容器处理JSP文件需要以下三个阶段:翻译——编译——执行 JSP的页面元素包括 静态内容-HTML静态文本 指令-以“& ...

  6. PHP 数组current和next用法

    1.current   当前数组 <?php $transport = array('foot', 'bike', 'car', 'plane'); $mode = current($trans ...

  7. MySQL自定义排序函数FIELD()

    MySQL可以通过field()函数自定义排序,格式:field(value,str1,str2,str3,str4),value与str1.str2.str3.str4比较,返回1.2.3.4,如遇 ...

  8. MySQL Study之--MySQL普通用户无法本地登陆

    MySQL Study之--MySQL普通用户无法本地登陆       在安装完毕MySQL后,我们通常加入拥有对应权限的普通用户用来訪问数据库.在使用用户本地登录数据库的时候,常常会出现怎么登录也无 ...

  9. C#------如何处理缺少对公共可见类型或成员的xml注释的警告

    出现警告的原因: 使用Swagger框架时 如图,只要加上注释就可以了 使用前: 使用后:

  10. Dubbo -- 系统学习 笔记 -- 示例 -- 泛化引用

    Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 泛化引用 泛接口调用方式主要用于客户端没有API接口及模型类元的情况,参数及返回值 ...