Chinaunix首页 | 论坛 | 博客
  • 博客访问: 349377
  • 博文数量: 161
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 345
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-13 11:04
文章分类

全部博文(161)

文章存档

2015年(15)

2014年(144)

2013年(2)

我的朋友

分类: C/C++

2013-12-13 11:28:50


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <getopt.h>
  7. char *l_opt_arg;
  8. char* const short_options = "ht:";
  9. struct option long_options[] = { { "help", 0, NULL, 'h' }, { "time", 1, NULL,
  10.         't' }, { 0, 0, 0, 0 }, };
  11. void useage() {
  12.     printf("Usage:"
  13.         "\ttime_tool --help -h\n"
  14.         "\ttime_tool 12:00\n");
  15. }
  16. void output(char *Time) {
  17.     time_t t;
  18.     struct tm p_t;
  19.     char Tmp[200];
  20.     int i = 1;
  21.     time(&t);
  22.     localtime_r(&t, &p_t);
  23.     if (Time != NULL) {
  24.         sscanf(Time, "%d:%d", &p_t.tm_hour, &p_t.tm_min);
  25.     }
  26.     for (i = 1; i < 11; i++) {
  27.         p_t.tm_min += 1;
  28.         p_t.tm_min %= 60;
  29.         memset(Tmp, 0, sizeof(Tmp));
  30.         if (strftime(Tmp, sizeof(Tmp), "%H:%M", &p_t) == 0) {
  31.             fprintf(stderr, "strftime returned 0");
  32.             exit(EXIT_FAILURE);
  33.         }

  34.         if (i % 2 == 0) {
  35.             printf("-");
  36.         } else {
  37.             if (i != 1)
  38.                 printf(",");
  39.         }
  40.         printf("%s", Tmp);
  41.     }
  42.     printf("\n");
  43. }
  44. int main(int argc, char **argv) {
  45.     int c = 0;
  46.     while ((c = getopt_long(argc, argv, short_options, long_options, NULL))
  47.             != -1) {
  48.         switch (c) {
  49.         case 'h':
  50.             useage();
  51.             return 0;
  52.         case 't':
  53.             l_opt_arg = optarg;
  54.             output(l_opt_arg);
  55.             return 0;
  56.         }
  57.     }
  58.     if (c == -1) {
  59.         output(NULL);
  60.     }
  61.     return 0;
  62. }

阅读(1429) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:udhcp测试

给主人留下些什么吧!~~