ponymail-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject incubator-ponymail git commit: DRY: move leapYear and end of month calculations to utils
Date Sat, 14 Jan 2017 13:57:05 GMT
Repository: incubator-ponymail
Updated Branches:
  refs/heads/master c09857c1b -> 187660a27


DRY: move leapYear and end of month calculations to utils


Project: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/commit/187660a2
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/tree/187660a2
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/diff/187660a2

Branch: refs/heads/master
Commit: 187660a274d0978250f78a811328525e9fac889d
Parents: c09857c
Author: Sebb <sebb@apache.org>
Authored: Sat Jan 14 12:31:25 2017 +0000
Committer: Sebb <sebb@apache.org>
Committed: Sat Jan 14 12:55:01 2017 +0000

----------------------------------------------------------------------
 CHANGELOG.md           |  1 +
 site/api/lib/utils.lua | 32 +++++++++++++++++++++++++++++++-
 site/api/mbox.lua      | 24 +-----------------------
 site/api/stats.lua     | 22 +---------------------
 4 files changed, 34 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/187660a2/CHANGELOG.md
----------------------------------------------------------------------
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 437a674..6203c01 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -120,6 +120,7 @@
 - links in stats pane don't set up the correct date range (#106)
 - code should delete scroll id after use (#336)
 - ll.py - Make --count work with --pretty
+- DRY: move leapYear and end of month calculations to utils
 
 ## CHANGES in 0.9b:
 

http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/187660a2/site/api/lib/utils.lua
----------------------------------------------------------------------
diff --git a/site/api/lib/utils.lua b/site/api/lib/utils.lua
index 6d168bb..131804c 100644
--- a/site/api/lib/utils.lua
+++ b/site/api/lib/utils.lua
@@ -19,6 +19,10 @@
 
 local JSON = require 'cjson' -- for JSON.null
 
+local days = { -- days in months of the year
+    31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 30, 31
+}
+
 -- find the original topic starter
 local function findParent(r, doc, elastic)
     local step = 0
@@ -93,11 +97,37 @@ local function extractCanonEmail(from)
     return eml
 end
 
+-- is it a leap year?
+local function leapYear(year)
+    if (year % 4 == 0) then
+        if (year%100 == 0) then
+            if (year %400 == 0) then
+                return true
+            end
+        else
+            return true
+        end
+        return false
+    end
+end
+
+-- get the last day of the month
+local function lastDayOfMonth(yyyy, mm)
+    local ldom
+    if mm == 2 and leapYear(yyyy) then
+        ldom = 29
+    else
+        ldom = days[mm]
+    end
+    return ldom
+end
 
 return {
     anonymizeHdrs = anonymizeHdrs,
     anonymizeBody = anonymizeBody,
     anonymizeEmail = anonymizeEmail,
     extractCanonEmail = extractCanonEmail,
-    findParent = findParent
+    findParent = findParent,
+    leapYear = leapYear,
+    lastDayOfMonth = lastDayOfMonth
 }

http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/187660a2/site/api/mbox.lua
----------------------------------------------------------------------
diff --git a/site/api/mbox.lua b/site/api/mbox.lua
index de769be..20ba38f 100644
--- a/site/api/mbox.lua
+++ b/site/api/mbox.lua
@@ -23,23 +23,6 @@ local user = require 'lib/user'
 local aaa = require 'lib/aaa'
 local utils = require 'lib/utils'
 
-local days = {
-    31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 30, 31 
-}
-
-local function leapYear(year)
-    if (year % 4 == 0) then
-        if (year%100 == 0)then                
-            if (year %400 == 0) then                    
-                return true
-            end
-        else                
-            return true
-        end
-        return false
-    end
-end
-
 --[[
     Parse the source to construct a valid 'From ' line.
 
@@ -81,12 +64,7 @@ function handle(r)
         local y, m = month:match("(%d+)%-(%d+)")
         m = tonumber(m)
         y = tonumber(y)
-        local d
-        if m == 2 and leapYear(y) then
-            d = 29
-        else
-            d = days[m]
-        end
+        local d = utils.lastDayOfMonth(y,m)
         
         -- fetch all results from the list (up to 10k results), make sure to get the 'private'
element
         local docs = elastic.raw {

http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/187660a2/site/api/stats.lua
----------------------------------------------------------------------
diff --git a/site/api/stats.lua b/site/api/stats.lua
index 39ccc25..135ded6 100644
--- a/site/api/stats.lua
+++ b/site/api/stats.lua
@@ -25,10 +25,6 @@ local config = require 'lib/config'
 local cross = require 'lib/cross'
 local utils = require 'lib/utils'
 
-local days = {
-    31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 30, 31 
-}
-
 local BODY_MAXLEN = config.stats_maxBody or 200
 -- words to exclude from word cloud:
 local EXCLUDE = config.stats_wordExclude or ".|..|..."
@@ -57,19 +53,6 @@ local function findSubject(gblob, blob, subject, epoch, maxAge)
     return nil
 end
 
-local function leapYear(year)
-    if (year % 4 == 0) then
-        if (year%100 == 0)then                
-            if (year %400 == 0) then                    
-                return true
-            end
-        else                
-            return true
-        end
-        return false
-    end
-end
-
 -- extract canonical email name from from field
 local function extractCanonName(from)
     local name = from:match("([^<]+)%s*<.->") or from:match("%S+@%S+") or from:match("%((.-)%)")
or "unknown"
@@ -252,10 +235,7 @@ function handle(r)
     if get.s and get.e then
         local em = tonumber(get.e:match("%-(%d%d?)$"))
         local ey = tonumber(get.e:match("^(%d%d%d%d)"))
-        local ec = days[em]
-        if em == 2 and leapYear(ey) then
-            ec = ec + 1
-        end
+        local ec = utils.lastDayOfMonth(ey, em)
         daterange = {        
             gte = get.s:gsub("%-","/").."/01 00:00:00",
             lte = get.e:gsub("%-","/").."/" .. ec .. " 23:59:59",


Mime
View raw message