Page 1 of 1

Any one-liner script to determine leap year?

Posted: 25 May 2008 11:42
by chcfrank
I can determine whether it is leap year in couple of lines of batch script(witout using &) and most other scripting language support one-liner equation to determine whether it is leap year. But I cannot figure out how to do this in dos batch script since DOS does not support operator like (a == 1) to be true or false.
However, I still think it is doable with set /a leapyear=" ... " type of equation with operator like ^ & |. but my head is not clear enough to figure that out.

-Frank

Posted: 25 May 2008 15:10
by jeb
Hi Frank,

in a batchfile this is a solution for the years from 1901-2099

Code: Select all

set /a leapyear=! (%year% %% 4 )


obviously :wink: this is the full solution

Code: Select all

set /a leapyear=((!(%year%%%400))^|!(!(%year%%%100)))^&!(%year%%%4)


jeb

Posted: 26 May 2008 20:45
by chcfrank
jeb,

Thanks! I know it is doable! Again, how do I interpret '^|' and '^&'. I do not seem to find anything reference about these operator(most stuff about DOS scripts I can find by 'set /?', 'for /?', 'if /?', 'call /?', ...)
And this equation will fail if I put <space> between ^ and | or ^ and &.

-Frank

jeb wrote:Hi Frank,

in a batchfile this is a solution for the years from 1901-2099

Code: Select all

set /a leapyear=! (%year% %% 4 )


obviously :wink: this is the full solution

Code: Select all

set /a leapyear=((!(%year%%%400))^|!(!(%year%%%100)))^&!(%year%%%4)


jeb

Posted: 29 May 2008 13:42
by jeb
Hi Frank,

the 'set' command supports '&' "bitwise and", "| bitwise or"
but the & is also the delimeter for the next command on one line.

like

Code: Select all

set /a var=1 & echo hello


same for the pipe '|'

therefore you need to escape them with '^'

jeb

Posted: 29 May 2008 19:49
by chcfrank
Hi, Jeb,

Thanks, I thought ^ is 'bitwise exclusive or'. Never thought about it is escape char.

-Frank

jeb wrote:Hi Frank,

the 'set' command supports '&' "bitwise and", "| bitwise or"
but the & is also the delimeter for the next command on one line.

like

Code: Select all

set /a var=1 & echo hello


same for the pipe '|'

therefore you need to escape them with '^'

jeb