2013-03-25 13 views
7

Ho elenco dei numeri di settimana estratti dalle enormi file di log, che sono stati estratti utilizzando la sintassi:Data UNIX: come convertire il numero della settimana in un intervallo di date (lun-dom)?

$ date --date="Wed Mar 20 10:19:56 2012" +%W; 
12 

Voglio creare una semplice funzione bash che può convertire questi numeri alla settimana per un intervallo di date. Suppongo che la funzione dovrebbe accettare 2 argomenti: $ numero e $ l'anno, ad esempio:

$ week() { ......... } 
$ number=12; year=2012 
$ week $number $year 
"Mon Mar 19 2012" - "Sun Mar 25 2012" 

risposta

9

Con GNU date:

$ cat weekof.sh 
function weekof() 
{ 
    local week=$1 year=$2 
    local week_num_of_Jan_1 week_day_of_Jan_1 
    local first_Mon 
    local date_fmt="+%a %b %d %Y" 
    local mon sun 

    week_num_of_Jan_1=$(date -d $year-01-01 +%W) 
    week_day_of_Jan_1=$(date -d $year-01-01 +%u) 

    if ((week_num_of_Jan_1)); then 
     first_Mon=$year-01-01 
    else 
     first_Mon=$year-01-$((01 + (7 - week_day_of_Jan_1 + 1))) 
    fi 

    mon=$(date -d "$first_Mon +$((week - 1)) week" "$date_fmt") 
    sun=$(date -d "$first_Mon +$((week - 1)) week + 6 day" "$date_fmt") 
    echo "\"$mon\" - \"$sun\"" 
} 

weekof $1 $2 
$ bash weekof.sh 12 2012 
"Mon Mar 19 2012" - "Sun Mar 25 2012" 
$ bash weekof.sh 1 2018 
"Mon Jan 01 2018" - "Sun Jan 07 2018" 
$ 
+0

Perfetto !! Grazie mille. – hellish

0

Se qualcuno ne ha bisogno: ho trovato un modo ancora più breve (non so se più facile) :

function weekof() { 
     local year=$2 
     local week=`echo $1 | sed 's/^0*//'` # Fixes random bug 
     local dateFormat="+%a %b %d %Y" 
     # Offset is the day of week, so we can calculate back to monday 
     local offset="`date -d "$year/01/01 +$((week - 1)) week" "+%u"`" 
     echo -n "`date -d "$year/01/01 +$((week - 1)) week +$((1 - $offset)) day" "$dateFormat"`" # Monday 
     echo -n " - " 
     echo "`date -d "$year/01/01 +$((week - 1)) week +$((7 - $offset)) day" "$dateFormat"`" # Sunday } 

Prendo il primo giorno dell'anno e vado avanti n settimane per essere da qualche parte nella settimana giusta. Poi prendo il giorno della settimana e ritorno/inoltro per raggiungere lunedì e domenica.

+0

La tua produzione 'settimana del 12 2012'' dal 12 marzo al 2012 - dom 18 mar 2012 ». Dovrebbe essere "lun mar 19 2012 - dom 25 mar 2012". – pynexj

1

Se l'inizio di una settimana è Domenica, è possibile utilizzare questa versione di weekof:

function weekof() 
{ 
    local week=$1 year=$2 
    local week_num_of_Jan_1 week_day_of_Jan_1 
    local first_Sun 
    local date_fmt="+%Y-%m-%d" 
    local sun sat 

    week_num_of_Jan_1=$(date -d $year-01-01 +%U) 
    week_day_of_Jan_1=$(date -d $year-01-01 +%u) 

    if ((week_num_of_Jan_1)); then 
     first_Sun=$year-01-01 
    else 
     first_Sun=$year-01-$((01 + (7 - week_day_of_Jan_1))) 
    fi 

    sun=$(date -d "$first_Sun +$((week - 1)) week" "$date_fmt") 
    sat=$(date -d "$first_Sun +$((week - 1)) week + 6 day" "$date_fmt") 
    echo "$sun $sat" 
} 
1

Lunedi è il primo giorno della settimana, ISO week numbers:

function week2date() { 
    local year=$1 
    local week=$2 
    local dayofweek=$3 
    date -d "$year-01-01 +$(($week * 7 + 1 - $(date -d "$year-01-04" +%w) - 3)) days -2 days + $dayofweek days" +"%Y-%m-%d" 
} 

week2date 2017 35 1 
week2date 2017 35 7 

uscita:

2017-08-28 
2017-09-03 
+0

Questo ha già avuto risposta 4 anni fa. Quale valore aggiunto porta la tua risposta? –

+0

1. Si rivolge in modo esplicito ai numeri della settimana ISO 2. Utilizza un algoritmo descritto in Wikipedia 3. È più breve –

+0

OK, abbastanza giusto! –

Problemi correlati