<commit_before>/*
 * Copyright 2013 INRIA.
 * Copyright 2014 <NAME> <<EMAIL>>
 *
 * This file is subject to the terms and conditions of the GNU Lesser
 * General Public License v2.1. See the file LICENSE in the top level
 * directory for more details.
 */

/**
 * @ingroup shell_commands
 * @{
 * @file
 * @brief   Shell command implementation for the peripheral RTC interface
 *
 * @author  <NAME> <<EMAIL>>
 * @author  <NAME> <<EMAIL>>
 * @}
 */

#ifdef FEATURE_PERIPH_RTC

#include <stdio.h>
#include <stdint.h>
#include <string.h>

#include "periph/rtc.h"

void _alarm_handler(void *arg)
{
    (void) arg;

    puts("The alarm rang");
}

static int dow(int year, int month, int day)
{
    /* calculate the day of week using Tøndering's algorithm */
    static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
    year -= month < 3;
    return (year + year/4 - year/100 + year/400 + t[month-1] + day) % 7;
}


static int _parse_time(char **argv, struct tm *time)
{
    short i1, i2, i3;

    if(sscanf(argv[0], "%6hd-%6hd-%6hd", &i1, &i2, &i3) != 3) {
        puts("could not parse date");
        return -1;
    }

    time->tm_year = i1 - 1900;
    time->tm_mon = i2 - 1;
    time->tm_mday = i3;
    time->tm_wday = dow(i1, i2, i3);

    if(sscanf(argv[1], "%6hd:%6hd:%6hd", &i1, &i2, &i3) != 3) {
        puts("could not parse time");
        return -1;
    }

    time->tm_hour = i1;
    time->tm_min = i2;
    time->tm_sec = i3;

    time->tm_isdst = -1; /* undefined */

    return 0;
}

static void _rtc_getalarm(void)
{
    struct tm now;
    if (rtc_get_alarm(&now) == 0) {
        /* cppcheck: see man 3 asctime: obsoleted by POSIX.1-2008 */
        /* cppcheck-suppress obsoleteFunctionsasctime */
        printf("%s", asctime(&now));
    }
    else {
        puts("rtc: error getting alarm");
    }
}

static void _rtc_setalarm(char **argv)
{
    struct tm now;

    if (_parse_time(argv, &now) == 0) {
        if (rtc_set_alarm(&now, _alarm_handler, NULL) == -1) {
            puts("rtc: error setting alarm");
        }
    }
}

static void _rtc_gettime(void)
{
    struct tm now;
    if (rtc_get_time(&now) == 0) {
        /* cppcheck: see man 3 asctime: obsoleted by POSIX.1-2008 */
        /* cppcheck-suppress obsoleteFunctionsasctime */
        printf("%s", asctime(&now));
    }
    else {
        puts("rtc: error getting time");
    }
}

static void _rtc_settime(char **argv)
{
    struct tm now;

    if (_parse_time(argv, &now) == 0) {
        if (rtc_set_time(&now) == -1) {
            puts("rtc: error setting time");
        }
    }
}

static void _rtc_usage(void)
{
    puts("usage: rtc <command> [arguments]");
    puts("commands:");
    puts("\tinit\t\tinitialize the interface");
    puts("\tpoweron\t\tpower the interface on");
    puts("\tpoweroff\tpower the interface off");
    puts("\tclearalarm\tdeactivate the current alarm");
    puts("\tgetalarm\tprint the currently alarm time");<commit_msg>sys/shell: use more common libc calls<commit_after>/*
 * Copyright 2013 INRIA.
 * Copyright 2014 <NAME> <<EMAIL>>
 *
 * This file is subject to the terms and conditions of the GNU Lesser
 * General Public License v2.1. See the file LICENSE in the top level
 * directory for more details.
 */

/**
 * @ingroup shell_commands
 * @{
 * @file
 * @brief   Shell command implementation for the peripheral RTC interface
 *
 * @author  <NAME> <oliver.hahm@inria.fr>
 * @author  <NAME> <ludwig.ortmann@fu-berlin.de>
 * @}
 */

#ifdef FEATURE_PERIPH_RTC

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>

#include "periph/rtc.h"

void _alarm_handler(void *arg)
{
    (void) arg;

    puts("The alarm rang");
}

static int dow(int year, int month, int day)
{
    /* calculate the day of week using Tøndering's algorithm */
    static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
    year -= month < 3;
    return (year + year/4 - year/100 + year/400 + t[month-1] + day) % 7;
}


static int _parse_time(char **argv, struct tm *time)
{
    short i1, i2, i3;
    char *end;

    i1 = strtol(argv[0], &end, 10);
    i2 = strtol(end + 1, &end, 10);
    i3 = strtol(end + 1, &end, 10);

    time->tm_year = i1 - 1900;
    time->tm_mon = i2 - 1;
    time->tm_mday = i3;
    time->tm_wday = dow(i1, i2, i3);

    i1 = strtol(argv[1], &end, 10);
    i2 = strtol(end + 1, &end, 10);
    i3 = strtol(end + 1, &end, 10);

    time->tm_hour = i1;
    time->tm_min = i2;
    time->tm_sec = i3;

    time->tm_isdst = -1; /* undefined */

    return 0;
}

static void _print_time(struct tm *time)
{
    printf("%04i-%02i-%02i %02i:%02i:%02i\n",
            time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
            time->tm_hour, time->tm_min, time->tm_sec
          );
}

static void _rtc_getalarm(void)
{
    struct tm t;
    if (rtc_get_alarm(&t) == 0) {
        _print_time(&t);
    }
    else {
        puts("rtc: error getting alarm");
    }
}

static void _rtc_setalarm(char **argv)
{
    struct tm now;

    if (_parse_time(argv, &now) == 0) {
        if (rtc_set_alarm(&now, _alarm_handler, NULL) == -1) {
            puts("rtc: error setting alarm");
        }
    }
}

static void _rtc_gettime(void)
{
    struct tm t;
    if (rtc_get_time(&t) == 0) {
        _print_time(&t);
    }
    else {
        puts("rtc: error getting time");
    }
}

static void _rtc_settime(char **argv)
{
    struct tm now;

    if (_parse_time(argv, &now) == 0) {
        if (rtc_set_time(&now) == -1) {
            puts("rtc: error setting time");
        }
    }
}

static void _rtc_usage(void)
{
    puts("usage: rtc <command> [arguments]");
    puts("commands:");
    puts("\tinit\t\tinitialize the interface");
    puts("\tpoweron\t\tpower the interface on");
    puts("\tpoweroff\tpower the interface off");
    puts("\tclearalarm\tdeactivate the current alarm");
    puts("\tgetalarm\tprint the currently alarm time");
{'fraction_non_alphanumeric': 0.15922263893624275, 'fraction_numerical': 0.03153767473576543, 'mean_word_length': 2.6149106592729514, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3598673', 'n_tokens_mistral': 2321, 'n_tokens_neox': 2216, 'n_words': 587}