Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4823117
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: LINUX

2010-01-15 20:31:17

  kernel内面获得时间首先要包含linux/time.h 使用do_gettimeofday函数获得时间
struct timeval* tv
 
time = tv->tv_sec + tv->tv_sec/1000000
 
 
然后使用下面的my_gmtime(&time)
 
 


#define time_t unsigned int
  
struct tm
{
  int    tm_sec;
  int    tm_min;
  int    tm_hour;
  int    tm_mday;
  int    tm_mon;
  int    tm_year;
  int    tm_wday;
  int    tm_yday;
  int    tm_isdst;
};

#define YEAR0 1900
#define EPOCH_YR 1970
#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 2147483647L

int _daylight = 0; // Non-zero if daylight savings time is used

long _dstbias = 0; // Offset for Daylight Saving Time

long _timezone = 0; // Difference in seconds between GMT and local time

char *_tzname[2] = {"GMT", "GMT"}; // Standard/daylight savings time zone names


const char *_days[] =
{
  "Sunday", "Monday", "Tuesday", "Wednesday",
  "Thursday", "Friday", "Saturday"
};

const char *_days_abbrev[] =
{
  "Sun", "Mon", "Tue", "Wed",
  "Thu", "Fri", "Sat"
};

const char *_months[] =
{
  "January", "February", "March",
  "April", "May", "June",
  "July", "August", "September",
  "October", "November", "December"
};

const char *_months_abbrev[] =
{
  "Jan", "Feb", "Mar",
  "Apr", "May", "Jun",
  "Jul", "Aug", "Sep",
  "Oct", "Nov", "Dec"
};

const int _ytab[2][12] =
{
  {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

struct tm *my_gmtime_r(const time_t *timer, struct tm *tmbuf)
{
  time_t time = *timer;
  unsigned long dayclock, dayno;
  int year = EPOCH_YR;

  dayclock = (unsigned long) time % SECS_DAY;
  dayno = (unsigned long) time / SECS_DAY;

  tmbuf->tm_sec = dayclock % 60;
  tmbuf->tm_min = (dayclock % 3600) / 60;
  tmbuf->tm_hour = dayclock / 3600;
  tmbuf->tm_wday = (dayno + 4) % 7; // Day 0 was a thursday

  while (dayno >= (unsigned long) YEARSIZE(year))
  {
    dayno -= YEARSIZE(year);
    year++;
  }
  tmbuf->tm_year = year - YEAR0;
  tmbuf->tm_yday = dayno;
  tmbuf->tm_mon = 0;
  while (dayno >= (unsigned long) _ytab[LEAPYEAR(year)][tmbuf->tm_mon])
  {
    dayno -= _ytab[LEAPYEAR(year)][tmbuf->tm_mon];
    tmbuf->tm_mon++;
  }
  tmbuf->tm_mday = dayno + 1;
  tmbuf->tm_isdst = 0;

  return tmbuf;
}

struct tm *my_gmtime(const time_t *timer)
{
  struct tm *tmbuf = (struct tm *)kmalloc(sizeof(struct tm *), GFP_ATOMIC);
  return my_gmtime_r(timer, tmbuf);
}

 

也可以简单点


 


#define time_t unsigned int
  
struct tm
{
  int    tm_sec;
  int    tm_min;
  int    tm_hour;
  int    tm_mday;
  int    tm_mon;
  int    tm_year;
  int    tm_wday;
  int    tm_yday;
  int    tm_isdst;
};

#define YEAR0 1900
#define EPOCH_YR 1970
#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)


const int _ytab[2][12] =
{
  {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

struct tm *my_gmtime_r(const time_t *timer, struct tm *tmbuf)
{
  time_t time = *timer;
  unsigned long dayclock, dayno;
  int year = EPOCH_YR;

  dayclock = (unsigned long) time % SECS_DAY;
  dayno = (unsigned long) time / SECS_DAY;

  tmbuf->tm_sec = dayclock % 60;
  tmbuf->tm_min = (dayclock % 3600) / 60;
  tmbuf->tm_hour = dayclock / 3600;
  tmbuf->tm_wday = (dayno + 4) % 7; // Day 0 was a thursday

  while (dayno >= (unsigned long) YEARSIZE(year))
  {
    dayno -= YEARSIZE(year);
    year++;
  }
  tmbuf->tm_year = year - YEAR0;
  tmbuf->tm_yday = dayno;
  tmbuf->tm_mon = 0;
  while (dayno >= (unsigned long) _ytab[LEAPYEAR(year)][tmbuf->tm_mon])
  {
    dayno -= _ytab[LEAPYEAR(year)][tmbuf->tm_mon];
    tmbuf->tm_mon++;
  }
  tmbuf->tm_mday = dayno + 1;
  tmbuf->tm_isdst = 0;

  return tmbuf;
}

struct tm *my_gmtime(const time_t *timer)
{
  struct tm *tmbuf = (struct tm *)kmalloc(sizeof(struct tm *), GFP_ATOMIC);
  return my_gmtime_r(timer, tmbuf);
}


阅读(1255) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

ubuntuer2010-01-15 20:34:41

为了不申请一个空间可以 my_gmtime_r(&timer, &tmbuf); 这样调用