{"id":931,"date":"2016-01-16T09:41:09","date_gmt":"2016-01-16T17:41:09","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=931"},"modified":"2016-01-16T09:41:09","modified_gmt":"2016-01-16T17:41:09","slug":"get-third-sunday-of-the-month","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/get-third-sunday-of-the-month\/","title":{"rendered":"Get Third Sunday of The Month"},"content":{"rendered":"

Sometimes you have to rely on a routine to reliably provide dates. In this example I want to know for certain what the third Sunday of every month is. Could be various reasons but mostly for me it is to predict maintenance windows or use during scripting for automation.<\/p>\n

\r\n$ cat \/tmp\/thirdweek.py \r\nfrom datetime import date, timedelta\r\n\r\ndef allsundays(year):\r\n   d = date(year, 1, 1)                    # January 1st\r\n   d += timedelta(days = 6 - d.weekday())  # First Sunday\r\n   while d.year == year:\r\n      yield d\r\n      d += timedelta(days = 7)\r\n\r\ndef SundaysInMonth(year,month):\r\n  d = date(year,month,1)\t\t  # First day of the month\r\n  d += timedelta(days = 6 - d.weekday())  # First Sunday\r\n  while d.year == year and d.month == month:\r\n      yield d\r\n      d += timedelta(days = 7)\r\n\r\ndef xSundayOfMonth(x,Sundays):\r\n  i=0\r\n  for Sunday in Sundays:\r\n    i=i+1\r\n    if i == x: \r\n      #print "%s - %s" % (i,Sunday)\r\n      return Sunday\r\n\r\nprint "All Sundays in Year"\r\nfor d in allsundays(2016):\r\n   print d\r\n\r\nprint "All Third Sundays in Year"\r\nfor month in range(1,13): \r\n  Sundays=SundaysInMonth(2016,month)\r\n  print xSundayOfMonth(3,Sundays)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

Sometimes you have to rely on a routine to reliably provide dates. In this example I want to know for<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61,13],"tags":[],"class_list":["post-931","post","type-post","status-publish","format-standard","hentry","category-automation","category-python"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/comments?post=931"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/931\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}