Page 1 of 1

Generate date using DFSORT

PostPosted: Mon Dec 21, 2015 10:37 pm
by puffes
Hi,
I wonder if it's possible to generate all dates for one year using DFSORT?
So for year 2016 I want to generate 366 records on a sequential file starting with value 2016-01-01, 2016-01-02 and so on

Regards,

Mikael

Re: Generate date

PostPosted: Tue Dec 22, 2015 1:23 am
by BillyBoyo
Yes. Look at REPEAT in OUTFIL and the date functions, where there are a number of ways to do it. You can add a sequence number to a base date, or PUSH the date and get the next day...

Re: Generate date

PostPosted: Tue Dec 22, 2015 4:32 pm
by puffes
Hi again,
I've tried but I don't managed to get it right.

The file should contain:
Pos 1-6 Date in format YYMMDD
Pos 7-9 Day of year (001-366)
Pos 10-11 Week (01-52)
Pos 12 Day of week (1-7)

So the can look like this:
160101001536
160102002537
160103003531
160104004012
160105005013
and so on

How do I write it using Repeat other functions?

Regards

Mikael

Re: Generate date

PostPosted: Tue Dec 22, 2015 5:39 pm
by enrico-sorichetti
Pos 10-11 Week (01-52)


do the application designers know about the quirks of the week number ?

Re: Generate date

PostPosted: Tue Dec 22, 2015 10:21 pm
by prino
enrico-sorichetti wrote:
Pos 10-11 Week (01-52)


do the application designers know about the quirks of the week number ?

I guess that question is meant to be rhetorical. :mrgreen: :mrgreen: :mrgreen:

Re: Generate date

PostPosted: Tue Dec 22, 2015 10:38 pm
by enrico-sorichetti
Naturally ;)

I have been repeating the concept ad nauseam ...
people asking on forums just care about the lowly technicalities
and completely forget about proper business concerns.

Re: Generate date

PostPosted: Tue Dec 22, 2015 11:12 pm
by enrico-sorichetti
I just reread the whole topic and ...

as usual the code and the data do not match the comments
the comment talk about week number from 1 to 52,
but the data has the right week
16/01/01 is week number 53
and the wrong week day

and anyway another column is needed for the year

the comment for poor analysis still stands ( thanks &deity )

the algorithm is still wrong ...

for 16/01/01 it reports week 53 day 6
but
for 16/01/03 it reports week 53 day 1

a good analysis implies ( for congruency ) monotonicity of data

I suggest to number with 1 staring from monday

so that week 53 of 2015 will span into 2016 with the proper monotone days of the week

for 16/01/01 year 2015 week 53 day 5
for 16/01/03 year 2015 week 53 day 7

Re: Generate date

PostPosted: Tue Dec 22, 2015 11:29 pm
by enrico-sorichetti
very good mode on
here a rexx snippet to do the calculations
tested and working on my pc using open object rexx

#! /usr/bin/rexx

/* REXX
      y         year
      d         day
      w       week

      s         date sorted   yyyymmdd
      b         date base   ( as per rexx definition 0 = monday/00010101 )

      fwky      procedure,
            returns the base date of the monday of the first week of a year
            the week containin the thursday

      iso1      procedure,
            format the iso date appropriately

*/

parse arg s k
if k = "" then k = 1

f = date("b",right(s,4,"0") || "0101", "S")
t = date("b",right(s+k,4,"0") || "0101", "S" ) - 1

do   b  = f to t
   s = date("S",b,"B")
   i = date2iso(s)
   if   s \=  iso2date(i) then do
         say "error for" b s i
         exit
   end
   if   k = 1 then ,
         say s b i
end

exit

date2iso : procedure
   parse arg s
   b = date("B", s, "S")
   y = left(s, 4 )

   /* the easy one first */
   if   b >= fwky(y+1 ) then ,
       return iso1(y+1, 1, b // 7 + 1 )

      if   b >= fwky(y ) then do
         w = b % 7 - fwky(y ) % 7 + 1
         return iso1(y, w, b // 7 + 1 )
      end

      w = b % 7 - fwky(y-1 ) % 7 + 1
      return iso1(y-1, w, b // 7 + 1 )

iso1: procedure
      parse arg y, w, d
      return right(y, 4, "0" ) || "W" || right(w, 2, "0" ) || d

iso2date:procedure
      parse arg i
      parse var i with 1 y 5 . 6 w 8 d
      b = fwky(y ) + (w - 1 )*7 + d - 1
      return date("S", b, "B")

fwky: procedure
      /* base date of the first week of the year */
      parse arg y
      w = date("b", right(y, 4, "0" ) || "01" || "01", "s" )
      d = w // 7 + 1
      if   d <= 4 then ,
       return w + 1 - d
      else ,
       return w + 1 - d + 7