[pisa-src] Re: r2944 - in trunk: Makefile.am pairing/README pairing/util pairing/util/pisa-pairing-date.c

  • From: Diego Biurrun <diego@xxxxxxxxxx>
  • To: pisa-src@xxxxxxxxxxxxx
  • Date: Thu, 22 Mar 2012 17:09:50 +0100

On Thu, Mar 22, 2012 at 02:02:18PM +0100, Christoph Viethen wrote:
> Author: viethen
> Date: Thu Mar 22 14:02:15 2012
> New Revision: 2944
> 
> Log:
> Add a little utility program needed within the shell scripts for the
> webif GUI.
> 
> --- /dev/null 00:00:00 1970   (empty, because file is newly added)
> +++ trunk/pairing/util/pisa-pairing-date.c    Thu Mar 22 14:02:15 2012        
> (r2944)
> @@ -0,0 +1,222 @@
> +/**
> + * @file
> + * This program will verify a date string of the format yyyy-mm-dd for
> + * validity. It will indicate success or failure by an appropriate result
> + * code.
> + *
> + * If the given date is valid, and if an optional offset value is provided,
> + * this offset will be interpreted as a number of months. The date will be
> + * moved into the future by this indicated number of months. The "day" of
> + * the date will then be set to correctly indicate the last day of the
> + * respective calender month. The offset value must be between 0 and 99.
> + *
> + * A date thus modified will be output to stdout, in the same yyyy-mm-dd
> + * format.
> + *
> + * Examples: 2012-03-31, with offset 6, will yield 2012-09-30.
> + *           2012-02-29, with offset 12, will yield 2013-02-28.
> + *           2011-01-14, with offset 6, will yield 2011-07-31.
> + *           2012-06-03, with offset 0, will yield 2012-06-30.
> + *
> + * @brief verify and increment date strings in yyyy-mm-dd format.
> + * @author Christoph Viethen <christoph.viethen@xxxxxxxxxxxxxx>

Come on, copyright header please..

> + */
> +#include <inttypes.h>
> +#include <stdbool.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#define __USE_MISC
> +#include <time.h>

I think __USE_MISC is a glibc-internal thing, like all those double
underscore definitions, _BSD_SOURCE should do the trick.

> +static void print_patched_date(char *input_date_string, char *offset_string)
> +{
> +
> +    /* add the offset plus one more month, yielding the month following
> +     * the actual target ... */
> +    month += (offset + 1);

pointless parentheses

> +    if (month > 12) {
> +        year += (month - 1) / 12;
> +        month = ((month - 1) % 12) + 1;
> +    }
> +
> +    /* ... and express the first day of that following month */
> +    memset(&first_of_following_month, 0, sizeof(struct tm));

You can replace this memset by a zero initialization.  Also sizeof(type)
is brittle, better use sizeof(*ptr).

> +    /* Calculate the timestamp of that day, 00:00:00 GMT */
> +    timestamp = timegm(&first_of_following_month);

timegm appears not to exist on OpenWrt...

> +    printf("%04d-%02d-%02d\n", last_of_target_month.tm_year + 1900, 
> last_of_target_month.tm_mon + 1, last_of_target_month.tm_mday);

Please shorten this long line.

> +int main(int argc, char *argv[])
> +{
> +    int exit_status = EXIT_FAILURE;
> +
> +    if (argc == 2 || argc == 3) {
> +        if (verify_date_format(argv[1]) == true &&
> +            verify_date_semantics(argv[1]) == true) {
> +            exit_status = EXIT_SUCCESS;
> +
> +            if (argc == 3) {
> +                if (verify_offset(argv[2]) == true) {
> +                    print_patched_date(argv[1], argv[2]);
> +                } else {
> +                    exit_status = EXIT_FAILURE;
> +                }
> +            }
> +        }
> +    }
> +
> +    exit(exit_status);

Returning from main is IMHO more natural than exiting.

Diego
-- 
This is the pisa developer mailing list. Please also subscribe to the main pisa 
list at:
//www.freelists.org/list/pisa

Other related posts: