Mercurial > gemma
annotate pkg/common/time.go @ 4112:7959f62d3051
calculate scaling factor depending on DPI
author | Thomas Junk <thomas.junk@intevation.de> |
---|---|
date | Tue, 30 Jul 2019 16:31:21 +0200 |
parents | 80bdcd137a1d |
children | 8c5df0f3562e |
rev | line source |
---|---|
2150
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 // without warranty, see README.md and license for details. |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 // |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 // |
2929
e5f5afa45451
Change basic in/out datetime format to RFC3339
Bernhard Reiter <bernhard@intevation.de>
parents:
2150
diff
changeset
|
7 // Copyright (C) 2018, 2019 by via donau |
2150
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 // Software engineering by Intevation GmbH |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 // |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 // Author(s): |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
2929
e5f5afa45451
Change basic in/out datetime format to RFC3339
Bernhard Reiter <bernhard@intevation.de>
parents:
2150
diff
changeset
|
13 // * Bernhard E. Reiter <bernhard.reiter@intevation.de> |
2150
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 package common |
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
17 import ( |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
18 "math" |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
19 "time" |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
20 ) |
2929
e5f5afa45451
Change basic in/out datetime format to RFC3339
Bernhard Reiter <bernhard@intevation.de>
parents:
2150
diff
changeset
|
21 |
2150
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 const ( |
2929
e5f5afa45451
Change basic in/out datetime format to RFC3339
Bernhard Reiter <bernhard@intevation.de>
parents:
2150
diff
changeset
|
23 // time.RFC3339 equals "simplified ISO format as defined by ECMA-262" |
e5f5afa45451
Change basic in/out datetime format to RFC3339
Bernhard Reiter <bernhard@intevation.de>
parents:
2150
diff
changeset
|
24 // https://tc39.github.io/ecma262/#sec-date-time-string-format |
e5f5afa45451
Change basic in/out datetime format to RFC3339
Bernhard Reiter <bernhard@intevation.de>
parents:
2150
diff
changeset
|
25 // and "SHOULD be used in new protocols on the Internet." (RFC section 5.6) |
4078
80bdcd137a1d
Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
26 TimeFormat = time.RFC3339 |
80bdcd137a1d
Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
27 TimeFormatMicro = "2006-01-02T15:04:05.999Z07:00" |
80bdcd137a1d
Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
28 TimeFormatMicroLocal = "2006-01-02T15:04:05.000" |
80bdcd137a1d
Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
29 DateFormat = "2006-01-02" |
2150
2c67c51d57ad
Print templates: Implemented GET of all templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 ) |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
31 |
4075
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
32 // TimeParser is a list of time formats. |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
33 type TimeParser []string |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
34 |
4078
80bdcd137a1d
Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
35 var ParseTime = TimeParser{ |
80bdcd137a1d
Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
36 TimeFormat, |
80bdcd137a1d
Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
37 TimeFormatMicro, |
80bdcd137a1d
Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
38 TimeFormatMicroLocal, |
80bdcd137a1d
Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
39 DateFormat, |
80bdcd137a1d
Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
40 }.Parse |
80bdcd137a1d
Parse timezones from time inputs and send timezones in results.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4075
diff
changeset
|
41 |
3332
c86a8e70b40f
Made time interpolation more precise and added a unit test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3095
diff
changeset
|
42 var utc0 = time.Unix(0, 0) |
c86a8e70b40f
Made time interpolation more precise and added a unit test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3095
diff
changeset
|
43 |
4075
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
44 // Parse tries to parse a given string by the entries of the layout |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
45 // list one after another. The first matching time is returned. |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
46 // If no layout matches the last error is returned or time zero |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
47 // if the layout list is empty. |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
48 func (tg TimeParser) Parse(s string) (time.Time, error) { |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
49 var err error |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
50 var t time.Time |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
51 for _, layout := range tg { |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
52 if t, err = time.Parse(layout, s); err == nil { |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
53 break |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
54 } |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
55 } |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
56 return t, err |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
57 } |
cb74aa69954e
Moved TimeParser to common package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3367
diff
changeset
|
58 |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3332
diff
changeset
|
59 func InterpolateTime(t1 time.Time, m1 float64, t2 time.Time, m2 float64) func(float64) time.Time { |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
60 |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
61 // f(m1) = t1 |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
62 // f(m2) = t2 |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
63 // t1 = m1*a + b <=> b = t1 - m1*a |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
64 // t2 = m2*a + b |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
65 |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
66 // t1 - t2 = a*(m1 - m2) |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
67 // a = (t1-t2)/(m1 - m2) for m1 != m2 |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
68 |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
69 if m1 == m2 { |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3332
diff
changeset
|
70 t := t1.Add(t2.Sub(t1) / 2) |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3332
diff
changeset
|
71 return func(float64) time.Time { return t } |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
72 } |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
73 |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
74 a := t1.Sub(t2).Seconds() / (m1 - m2) |
3332
c86a8e70b40f
Made time interpolation more precise and added a unit test.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3095
diff
changeset
|
75 b := t1.Sub(utc0).Seconds() - m1*a |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
76 |
3367
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3332
diff
changeset
|
77 return func(m float64) time.Time { |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3332
diff
changeset
|
78 x := m*a + b |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3332
diff
changeset
|
79 secs := math.Ceil(x) |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3332
diff
changeset
|
80 nsecs := math.Ceil((x - secs) * (999999999 + 1)) |
ecb4baa2be1a
Simplified waterlevel classification.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3332
diff
changeset
|
81 return time.Unix(int64(secs), int64(nsecs)) |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
82 } |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2929
diff
changeset
|
83 } |