linux c timeval 转化为标准时间,怎么将下面的结构转换为标准的时间?比如年月日时分秒...

1. gmt2local.c:

2. int32_t

3. gmt2local(time_t t)

4. {

5.     register int dt, dir;

6.     register struct tm *gmt, *loc;

7.     struct tm sgmt;

8.

9.     if (t == 0)

10.         t = time(NULL);

11.     gmt = &sgmt;

12.     *gmt = *gmtime(&t);

13.     loc = localtime(&t);

14.     dt = (loc->tm_hour - gmt->tm_hour) * 60 * 60 +

15.         (loc->tm_min - gmt->tm_min) * 60;

16.

17.     /*

18.      * If the year or julian day is different, we span 00:00 GMT

19.      * and must add or subtract a day. Check the year first to

20.      * avoid problems when the julian day wraps.

21.      */

22.     dir = loc->tm_year - gmt->tm_year;

23.     if (dir == 0)

24.         dir = loc->tm_yday - gmt->tm_yday;

25.     dt += dir * 24 * 60 * 60;

26.

27.     return (dt);

28. }

29.

30.

31. tcpdump.c

32. int tflag=1;

33.

34. cast 't'  //如果在参数中有指定

35.   --tflag;

36.   break;

37.

38. if (tflag > 0)

39.   thiszone = gmt2local(0);

40.

41. util.c

42. void

43. ts_print(register const struct timeval *tvp)

44. {

45.     register int s;

46.     struct tm *tm;

47.     time_t Time;

48.     static unsigned b_sec;

49.     static unsigned b_usec;

50.

51.     switch(tflag) {

52.     case 1: /* Default */

53.         s = (tvp->tv_sec + thiszone) % 86400;

54.         (void)printf("%02d:%02d:%02d.%06u ",

55.                  s / 3600, (s % 3600) / 60, s % 60,

56.                  (unsigned)tvp->tv_usec);

57.         break;

58.     case -1: /* Unix timeval style */

59.         (void)printf("%u.%06u ",

60.                  (unsigned)tvp->tv_sec,

61.                  (unsigned)tvp->tv_usec);

62.         break;

63.     case -2:

64.         if (b_sec == 0) {

65.             printf("000000 ");

66.         } else {

67.             int d_usec = tvp->tv_usec - b_usec;

68.             int d_sec = tvp->tv_sec - b_sec;

69.

70.             while (d_usec tv_sec;

79.         b_usec = tvp->tv_usec;

80.         break;

81.     case -3: /* Default + Date*/

82.         s = (tvp->tv_sec + thiszone) % 86400;

83.         Time = (tvp->tv_sec + thiszone) - s;

84.         tm = gmtime (&Time);

85.         if (!tm)

86.             printf("Date fail  ");

87.         else

88.             printf("%04d-%02d-%02d ",

89.                    tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);

90.         printf("%02d:%02d:%02d.%06u ",

91.                s / 3600, (s % 3600) / 60, s % 60, (unsigned)tvp->tv_usec);

92.         break;

93.     }

94. }

在C语言中,要在Linux环境下修改时间结构体并使用`settimeofday()`函数设置系统时间,你需要包含头文件`sys/time.h`,这个函数定义了`struct timeval`用于存储绝对时间,包括秒数和微秒数。以下是简单的步骤: ```c #include <stdio.h> #include <sys/time.h> #include <time.h> // 创建一个timeval结构体实例 struct timeval time_struct; struct timezone tz; int main() { // 获取当前时间 gettimeofday(&time_struct, &tz); // 设置新的时间(这里假设你想将时间设为某个特定的时间time_t new_time = time(NULL); // 获取当前系统时间 struct tm* new_tm = localtime(&new_time); // 修改timeval中的秒数和微妙数 time_struct.tv_sec = new_tm->tm_hour * 3600 + new_tm->tm_min * 60 + new_tm->tm_sec; // 将小时、分钟、秒转换为秒 time_struct.tv_usec = new_tm->tm_usec; // 保持微秒不变 // 调用settimeofday()函数设置时间 if (settimeofday(&time_struct, NULL) != -1) { printf("成功设置时间到: %04d-%02d-%02d %02d:%02d:%02d.%03ld\n", new_time / (3600 * 24), // 年份 new_time / (3600 % 24), // 月份 new_time / 60 % 60, // 日 new_time % 60 / 60, // 时 new_time % 60, // 分 new_tm->tm_usec, // 秒 new_tm->tm_usec / 1000); // 毫秒 } else { perror("Failed to set time"); } return 0; } ``` 在这个例子中,我们首先获取当前时间,然后创建一个新的`struct tm`表示新时间,并将其转换为`timeval`中的秒数。最后,调用`settimeofday()`函数设置时间和时区。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值