The symmetry of months
JavaScript (ES6), 55 bytes
Saved 6 bytes thanks to @Neil
Takes input in currying syntax (year)(month)
. Returns false
for asymmetric, true
for centrally symmetric and 0
for completely symmetric.
y=>m=>(n=(g=_=>new Date(y,m--,7).getDay())()+g())&&n==7
Try it online!
How?
We define the function g() which returns the weekday of yyyy/mm/01, as an integer between 0 = Monday and 6 = Sunday.
g = _ => new Date(y, m--, 7).getDay()
Because getDay() natively returns 0 = Sunday to 6 = Saturday, we shift the result to the expected range by querying for the 7th day instead.
Then we define:
n = g() + g()
Because the constructor of Date expects a 0-indexed month and because g() decrements m after passing it to Date, we actually first compute the weekday of the first day of the next month and then add that of the current one.
Completely symmetric months
Completely symmetric months start with a Monday and are followed by a month which also starts with a Monday. This is only possible for February of a non-leap year.
- Feb -------------- - Mar --------------
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
-------------------- --------------------
01 02 03 04 05 06 07 01 02 03 04 05 06 07
08 09 10 11 12 13 14 08 09 10 11 12 13 14
15 16 17 18 19 20 21 15 16 17 18 19 20 21
22 23 24 25 26 27 28 22 23 24 25 26 27 28
29 30 31
This leads to n = 0.
Centrally symmetric months
Centrally symmetric months are months for which the sum of the weekday of their first day and that of the next month is 7.
- M ---------------- - M+1 --------------
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
-------------------- --------------------
0 1 [2] 3 4 5 6 0 1 2 3 4 [5] 6
-------------------- --------------------
01 02 03 04 05 01 02
06 07 08 09 10 11 12 03 04 05 06 07 07 09
13 14 15 16 17 18 19 ...
20 21 22 23 24 25 26
27 28 29 30 31
Hence the second test: n == 7.
No built-in, 93 bytes
Uses Zeller's congruence. Same I/O format as the other version.
y=>m=>(n=(g=_=>(Y=y,((m+(m++>2||Y--&&13))*2.6|0)+Y+(Y>>2)-6*~(Y/=100)+(Y>>2))%7)()+g())&&n==7
Try it online!
T-SQL, 213 bytes (strict I/O rules)
SET DATEFIRST 1SELECT CASE WHEN a+b<>8THEN'a'WHEN a=1THEN''ELSE'centrally 'END+'symetric'FROM(SELECT DATEPART(DW,f)a,DATEPART(DW,DATEADD(M,1,f)-1)b FROM (SELECT CONVERT(DATETIME,REPLACE(s,'.','')+'01')f FROM t)y)x
The above query considers the strict input/output formatting rules.
The input is taken from the column s
of a table named t
:
CREATE TABLE t (s CHAR(7))
INSERT INTO t VALUES ('2018.04'),('2018.03'),('2010.02'),('1996.02')
Ungolfed:
SET DATEFIRST 1
SELECT *, CASE WHEN a+b<>8 THEN 'a' WHEN a=1 AND b=7 THEN '' ELSE 'centrally ' END+'symetric'
FROM (
SELECT *,DATEPART(WEEKDAY,f) a,
DATEPART(WEEKDAY,DATEADD(MONTH,1,f)-1) b
FROM (SELECT *,CONVERT(DATETIME,REPLACE(s,'.','')+'01')f FROM t)y
) x
SQLFiddle 1
T-SQL, 128 bytes (permissive I/O rules)
SET DATEFIRST 1SELECT CASE WHEN a+b<>8THEN 1WHEN a=1THEN\END FROM(SELECT DATEPART(DW,d)a,DATEPART(DW,DATEADD(M,1,d)-1)b FROM t)x
If the format of the input and of the output can be changed, I would choose to input the first day of the month, in a datetime
column named d
:
CREATE TABLE t (d DATETIME)
INSERT INTO t VALUES ('20180401'),('20180301'),('20100201'),('19960201')
The output would be 1 for asymetric, 0 for symetric, NULL for centrally symetric.
If we can run it on a server (or with a login) configured for BRITISH language, we can remove SET DATEFIRST 1
saving 15 more bytes.
SQLFiddle 2
Haskell, 170 bytes
import Data.Time.Calendar
import Data.Time.Calendar.WeekDate
a%b=((\(_,_,a)->a).toWeekDate.fromGregorian a b$1)!gregorianMonthLength a b
1!28=2
4!29=1
7!30=1
3!31=1
_!_=0
Returns 2 for centrally symmetric, 1 for symmetric and 0 for asymmetric