+ -
当前位置:首页 → 问答吧 → 求助一代码,功能是将传递的时间戳转为特定格式的时间显示

求助一代码,功能是将传递的时间戳转为特定格式的时间显示

时间:2010-09-08

来源:互联网

本帖最后由 ixqbar 于 2010-09-08 12:37 编辑

不知道如何将char转换为time_t类型

需求是如何将传递的时间戳转换为特定格式(Year-month-day Hour:mi:s)显示
比如: ./showtime 1283917950 运行后显示为 2010-09-08 11:52:30


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>

int main(int argc,char *argv) {
    time_t newtime;
    time(&newtime); //想实现的是newtime是我传递的参数
    char szBuff[30];
    strftime(szBuff, sizeof(szBuff), "%Y:%m:%d %X", localtime(&newtime));
    printf("%s\n",szBuff);   
}

求高手帮忙

作者: ixqbar   发布时间: 2010-09-08

  1. void
  2. ngx_gmtime(time_t t, ngx_tm_t *tp)
  3. {
  4.     ngx_int_t   yday;
  5.     ngx_uint_t  n, sec, min, hour, mday, mon, year, wday, days, leap;

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

  7.     n = (ngx_uint_t) t;

  8.     days = n / 86400;

  9.     /* Jaunary 1, 1970 was Thursday */

  10.     wday = (4 + days) % 7;

  11.     n %= 86400;
  12.     hour = n / 3600;
  13.     n %= 3600;
  14.     min = n / 60;
  15.     sec = n % 60;

  16.     /*
  17.      * the algorithm based on Gauss' formula,
  18.      * see src/http/ngx_http_parse_time.c
  19.      */

  20.     /* days since March 1, 1 BC */
  21.     days = days - (31 + 28) + 719527;

  22.     /*
  23.      * The "days" should be adjusted to 1 only, however, some March 1st's go
  24.      * to previous year, so we adjust them to 2.  This causes also shift of the
  25.      * last Feburary days to next year, but we catch the case when "yday"
  26.      * becomes negative.
  27.      */

  28.     year = (days + 2) * 400 / (365 * 400 + 100 - 4 + 1);

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

  30.     if (yday < 0) {
  31.         leap = (year % 4 == 0) && (year % 100 || (year % 400 == 0));
  32.         yday = 365 + leap + yday;
  33.         year--;
  34.     }

  35.     /*
  36.      * The empirical formula that maps "yday" to month.
  37.      * There are at least 10 variants, some of them are:
  38.      *     mon = (yday + 31) * 15 / 459
  39.      *     mon = (yday + 31) * 17 / 520
  40.      *     mon = (yday + 31) * 20 / 612
  41.      */

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

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

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

  45.     if (yday >= 306) {

  46.         year++;
  47.         mon -= 10;

  48.         /*
  49.          * there is no "yday" in Win32 SYSTEMTIME
  50.          *
  51.          * yday -= 306;
  52.          */

  53.     } else {

  54.         mon += 2;

  55.         /*
  56.          * there is no "yday" in Win32 SYSTEMTIME
  57.          *
  58.          * yday += 31 + 28 + leap;
  59.          */
  60.     }

  61.     tp->ngx_tm_sec = (ngx_tm_sec_t) sec;
  62.     tp->ngx_tm_min = (ngx_tm_min_t) min;
  63.     tp->ngx_tm_hour = (ngx_tm_hour_t) hour;
  64.     tp->ngx_tm_mday = (ngx_tm_mday_t) mday;
  65.     tp->ngx_tm_mon = (ngx_tm_mon_t) mon;
  66.     tp->ngx_tm_year = (ngx_tm_year_t) year;
  67.     tp->ngx_tm_wday = (ngx_tm_wday_t) wday;
  68. }
复制代码

作者: lenky0401   发布时间: 2010-09-08

请从http://nginx.org/下载到nginx源码 在ngx_times.c源文件内找到这个以及相关函数 改改就可以了

作者: lenky0401   发布时间: 2010-09-08

先看看

作者: ixqbar   发布时间: 2010-09-08



QUOTE:
请从下载到nginx源码 在ngx_times.c源文件内找到这个以及相关函数 改改就可以了
lenky0401 发表于 2010-09-08 12:53




   已经找到思路了,谢谢

作者: ixqbar   发布时间: 2010-09-08

相关阅读 更多

热门下载

更多