{"id":7188,"date":"2012-02-15T20:17:03","date_gmt":"2012-02-16T01:17:03","guid":{"rendered":"http:\/\/scruss.com\/blog\/?p=7188"},"modified":"2012-02-15T20:17:03","modified_gmt":"2012-02-16T01:17:03","slug":"awk-day-of-the-week-function","status":"publish","type":"post","link":"https:\/\/scruss.com\/blog\/2012\/02\/15\/awk-day-of-the-week-function\/","title":{"rendered":"Awk day of the week function"},"content":{"rendered":"<p>Yet another in my series of awk functions no-one but me will ever use:<\/p>\n<pre style=\"color: #000000; background: #ffffff;\"><span style=\"color: #800000; font-weight: bold;\">function<\/span> dow<span style=\"color: #808030;\">(<\/span>year<span style=\"color: #808030;\">,<\/span> month<span style=\"color: #808030;\">,<\/span> day<span style=\"color: #808030;\">)<\/span> <span style=\"color: #800080;\">{<\/span>\r\n<span style=\"color: #696969;\"># Modified from C Snippets \"calsupp.c\" public domain by Ray McVay <\/span>\r\n<span style=\"color: #696969;\"># <a href=\"http:\/\/www8.cs.umu.se\/~isak\/snippets\/calsupp.c\">http:\/\/www8.cs.umu.se\/~isak\/snippets\/calsupp.c<\/a><\/span>\r\n<span style=\"color: #696969;\"># returns 0-6 where 0 == sunday<\/span>\r\n<span style=\"color: #696969;\"># tested over 24000 days in range of unix timestamp, 1970-2035<\/span>\r\n\r\n    day_of_week <span style=\"color: #808030;\">=<\/span> <span style=\"color: #008c00;\">0<\/span><span style=\"color: #808030;\">;<\/span>\r\n\r\n    <span style=\"color: #800000; font-weight: bold;\">if<\/span> <span style=\"color: #808030;\">(<\/span>month <span style=\"color: #808030;\">&lt;<\/span><span style=\"color: #808030;\">=<\/span> <span style=\"color: #008c00;\">2<\/span><span style=\"color: #808030;\">)<\/span> <span style=\"color: #800080;\">{<\/span>\r\n        month <span style=\"color: #808030;\">+<\/span><span style=\"color: #808030;\">=<\/span> <span style=\"color: #008c00;\">12<\/span><span style=\"color: #808030;\">;<\/span>\r\n        year<span style=\"color: #808030;\">-<\/span><span style=\"color: #808030;\">-<\/span><span style=\"color: #808030;\">;<\/span>\r\n    <span style=\"color: #800080;\">}<\/span>\r\n    day_of_week <span style=\"color: #808030;\">=<\/span> <span style=\"color: #808030;\">(<\/span>day <span style=\"color: #808030;\">+<\/span> month <span style=\"color: #808030;\">*<\/span> <span style=\"color: #008c00;\">2<\/span> <span style=\"color: #808030;\">+<\/span> <span style=\"color: #800000; font-weight: bold;\">int<\/span><span style=\"color: #808030;\">(<\/span><span style=\"color: #808030;\">(<\/span><span style=\"color: #808030;\">(<\/span>month <span style=\"color: #808030;\">+<\/span> <span style=\"color: #008c00;\">1<\/span><span style=\"color: #808030;\">)<\/span> <span style=\"color: #808030;\">*<\/span> <span style=\"color: #008c00;\">6<\/span><span style=\"color: #808030;\">)<\/span> <span style=\"color: #800000;\">\/<\/span><span style=\"color: #0000e6;\"> 10) + year + int(year <\/span><span style=\"color: #800000;\">\/<\/span> <span style=\"color: #008c00;\">4<\/span><span style=\"color: #808030;\">)<\/span> <span style=\"color: #808030;\">-<\/span> <span style=\"color: #800000; font-weight: bold;\">int<\/span><span style=\"color: #808030;\">(<\/span>year <span style=\"color: #800000;\">\/<\/span><span style=\"color: #0000e6;\"> 100) + int(year <\/span><span style=\"color: #800000;\">\/<\/span> <span style=\"color: #008c00;\">400<\/span><span style=\"color: #808030;\">)<\/span> <span style=\"color: #808030;\">+<\/span> <span style=\"color: #008c00;\">2<\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #808030;\">;<\/span>\r\n    day_of_week <span style=\"color: #808030;\">=<\/span> day_of_week <span style=\"color: #808030;\">%<\/span> <span style=\"color: #008c00;\">7<\/span><span style=\"color: #808030;\">;<\/span>\r\n    <span style=\"color: #800000; font-weight: bold;\">return<\/span> <span style=\"color: #808030;\">(<\/span><span style=\"color: #808030;\">(<\/span>day_of_week <span style=\"color: #808030;\">?<\/span> day_of_week <span style=\"color: #808030;\">:<\/span> <span style=\"color: #008c00;\">7<\/span><span style=\"color: #808030;\">)<\/span> <span style=\"color: #808030;\">-<\/span> <span style=\"color: #008c00;\">1<\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #808030;\">;<\/span>\r\n<span style=\"color: #800080;\">}<\/span><\/pre>\n<p>Basically, all this does is calculate a Julian day number, then take its remainder modulo 7. I&#8217;d seen an <a href=\"http:\/\/unix-simple.blogspot.com\/2006\/09\/awk-day-of-week-program.html\">example<\/a> that parsed the output of &#8216;cal&#8217;. That&#8217;s one way of doing it; not necessarily mine.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yet another in my series of awk functions no-one but me will ever use: function dow(year, month, day) { # Modified from C Snippets &#8220;calsupp.c&#8221; public domain by Ray McVay # http:\/\/www8.cs.umu.se\/~isak\/snippets\/calsupp.c # returns 0-6 where 0 == sunday # tested over 24000 days in range of unix timestamp, 1970-2035 day_of_week = 0; if (month [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7],"tags":[817],"class_list":["post-7188","post","type-post","status-publish","format-standard","hentry","category-computers-suck","tag-awk"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pQNZZ-1RW","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/7188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/comments?post=7188"}],"version-history":[{"count":5,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/7188\/revisions"}],"predecessor-version":[{"id":7194,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/7188\/revisions\/7194"}],"wp:attachment":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/media?parent=7188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/categories?post=7188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/tags?post=7188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}