LoginSignup
1
1

More than 5 years have passed since last update.

昨日〜9日前までの日付を空白区切りで返すサンプル

Last updated at Posted at 2018-01-06

ソース

datelist.c
#include <stdio.h>
#include <time.h>

int main(void) {
    time_t timer;
    struct tm *local;

    /* 現在時刻を取得 */
    time(&timer);

    int i;
    int max = 9;
    for(i=0; i<max;i++) {
        //1日分過去にする
        timer -= 24*60*60;
        /* GMTではなくローカルタイムに変換 */
        local = localtime(&timer);

        printf("%4d", local->tm_year + 1900);
        printf("%02d", local->tm_mon + 1);
        printf("%02d", local->tm_mday);
        // 最後の値には空白をつけたくない
        if(i < max-1) {
            printf(" ");
        }
    }

    return 0;
}

使い方

コンパイル
gcc datelist.c -o datelist
使い方
$ for i in `./datelist`;do ls access_log-$i.gz;done
access_log-20180105.gz
access_log-20180104.gz
access_log-20180103.gz
access_log-20180102.gz
access_log-20180101.gz
access_log-20171231.gz
access_log-20171230.gz
access_log-20171229.gz
access_log-20171228.gz
1
1
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1