Nginx 中ngx_gmtime函数小析

解析Nginx中ngx_gmtime函数的高斯算法原理
本文深入剖析了Nginx源码中的ngx_gmtime函数,详细解释了其背后的高斯算法原理,通过计算时间戳转换为日期时间,包括年份、月份、日期等关键信息的获取过程,以及特殊日期如1970年1月1日的处理方式。文章还提供了计算方法和公式验证,帮助读者理解这一核心函数的工作机制。

Nginx 中ngx_gmtime函数小析

 

 

最近开始看nginx原码,从main()函数开始,看到ngx_gmtime()函数时,发现有些地方看不明白.

 

void

ngx_gmtime(time_t t, ngx_tm_t *tp)

{

    ngx_int_t   yday;

    ngx_uint_t  n, sec, min, hour, mday, mon, year, wday, days, leap;

 

    /* the calculation is valid for positive time_t only */

 

    n = (ngx_uint_t) t;

 

    days = n / 86400;

 

    /* Jaunary 1, 1970 was Thursday */

 

    wday = (4 + days) % 7;

 

    n %= 86400;

    hour = n / 3600;

    n %= 3600;

    min = n / 60;

    sec = n % 60;

  …..  (这里处理 1970.1.1后的当时秒数的days ,wday  hour  min  sec 数据)

    /*

     * the algorithm based on Gauss' formula,

     * see src/http/ngx_http_parse_time.c

     */

 

    /* days since March 1, 1 BC */

days = days - (31 + 28) + 719527;  (这里比较疑惑  719527 是怎么来的?) 经过查找资料才明白 719527 是从公元前1年3月1日到1970年3月1日的天数. 不信的朋友可以算算.

1970*365 + 1970/4(被4整除的润年的天数)- 1970/100(扣掉被100整除的天数)+1970/400(能被400整除的润年的天数) =719527(天)

 

//(365 * 400 + 100 - 4 + 1) 为400年总的天数

year = (days + 2) * 400 / (365 * 400 + 100 - 4 + 1);  //计算年份

 

    yday = days - (365 * year + year / 4 - year / 100 + year / 400);

 

    if (yday < 0) {

        leap = (year % 4 == 0) && (year % 100 || (year % 400 == 0));

        yday = 365 + leap + yday;

        year--;

    }

在本算法中采用了高斯算法, 从3月1日开始算 这样就省去了2月份是28天还是29天的麻烦. 有问题的就是第一个月, 把1,2月放到上一年的末尾,改变后的月份就变成如下形式

月份

月份

天数

间隔

1

3

30

30

2

4

31

61

3

5

30

91

4

6

31

122

5

7

30

152

6

8

31

183

7

9

31

214

8

10

30

244

9

11

31

275

10

12

30

305

11

1

31

336

12

2

31

367

 

  mon = (yday + 31) * 10 / 306;

 

    /* the Gauss' formula that evaluates days before the month */

 

    mday = yday - (367 * mon / 12 - 30) + 1;

 

至此  306 , 367这些数字都清楚它的由来了.

 

 

参考资料: http://hi.baidu.com/jiazheng2222/blog/item/13ed39cd55f20e0601e92817.html

 

 

转载于:https://www.cnblogs.com/cdamo/archive/2011/11/30/2269053.html

要在 Nginx 中安装 ngx_http_mp4_module 模块,需要按照以下步骤进行操作: 1. 确认 Nginx 是否支持 ngx_http_mp4_module 模块 首先需要确认 Nginx 是否支持 ngx_http_mp4_module 模块,可以使用以下命令查看 Nginx 是否已经编译了该模块: ``` nginx -V 2>&1 | grep -o with-http_mp4_module ``` 如果输出结果为 with-http_mp4_module,则说明 Nginx 已经编译了 ngx_http_mp4_module 模块;如果输出结果为空,则说明 Nginx 没有编译该模块。 2. 下载 ngx_http_mp4_module 模块 如果 Nginx 没有编译 ngx_http_mp4_module 模块,需要下载该模块并添加到 Nginx 中。可以从 Github 上下载该模块,链接为:https://github.com/kaltura/nginx-vod-module。 可以使用以下命令将 ngx_http_mp4_module 模块下载到 /opt 目录下: ``` cd /opt git clone https://github.com/kaltura/nginx-vod-module.git ``` 3. 编译 Nginx 并添加 ngx_http_mp4_module 模块 在编译 Nginx 时需要添加 --add-module=/opt/nginx-vod-module 参数来指定 ngx_http_mp4_module 模块所在的目录,具体命令如下: ``` ./configure --prefix=/usr/local/nginx --add-module=/opt/nginx-vod-module make make install ``` 4. 配置 NginxNginx 的配置文件中添加以下内容,即可使用 ngx_http_mp4_module 模块: ``` location /video/ { mp4; mp4_buffer_size 1m; mp4_max_buffer_size 5m; } ``` 其中,/video/ 是视频文件所在的目录。mp4 是 ngx_http_mp4_module 模块提供的指令,表示该目录下的文件都是 MP4 格式的视频文件。 mp4_buffer_size 和 mp4_max_buffer_size 是 ngx_http_mp4_module 模块提供的两个参数,用于控制视频文件的缓存大小。 5. 重启 Nginx 完成以上步骤后,需要重启 Nginx 使配置生效: ``` nginx -s reload ``` 至此,ngx_http_mp4_module 模块安装完成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值