root/libutil/date.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. now

   1 /*
   2  * Copyright (c) 2004 Tama Communications Corporation
   3  *
   4  * This file is part of GNU GLOBAL.
   5  *
   6  * This program is free software: you can redistribute it and/or modify
   7  * it under the terms of the GNU General Public License as published by
   8  * the Free Software Foundation, either version 3 of the License, or
   9  * (at your option) any later version.
  10  * 
  11  * This program is distributed in the hope that it will be useful,
  12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14  * GNU General Public License for more details.
  15  * 
  16  * You should have received a copy of the GNU General Public License
  17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18  */
  19 
  20 #ifdef HAVE_CONFIG_H
  21 #include <config.h>
  22 #endif
  23 #include <stdio.h>
  24 #ifdef HAVE_STRING_H
  25 #include <string.h>
  26 #else
  27 #include <strings.h>
  28 #endif
  29 #include <time.h>
  30 #include "die.h"
  31 #include "strlimcpy.h"
  32 #include "date.h"
  33 /**
  34  * now: current date and time
  35  *
  36  *      @return         date and time
  37  *
  38  * If function @NAME{strftime()} is available on your system, the format of the date
  39  * and time returned is @CODE{'\%a \%b \%d \%H:\%M:\%S \%Z \%Y'} using local time, otherwise
  40  * the @NAME{date} shell command is used. If there's an error with the date command version
  41  * of this function, the string returned will be 'unknown time'.
  42  *
  43  *      @note The returned string will be overwritten on the next call of this function.
  44  */
  45 const char *
  46 now(void)
  47 {
  48         static char buf[128];
  49 
  50 #ifdef HAVE_STRFTIME
  51         time_t tval;
  52 
  53         if (time(&tval) == -1)
  54                 die("cannot get current time.");
  55         (void)strftime(buf, sizeof(buf), "%a %b %d %H:%M:%S %Z %Y", localtime(&tval));
  56 #else
  57         FILE *ip;
  58 
  59         strlimcpy(buf, "unknown time", sizeof(buf));
  60         if ((ip = popen("date", "r")) != NULL) {
  61                 if (fgets(buf, sizeof(buf), ip))
  62                         buf[strlen(buf) - 1] = 0;
  63                 pclose(ip);
  64         }
  65 #endif
  66         return buf;
  67 }

/* [previous][next][first][last][top][bottom][index][help] */