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

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

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

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

2、loc_time.h

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

3、asctime.c

#include    <string.h>
#include <time.h>
#include "loc_time.h" #define DATE_STR "??? ??? ?? ??:??:?? ????\n" //时间格式 static char * two_digits(register char *pb, int i, int nospace)
{
//将两位数字转化为字符形式并存储在pb所指向的地址空间中
*pb = (i / ) % + '';
if (!nospace && *pb == '') *pb = ' ';
pb++;
*pb++ = (i % ) + '';
return ++pb;
} static char * four_digits(register char *pb, int i)
{
//将四位数字转化为字符形式并存储在pb所指向的地址空间中
i %= ;
*pb++ = (i / ) + '';
i %= ;
*pb++ = (i / ) + '';
i %= ;
*pb++ = (i / ) + '';
*pb++ = (i % ) + '';
return ++pb;
} char *asctime(const struct tm *timeptr) //把timeptr指向的tm结构体中储存的时间转换为字符串格式返回。
{ // 格式为:Www Mmm dd hh:mm:ss yyyy。
//其中Www为星期;Mmm为月份;dd为日;hh为时;mm为分;ss为秒;yyyy为年份。
static char buf[];
register char *pb = buf;
register const char *ps;
register int n; strcpy(pb, DATE_STR); //对buf进行标准格式初始化: pb-> ??? ??? ?? ??:??:?? ????\n
ps = _days[timeptr->tm_wday]; //extern const char *_days[];
n = ABB_LEN; //#define ABB_LEN 3
while(--n >= ) *pb++ = *ps++;
pb++;
ps = _months[timeptr->tm_mon];
n = ABB_LEN;
while(--n >= ) *pb++ = *ps++;
pb++;
pb = two_digits(
two_digits(
two_digits(two_digits(pb, timeptr->tm_mday, )
, timeptr->tm_hour, )
, timeptr->tm_min, )
, timeptr->tm_sec, ); four_digits(pb, timeptr->tm_year + );
return buf;
}

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. MongoDB 之 幽灵操作避免

    进行静态加载数据到集合的过程中可能会出现. 假设建立一个任务(Job):在MongoDB中进行千条更新操作,开始后迅速终止任务,终止所有更新操作,但依然发现新的更新任务在不断出现,即使任务已经停止. ...

  2. C语言的结构体

    举例,一个结构体的定义如下: typedef struct _foo { ]; int age; int sex; } foo; 对齐 如果直接对上面的结构体作sizeof()运算: printf( ...

  3. centos 中文乱码解决途径

    在使用CentOS系统时,安装的时候可能你会遇到英文的CentOS系统,在这中情况下安装CentOS系统时是默认安装(即英文).安装完毕后,出现的各种中文乱码.那么,我们如何解决这种问题呢. 一.Ce ...

  4. 第六种方式,python使用cached_property缓存装饰器和自定义cached_class_property装饰器,动态添加类属性(三),selnium webdriver类无限实例化控制成单浏览器。

    使用 from lazy_object_proxy.utils import cached_property,使用这个装饰器. 由于官方的行数比较少,所以可以直接复制出来用自己的. class cac ...

  5. 8 -- 深入使用Spring -- 2...1 搜索Bean类

    8.2.1 搜索Bean类 既然不再使用Spring配置文件来配置任何Bean实例,那么只能希望Spring会自动搜索某些路径下的Java类,并将这些Java类注册成Bean实例. tips:Rail ...

  6. MySQL---insert into select from

    INSERT INTO perf_week(node_id,perf_time,pm25,pm10,temp,humi) SELECT node_id,'2016-12-22 11:55:00' AS ...

  7. 车牌、手机、身份证、等敏感信息 屏蔽 替换 、中文转unicode编码 函数

    应工作要求,需要对展示的内容进行敏感信息替换.琢磨的一些时间,编写的函数匹配率还是比较高的. 顺便说下思路,使用的是正则匹配替换和字符串替换.函数可以再改进. 先把需要匹配的内容写好相应的正则,然后进 ...

  8. PHP如何获取本周周二的日期?

    在计算某个特定周几的时候,需要对当前时间做一个转换和比对,比如,如何求出本周周二的时间日期. 代码如下: <?php // 如何计算本周的星期二 $time=time();//时间 $now=d ...

  9. error:undefined reference to 'net_message_processor::net_message_processor()'

    net_message_processor是我自己定义的一个类,文件名称分别是net_message_processor.h  & net_message_processor.cpp 和CCD ...

  10. 递归的几个demo

    /** * Created by root * Description : 递归函数 */ object RecursionTest { def main(args: Array[String]): ...