{"url":"https://api.github.com/gists/5827353","forks_url":"https://api.github.com/gists/5827353/forks","commits_url":"https://api.github.com/gists/5827353/commits","id":"5827353","node_id":"MDQ6R2lzdDU4MjczNTM=","git_pull_url":"https://gist.github.com/5827353.git","git_push_url":"https://gist.github.com/5827353.git","html_url":"https://gist.github.com/mbostock/5827353","files":{"weekday.js":{"filename":"weekday.js","type":"text/javascript","language":"JavaScript","raw_url":"https://gist.githubusercontent.com/mbostock/5827353/raw/9a69e8e4100ab1096158db4086a7330b0cc8d137/weekday.js","size":1446,"truncated":false,"content":"weekday = (function() {\n\n// Returns the weekday number for the given date relative to January 1, 1970.\nfunction weekday(date) {\n  var weekdays = weekdayOfYear(date),\n      year = date.getFullYear();\n  while (--year >= 1970) weekdays += weekdaysInYear(year);\n  return weekdays;\n}\n\n// Returns the date for the specified weekday number relative to January 1, 1970.\nweekday.invert = function(weekdays) {\n  var year = 1970,\n      yearWeekdays;\n \n  // Compute the year.\n  while ((yearWeekdays = weekdaysInYear(year)) <= weekdays) {\n    ++year;\n    weekdays -= yearWeekdays;\n  }\n \n  // Compute the date from the remaining weekdays.\n  var days = weekdays % 5,\n      day0 = ((new Date(year, 0, 1)).getDay() + 6) % 7;\n  if (day0 + days > 4) days += 2;\n  return new Date(year, 0, (weekdays / 5 | 0) * 7 + days + 1);\n};\n\n// Returns the number of weekdays in the specified year.\nfunction weekdaysInYear(year) {\n  return weekdayOfYear(new Date(year, 11, 31)) + 1;\n}\n\n// Returns the weekday number for the given date relative to the start of the year.\nfunction weekdayOfYear(date) {\n  var days = d3.time.dayOfYear(date),\n      weeks = days / 7 | 0,\n      day0 = (d3.time.year(date).getDay() + 6) % 7,\n      day1 = day0 + days - weeks * 7;\n  return Math.max(0, days - weeks * 2\n      - (day0 <= 5 && day1 >= 5 || day0 <= 12 && day1 >= 12) // extra saturday\n      - (day0 <= 6 && day1 >= 6 || day0 <= 13 && day1 >= 13)); // extra sunday\n}\n\nreturn weekday;\n\n})();\n","encoding":"utf-8"}},"public":true,"created_at":"2013-06-20T22:32:34Z","updated_at":"2020-11-30T09:49:08Z","description":"Counting Weekdays","comments":3,"user":null,"comments_enabled":true,"comments_url":"https://api.github.com/gists/5827353/comments","owner":{"login":"mbostock","id":230541,"node_id":"MDQ6VXNlcjIzMDU0MQ==","avatar_url":"https://avatars.githubusercontent.com/u/230541?v=4","gravatar_id":"","url":"https://api.github.com/users/mbostock","html_url":"https://github.com/mbostock","followers_url":"https://api.github.com/users/mbostock/followers","following_url":"https://api.github.com/users/mbostock/following{/other_user}","gists_url":"https://api.github.com/users/mbostock/gists{/gist_id}","starred_url":"https://api.github.com/users/mbostock/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mbostock/subscriptions","organizations_url":"https://api.github.com/users/mbostock/orgs","repos_url":"https://api.github.com/users/mbostock/repos","events_url":"https://api.github.com/users/mbostock/events{/privacy}","received_events_url":"https://api.github.com/users/mbostock/received_events","type":"User","user_view_type":"public","site_admin":false},"forks":[{"url":"https://api.github.com/gists/c0c1ca7f5bbb30cc3ef3","user":{"login":"mayo","id":2772,"node_id":"MDQ6VXNlcjI3NzI=","avatar_url":"https://avatars.githubusercontent.com/u/2772?v=4","gravatar_id":"","url":"https://api.github.com/users/mayo","html_url":"https://github.com/mayo","followers_url":"https://api.github.com/users/mayo/followers","following_url":"https://api.github.com/users/mayo/following{/other_user}","gists_url":"https://api.github.com/users/mayo/gists{/gist_id}","starred_url":"https://api.github.com/users/mayo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mayo/subscriptions","organizations_url":"https://api.github.com/users/mayo/orgs","repos_url":"https://api.github.com/users/mayo/repos","events_url":"https://api.github.com/users/mayo/events{/privacy}","received_events_url":"https://api.github.com/users/mayo/received_events","type":"User","user_view_type":"public","site_admin":false,"name":"Mayo Jordanov","company":null,"blog":"https://oyam.ca","location":"Vancouver, BC","email":null,"hireable":true,"bio":null,"twitter_username":null,"public_repos":49,"public_gists":20,"followers":14,"following":0,"created_at":"2008-03-10T21:44:56Z","updated_at":"2026-04-08T07:00:27Z"},"id":"c0c1ca7f5bbb30cc3ef3","created_at":"2014-05-06T23:23:35Z","updated_at":"2016-07-16T04:32:34Z"},{"url":"https://api.github.com/gists/a811cb38b506729d9e32","user":{"login":"andredumas","id":1518740,"node_id":"MDQ6VXNlcjE1MTg3NDA=","avatar_url":"https://avatars.githubusercontent.com/u/1518740?v=4","gravatar_id":"","url":"https://api.github.com/users/andredumas","html_url":"https://github.com/andredumas","followers_url":"https://api.github.com/users/andredumas/followers","following_url":"https://api.github.com/users/andredumas/following{/other_user}","gists_url":"https://api.github.com/users/andredumas/gists{/gist_id}","starred_url":"https://api.github.com/users/andredumas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andredumas/subscriptions","organizations_url":"https://api.github.com/users/andredumas/orgs","repos_url":"https://api.github.com/users/andredumas/repos","events_url":"https://api.github.com/users/andredumas/events{/privacy}","received_events_url":"https://api.github.com/users/andredumas/received_events","type":"User","user_view_type":"public","site_admin":false,"name":"André Dumas","company":null,"blog":"http://andredumas.au/","location":null,"email":null,"hireable":null,"bio":null,"twitter_username":null,"public_repos":10,"public_gists":43,"followers":127,"following":0,"created_at":"2012-03-09T03:47:59Z","updated_at":"2025-10-19T11:20:03Z"},"id":"a811cb38b506729d9e32","created_at":"2014-05-19T13:28:46Z","updated_at":"2015-08-29T14:01:36Z"}],"history":[{"user":{"login":"mbostock","id":230541,"node_id":"MDQ6VXNlcjIzMDU0MQ==","avatar_url":"https://avatars.githubusercontent.com/u/230541?v=4","gravatar_id":"","url":"https://api.github.com/users/mbostock","html_url":"https://github.com/mbostock","followers_url":"https://api.github.com/users/mbostock/followers","following_url":"https://api.github.com/users/mbostock/following{/other_user}","gists_url":"https://api.github.com/users/mbostock/gists{/gist_id}","starred_url":"https://api.github.com/users/mbostock/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mbostock/subscriptions","organizations_url":"https://api.github.com/users/mbostock/orgs","repos_url":"https://api.github.com/users/mbostock/repos","events_url":"https://api.github.com/users/mbostock/events{/privacy}","received_events_url":"https://api.github.com/users/mbostock/received_events","type":"User","user_view_type":"public","site_admin":false},"version":"2edd0182c4f0a59330ef84a0490f371f53e877d2","committed_at":"2013-06-20T22:35:20Z","change_status":{"total":44,"additions":25,"deletions":19},"url":"https://api.github.com/gists/5827353/2edd0182c4f0a59330ef84a0490f371f53e877d2"},{"user":{"login":"mbostock","id":230541,"node_id":"MDQ6VXNlcjIzMDU0MQ==","avatar_url":"https://avatars.githubusercontent.com/u/230541?v=4","gravatar_id":"","url":"https://api.github.com/users/mbostock","html_url":"https://github.com/mbostock","followers_url":"https://api.github.com/users/mbostock/followers","following_url":"https://api.github.com/users/mbostock/following{/other_user}","gists_url":"https://api.github.com/users/mbostock/gists{/gist_id}","starred_url":"https://api.github.com/users/mbostock/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mbostock/subscriptions","organizations_url":"https://api.github.com/users/mbostock/orgs","repos_url":"https://api.github.com/users/mbostock/repos","events_url":"https://api.github.com/users/mbostock/events{/privacy}","received_events_url":"https://api.github.com/users/mbostock/received_events","type":"User","user_view_type":"public","site_admin":false},"version":"62983310a183a009efb75daa8c1c0d5463a55459","committed_at":"2013-06-20T22:32:34Z","change_status":{"total":41,"additions":41,"deletions":0},"url":"https://api.github.com/gists/5827353/62983310a183a009efb75daa8c1c0d5463a55459"}],"truncated":false}