Any one-liner script to determine leap year?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
chcfrank
Posts: 10
Joined: 22 May 2008 20:24

Any one-liner script to determine leap year?

#1 Post by chcfrank » 25 May 2008 11:42

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

jeb
Expert
Posts: 1042
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#2 Post by jeb » 25 May 2008 15:10

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

chcfrank
Posts: 10
Joined: 22 May 2008 20:24

#3 Post by chcfrank » 26 May 2008 20:45

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

jeb
Expert
Posts: 1042
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#4 Post by jeb » 29 May 2008 13:42

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

chcfrank
Posts: 10
Joined: 22 May 2008 20:24

#5 Post by chcfrank » 29 May 2008 19:49

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

Post Reply