Skip to main content
integrate suggestion and information from other answer
Source Link
Bodo
  • 6.4k
  • 18
  • 30

You can save the date of every calculation in a variable and use it to calculate the next date. I use the default format in standard locale for storing the date because date cannot parse your specified format.

# start date d=$(LC_ALL=C date -d "1998-01-01 00:00") echo "# d=$d" # convert format date -d "$d" +"%d-%m-%Y %H:%M" for i in {1..1825}; do # add 6 hours d=$(LC_ALL=C date -d "$d +6 hours") # convert format date -d "$d" +"%d-%m-%Y %H:%M" done 

This prints

# d=Thu Jan 1 00:00:00 CET 1998 01-01-1998 00:00 01-01-1998 06:00 01-01-1998 12:00 01-01-1998 18:00 02-01-1998 00:00 02-01-1998 06:00 02-01-1998 12:00 02-01-1998 18:00 03-01-1998 00:00 03-01-1998 06:00 03-01-1998 12:00 03-01-1998 18:00 04-01-1998 00:00 04-01-1998 06:00 ... 

The results depend on your time zone and daylight saving time rules.

Your starting time specification may be ambiguous because it doesn't specify the time zone.

Edit based on Paul_Pedant's comment:

Make an outer loop for + $i days, and an inner loop hard-coded 00, 06, 12, 18. It runs a quarter as many date processes, and it does not care about DST variations.

# start date d=$(LC_ALL=C date -d "1998-01-01 00:00") echo "# d=$d" for i in {1..456}; do # convert (partial) format out=$(date -d "$d" +"%d-%m-%Y") for h in 00 06 12 18; do echo "$out $h:00" done # add 1 day d=$(LC_ALL=C date -d "$d +1 day") done 

Please specify in the question what result you expect when switching between normal time and daylight saving time occurs. (strictly printing 00:00, 06:00, 12:00, 18:00 or exactly 6 hours time difference when switching DST)

Edit based on glenn jackman's answer

Adding option -u to all date calls will make the script nearly the same as written by glenn jackman and result in strictly printing 00:00, 06:00, 12:00, 18:00 regardless of DST.

You can save the date of every calculation in a variable and use it to calculate the next date. I use the default format in standard locale for storing the date because date cannot parse your specified format.

# start date d=$(LC_ALL=C date -d "1998-01-01 00:00") echo "# d=$d" # convert format date -d "$d" +"%d-%m-%Y %H:%M" for i in {1..1825}; do # add 6 hours d=$(LC_ALL=C date -d "$d +6 hours") # convert format date -d "$d" +"%d-%m-%Y %H:%M" done 

This prints

# d=Thu Jan 1 00:00:00 CET 1998 01-01-1998 00:00 01-01-1998 06:00 01-01-1998 12:00 01-01-1998 18:00 02-01-1998 00:00 02-01-1998 06:00 02-01-1998 12:00 02-01-1998 18:00 03-01-1998 00:00 03-01-1998 06:00 03-01-1998 12:00 03-01-1998 18:00 04-01-1998 00:00 04-01-1998 06:00 ... 

The results depend on your time zone and daylight saving time rules.

Your starting time specification may be ambiguous because it doesn't specify the time zone.

You can save the date of every calculation in a variable and use it to calculate the next date. I use the default format in standard locale for storing the date because date cannot parse your specified format.

# start date d=$(LC_ALL=C date -d "1998-01-01 00:00") echo "# d=$d" # convert format date -d "$d" +"%d-%m-%Y %H:%M" for i in {1..1825}; do # add 6 hours d=$(LC_ALL=C date -d "$d +6 hours") # convert format date -d "$d" +"%d-%m-%Y %H:%M" done 

This prints

# d=Thu Jan 1 00:00:00 CET 1998 01-01-1998 00:00 01-01-1998 06:00 01-01-1998 12:00 01-01-1998 18:00 02-01-1998 00:00 02-01-1998 06:00 02-01-1998 12:00 02-01-1998 18:00 03-01-1998 00:00 03-01-1998 06:00 03-01-1998 12:00 03-01-1998 18:00 04-01-1998 00:00 04-01-1998 06:00 ... 

The results depend on your time zone and daylight saving time rules.

Your starting time specification may be ambiguous because it doesn't specify the time zone.

Edit based on Paul_Pedant's comment:

Make an outer loop for + $i days, and an inner loop hard-coded 00, 06, 12, 18. It runs a quarter as many date processes, and it does not care about DST variations.

# start date d=$(LC_ALL=C date -d "1998-01-01 00:00") echo "# d=$d" for i in {1..456}; do # convert (partial) format out=$(date -d "$d" +"%d-%m-%Y") for h in 00 06 12 18; do echo "$out $h:00" done # add 1 day d=$(LC_ALL=C date -d "$d +1 day") done 

Please specify in the question what result you expect when switching between normal time and daylight saving time occurs. (strictly printing 00:00, 06:00, 12:00, 18:00 or exactly 6 hours time difference when switching DST)

Edit based on glenn jackman's answer

Adding option -u to all date calls will make the script nearly the same as written by glenn jackman and result in strictly printing 00:00, 06:00, 12:00, 18:00 regardless of DST.

Source Link
Bodo
  • 6.4k
  • 18
  • 30

You can save the date of every calculation in a variable and use it to calculate the next date. I use the default format in standard locale for storing the date because date cannot parse your specified format.

# start date d=$(LC_ALL=C date -d "1998-01-01 00:00") echo "# d=$d" # convert format date -d "$d" +"%d-%m-%Y %H:%M" for i in {1..1825}; do # add 6 hours d=$(LC_ALL=C date -d "$d +6 hours") # convert format date -d "$d" +"%d-%m-%Y %H:%M" done 

This prints

# d=Thu Jan 1 00:00:00 CET 1998 01-01-1998 00:00 01-01-1998 06:00 01-01-1998 12:00 01-01-1998 18:00 02-01-1998 00:00 02-01-1998 06:00 02-01-1998 12:00 02-01-1998 18:00 03-01-1998 00:00 03-01-1998 06:00 03-01-1998 12:00 03-01-1998 18:00 04-01-1998 00:00 04-01-1998 06:00 ... 

The results depend on your time zone and daylight saving time rules.

Your starting time specification may be ambiguous because it doesn't specify the time zone.