nth day of the year?

  • Follow


hi,

how can I get the number of today in the year?

e.g. 24-nov-2005 = 328

-- 
-Gernot
int main(int argc, char** argv) {printf 
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}


0
Reply Gernot 11/24/2005 9:56:12 AM

Gernot Frisch wrote:
> hi,
> 
> how can I get the number of today in the year?
> 
> e.g. 24-nov-2005 = 328
> 

With GNU date...

$ date +%j -d"24-nov-2005"
328


Janis
0
Reply Janis 11/24/2005 10:07:00 AM


Janis Papanagnou wrote:
> Gernot Frisch wrote:
> 
>> hi,
>>
>> how can I get the number of today in the year?
>>
>> e.g. 24-nov-2005 = 328
>>
> 
> With GNU date...
> 
> $ date +%j -d"24-nov-2005"
> 328

Oops, we're in awk...

BEGIN{d="24-nov-2005"; "date +%j -d"d | getline day; print day}


Janis
0
Reply Janis 11/24/2005 10:10:22 AM

"Janis Papanagnou" <Janis_Papanagnou@hotmail.com> schrieb im 
Newsbeitrag news:dm43ie$cj6$2@online.de...
> Janis Papanagnou wrote:
>> Gernot Frisch wrote:
>>
>>> hi,
>>>
>>> how can I get the number of today in the year?
>>>
>>> e.g. 24-nov-2005 = 328
>>>
>>
>> With GNU date...
>>
>> $ date +%j -d"24-nov-2005"
>> 328
>
> Oops, we're in awk...
>
> BEGIN{d="24-nov-2005"; "date +%j -d"d | getline day; print day}

you're using "getline" here.. can't it be done in pure (g)awk?






0
Reply Gernot 11/24/2005 10:20:08 AM

In comp.lang.awk Gernot Frisch <Me@privacy.net>:
> hi,

> how can I get the number of today in the year?

> e.g. 24-nov-2005 = 328

$ date -d '24-nov-2005' +%j
328

No awk needed, using GNU date.

Good luck

-- 
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)
mail: echo zvpunry@urvzvat.qr | perl -pe 'y/a-z/n-za-m/'
#bofh excuse 77: Typo in the code
0
Reply Michael 11/24/2005 10:30:45 AM

Gernot Frisch wrote:
> "Janis Papanagnou" <Janis_Papanagnou@hotmail.com> schrieb im 
> Newsbeitrag news:dm43ie$cj6$2@online.de...
> 
>>Janis Papanagnou wrote:
>>
>>>Gernot Frisch wrote:
>>>
>>>
>>>>hi,
>>>>
>>>>how can I get the number of today in the year?
>>>>
>>>>e.g. 24-nov-2005 = 328
>>>>
>>>
>>>With GNU date...
>>>
>>>$ date +%j -d"24-nov-2005"
>>>328
>>
>>Oops, we're in awk...
>>
>>BEGIN{d="24-nov-2005"; "date +%j -d"d | getline day; print day}
> 
> 
> you're using "getline" here.. can't it be done in pure (g)awk?

There are the mktime() and stfrtime() functions in gawk. I never used the
functions myself, so I can't say. But let's try...

   BEGIN{d="2005 11 24 0 0 0"; print strftime("%j", mktime(d))}

Yes it works. Just reformat your date specification for input to mktime().

Janis
0
Reply Janis 11/24/2005 10:39:30 AM

In comp.lang.awk Michael Heiming <michael+USENET@www.heiming.de>:
> In comp.lang.awk Gernot Frisch <Me@privacy.net>:
>> hi,

>> how can I get the number of today in the year?

>> e.g. 24-nov-2005 = 328

> $ date -d '24-nov-2005' +%j
> 328

> No awk needed, using GNU date.

For the actual date, just:

 date +%j

-- 
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)
mail: echo zvpunry@urvzvat.qr | perl -pe 'y/a-z/n-za-m/'
#bofh excuse 380: Operators killed when huge stack of backup
tapes fell over.
0
Reply Michael 11/24/2005 11:51:38 AM

BEGIN{print strftime("%j", systime())}

Perfect!
Thank you.


0
Reply Gernot 11/24/2005 12:21:06 PM

On Thu, 24 Nov 2005 13:21:06 +0100, "Gernot Frisch" <Me@Privacy.net>
wrote:

>
>BEGIN{print strftime("%j", systime())}
>
>Perfect!
>Thank you.
>
  BEGIN{print strftime("%j")}
is enough.  If the second argument is omitted, systime() is assumed.

-- 
T.E.D. (tdavis@gearbox.maem.umr.edu)
0
Reply Ted 11/24/2005 7:37:55 PM

Ted Davis wrote:
> On Thu, 24 Nov 2005 13:21:06 +0100, "Gernot Frisch" <Me@Privacy.net>
> wrote:
>
> >
> >BEGIN{print strftime("%j", systime())}
> >
> >Perfect!
> >Thank you.
> >
>   BEGIN{print strftime("%j")}
> is enough.  If the second argument is omitted, systime() is assumed.
>
> --
> T.E.D. (tdavis@gearbox.maem.umr.edu)

Again, in GNU 
awk 'BEGIN{system( "date +%j");exit}'
Sashi

0
Reply Sashi 11/30/2005 9:13:27 PM

> 
> Again, in GNU 
> awk 'BEGIN{system( "date +%j");exit}'
> Sashi
> 

This works perfectly, but system() function is very slow in awk (fork 
new proces, exec program date, close program date, free its memory, 
transfer output through pipe) and I suggest to aviod using it if other 
way is possible. For a hundred of records it doeas not matter. For 
hundered thousands it does.
Marek
0
Reply MArek 1/3/2006 1:50:53 PM

In article <dpdve3$172d$1@ns.felk.cvut.cz>, MArek Simon  <simon@npkk.cz> wrote:
>> 
>> Again, in GNU 
>> awk 'BEGIN{system( "date +%j");exit}'
>> Sashi
>> 
>
>This works perfectly, but system() function is very slow in awk (fork 
>new proces, exec program date, close program date, free its memory, 
>transfer output through pipe) and I suggest to aviod using it if other 
>way is possible. For a hundred of records it doeas not matter. For 
>hundered thousands it does.
>Marek

Indeed.  And, why are we making this so hard on ourselves?
Since we are assuming GNU, hence (almost certainly) GAWK, just use
strftime().

BEGIN { print "This is day #",strftime("%j") }

0
Reply gazelle 1/3/2006 2:58:03 PM

On 2006-01-03, Kenny McCormack wrote:
> In article <dpdve3$172d$1@ns.felk.cvut.cz>, MArek Simon  <simon@npkk.cz> wrote:
>>> 
>>> Again, in GNU 
>>> awk 'BEGIN{system( "date +%j");exit}'
>>> Sashi
>>> 
>>
>>This works perfectly, but system() function is very slow in awk (fork 
>>new proces, exec program date, close program date, free its memory, 
>>transfer output through pipe) and I suggest to aviod using it if other 
>>way is possible. For a hundred of records it doeas not matter. For 
>>hundered thousands it does.
>>Marek
>
> Indeed.  And, why are we making this so hard on ourselves?
> Since we are assuming GNU, hence (almost certainly) GAWK, just use
> strftime().
>
> BEGIN { print "This is day #",strftime("%j") }

   The %j formatting option of date does not presume GNU; it's
   available in all versions of date.


-- 
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |          is released under the
   2005, Apress                 |     GNU General Public Licence
0
Reply Chris 1/3/2006 3:39:03 PM

In article <ndiq83-d6t.ln1@rogers.com>,
Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
>On 2006-01-03, Kenny McCormack wrote:
>> In article <dpdve3$172d$1@ns.felk.cvut.cz>, MArek Simon  <simon@npkk.cz> wrote:
>>>> 
>>>> Again, in GNU 
>>>> awk 'BEGIN{system( "date +%j");exit}'
>>>> Sashi
>>>> 
>>>
>>>This works perfectly, but system() function is very slow in awk (fork 
>>>new proces, exec program date, close program date, free its memory, 
>>>transfer output through pipe) and I suggest to aviod using it if other 
>>>way is possible. For a hundred of records it doeas not matter. For 
>>>hundered thousands it does.
>>>Marek
>>
>> Indeed.  And, why are we making this so hard on ourselves?
>> Since we are assuming GNU, hence (almost certainly) GAWK, just use
>> strftime().
>>
>> BEGIN { print "This is day #",strftime("%j") }
>
>   The %j formatting option of date does not presume GNU; it's
>   available in all versions of date.

Right - but we're not talking about date or other bits of shell/Unix
command trivia.

We're talking about AWK (if in doubt, please always check the name of the
newsgroup to which you are posting), and, to the best of my knowledge, GNU
AWK (GAWK) is the only version of AWK that has strftime() as a built-in
function.

HTH.  HAND.

0
Reply gazelle 1/3/2006 3:53:15 PM

On 2006-01-03, Kenny McCormack wrote:
> In article <ndiq83-d6t.ln1@rogers.com>,
> Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
>>On 2006-01-03, Kenny McCormack wrote:
>>> In article <dpdve3$172d$1@ns.felk.cvut.cz>, MArek Simon  <simon@npkk.cz> wrote:
>>>>> 
>>>>> Again, in GNU 
>>>>> awk 'BEGIN{system( "date +%j");exit}'
>>>>> Sashi
>>>>> 
>>>>
>>>>This works perfectly, but system() function is very slow in awk (fork 
>>>>new proces, exec program date, close program date, free its memory, 
>>>>transfer output through pipe) and I suggest to aviod using it if other 
>>>>way is possible. For a hundred of records it doeas not matter. For 
>>>>hundered thousands it does.
>>>>Marek
>>>
>>> Indeed.  And, why are we making this so hard on ourselves?
>>> Since we are assuming GNU, hence (almost certainly) GAWK, just use
>>> strftime().
>>>
>>> BEGIN { print "This is day #",strftime("%j") }
>>
>>   The %j formatting option of date does not presume GNU; it's
>>   available in all versions of date.
>
> Right - but we're not talking about date or other bits of shell/Unix
> command trivia.

   Look up, look waay up. Do you see: awk 'BEGIN{system( "date +%j");exit}'

> We're talking about AWK (if in doubt, please always check the name of the
> newsgroup to which you are posting),

   Good idea. See, it's comp.lang.awk, not comp.lang.gawk.

> and, to the best of my knowledge, GNU AWK (GAWK) is the only version
> of AWK that has strftime() as a built-in function.

   Exactly; which is why several people posted portable solutions
   using date.

-- 
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |          is released under the
   2005, Apress                 |     GNU General Public Licence
0
Reply Chris 1/3/2006 4:16:52 PM

In article <kkkq83-d6t.ln1@rogers.com>,
Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
....
>   Look up, look waay up. Do you see: awk 'BEGIN{system( "date +%j");exit}'
>
>> We're talking about AWK (if in doubt, please always check the name of the
>> newsgroup to which you are posting),
>
>   Good idea. See, it's comp.lang.awk, not comp.lang.gawk.

Boy, did you just step in it!  See, it is most definitely *not*
comp.unix.awk, so assuming Unix (or the availability of Unix commands such
as date) is certainly a lot more OT than is assuming GAWK.  Note that
anything based on "system("date")" will fail miserably under Microsoft
(unless Cygwin or something like that is installed).

Anyway:
	1) The poster to which I was responding had already mentioned GNU,
but didn't realize that making that stipulation made it unnecessary to
use system/date.
	2) Around here, in c.l.a., GAWK is pretty much the default
(unstated) assumption.  I know you probably disagree with this, but stick
around, and you might learn something.

>> and, to the best of my knowledge, GNU AWK (GAWK) is the only version
>> of AWK that has strftime() as a built-in function.
>
>   Exactly; which is why several people posted portable solutions
>   using date.

Portable?  Maybe (details for why "maybe" means "not", available upon
request).

Ugly?  Yes.

Inefficient?  Most definitely!

0
Reply gazelle 1/3/2006 4:51:55 PM

Gernot Frisch wrote:

> hi,
> 
> how can I get the number of today in the year?
> 
> e.g. 24-nov-2005 = 328
> 

function dayofyear(y,m,d) {
  return julian(y,m,d)-julian(y,1,1)+1
}

function julian (y,m,d,hr,min,sec,  A,B,C,D,ymd) {
#convert a calendar date & time to a Julian date
#Ref. Practical Astronomy With Your Calculator, 3rd Edition
#by Peter Duffett-Smith
#julian(-4712,1,1,12)==0; 4713 BC January 1, mid-day
#julian(1985,2,17,6)== 2446113.75000000
#julian(1990,0,0)==julian(1989,12,31)==2447891.5

  d = d + (hr + (min + sec / 60) / 60) / 24
  if (m < 1) m = 1
  if (m < 3) {
    y = y - 1
    m = m + 12
  }
  ymd = sprintf("%d%02d%02d", y, m, d)
  if (ymd >= 15821015) {
    A = int(y / 100)
    B = 2 - A + int(A / 4)
  } else {
    B = 0
  }
  if (y < 0) {
    C = int((365.25 * y) - 0.75)
  } else {
    C = int(365.25 * y)
  }
  D = int(30.6001 * (m + 1))
  return sprintf("%f", B + C + D + d + 1720994.5)
  
}

This script does not depend on gawk or unix features.

Hope this helps,
Alan Linton
0
Reply Alan 1/3/2006 8:47:23 PM


MArek Simon wrote:
>>
>> Again, in GNU awk 'BEGIN{system( "date +%j");exit}'
>> Sashi
>>
> 
> This works perfectly, but system() function is very slow in awk (fork 
> new proces, exec program date, close program date, free its memory, 
> transfer output through pipe) and I suggest to aviod using it if other 
> way is possible. For a hundred of records it doeas not matter. For 
> hundered thousands it does.
> Marek

But, since the system call is in the BEGIN block, it is only executed
once, not once for a hundred thousand records.  And since we're only
talking about the day of the year, don't worry about wrapping to the
next day somewhere in the middle of processing the input records.

$0.02
Bill Seivert

0
Reply Bill 1/4/2006 6:09:32 AM

On 2006-01-03, Kenny McCormack wrote:
> In article <kkkq83-d6t.ln1@rogers.com>,
> Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
> ...
>>   Look up, look waay up. Do you see: awk 'BEGIN{system( "date +%j");exit}'
>>
>>> We're talking about AWK (if in doubt, please always check the name of the
>>> newsgroup to which you are posting),
>>
>>   Good idea. See, it's comp.lang.awk, not comp.lang.gawk.
>
> Boy, did you just step in it!  See, it is most definitely *not*
> comp.unix.awk, so assuming Unix (or the availability of Unix commands such
> as date) is certainly a lot more OT than is assuming GAWK.  Note that
> anything based on "system("date")" will fail miserably under Microsoft
> (unless Cygwin or something like that is installed).

    You are quite write; I do tend to forget that awk is ported to
    non-*nix OSs. That doesn't make gawk the portable choice.

> Anyway:
> 	1) The poster to which I was responding had already mentioned GNU,
> but didn't realize that making that stipulation made it unnecessary to
> use system/date.

    Having GNU date in no way implies that gawk is installed. GNU date
    is often installed where gawk is not. GNU date is orders of
    magnitude more capable than the standard date command; gawk adds
    relatively little in comparison with awk.

> 	2) Around here, in c.l.a., GAWK is pretty much the default
> (unstated) assumption.

    Unstated because untrue.

> I know you probably disagree with this, but stick around, and you
> might learn something.

    From some posters in this group, I often do.

>>> and, to the best of my knowledge, GNU AWK (GAWK) is the only version
>>> of AWK that has strftime() as a built-in function.
>>
>>   Exactly; which is why several people posted portable solutions
>>   using date.
>
> Portable?  Maybe (details for why "maybe" means "not", available upon
> request).
>
> Ugly?  Yes.

    Maybe.


    Give that the OP seemed to have the date already and didn't need
    to derive the date, I am surprised that a pure awk solution wasn't
    presented sooner.

    The dayOfYear() function takes the date in ISO format (e.g.,
    2005-11-24), which can easily be derived from the 24-Nov-2005
    format the OP posted:

function dayOfYear(date) {
     m[2]  =  31;   m[3]  =  59;    m[4]  = 90
     m[5]  = 120;   m[6]  = 151;    m[7]  = 181
     m[8]  = 212;   m[9]  = 243;    m[10] = 273
     m[11] = 304;   m[12] = 334
     split( date, ymd, "-" )
     if ( ymd[2] > 2 )
        return m[ymd[2]+0] + ymd[3] + isLeapYear(ymd[1])
     else
        return m[ymd[2+0]] + ymd[3]
}

function isLeapYear(year) {
     if ( year %   4 != 0 ) return 0
     if ( year % 400 == 0 ) return 1
     if ( year % 100 == 0 ) return 0
     return 1
}


-- 
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |          is released under the
   2005, Apress                 |     GNU General Public Licence
0
Reply Chris 1/4/2006 4:05:16 PM

18 Replies
460 Views

(page loaded in 1.345 seconds)

Similiar Articles:


















7/25/2012 10:16:55 AM


Reply: