├── LICENSE ├── NOTICE ├── README.md ├── docs.go ├── example └── main.go ├── format.go ├── format_test.go ├── go.mod ├── parse.go ├── parse_test.go └── wercker.yml /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2015 Yoshi Yamaguchi 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | datemaki -- flexible datetime library 2 | Copyright 2015 Yoshi Yamaguchi 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # datemaki -- flexible and fancy datetime parse library 2 | [![wercker status](https://app.wercker.com/status/9ad56661ed04d8b632c2cf9f1f79f7dd/s "wercker status")](https://app.wercker.com/project/bykey/9ad56661ed04d8b632c2cf9f1f79f7dd) 3 | 4 | ![datemaki](https://4meee.s3.amazonaws.com/files/article/646005/large_646005_2.jpg "datemaki") 5 | 6 | ## Specs 7 | https://gist.github.com/shibukawa/6221750ca958a8880e01 8 | 9 | ## License 10 | Apache License, Version 2.0. See LICENSE file for details. 11 | -------------------------------------------------------------------------------- /docs.go: -------------------------------------------------------------------------------- 1 | // Package datemaki is a flexible and fancy datetime parsing library. 2 | // 3 | // This is work in progress. This package aims to handle datetime 4 | // expressed in more natural languages and convert them into 5 | // time.Time instances. 6 | // 7 | // Currently this package supports following datetime expression, mainly 8 | // used in Git commands. 9 | // 10 | // * Past datetime described in ago 11 | // * 2 seconds ago 12 | // * 3 minutes ago 13 | // * 4 hours ago 14 | // * 5 days ago 15 | // * 1 week ago 16 | // * 2 months ago 17 | // * 1 year, 3 months ago 18 | // * 1.year.4.months.ago 19 | // * 2.years.ago 20 | // 21 | // * Relative date 22 | // * now 23 | // * today 24 | // * yesterday 25 | // * last friday 26 | // 27 | // * Relative date and fixed time 28 | // * noon yesterday 29 | // * tea yesterday 30 | // * midnight today 31 | // * 3pm today 32 | // * 2am last friday 33 | // * 19:00 yesterday (under implementation) 34 | // * 10am 35 | // 36 | // * Absolute datetime 37 | // * August 6th 38 | // * 06/05/2009 39 | // * 06.05.2009 40 | // * Feb 28, 4AM 41 | // * 2AM Jun 4 42 | // * 6AM, June 7, 2009 43 | // * 2008-12-01 44 | package datemaki 45 | -------------------------------------------------------------------------------- /example/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/ymotongpoo/datemaki" 7 | ) 8 | 9 | func main() { 10 | exps := []string{ 11 | "3 days ago", 12 | "2015 Dec 22nd 23:00:00", 13 | "yesterday 14:00", 14 | } 15 | 16 | for _, e := range exps { 17 | t, err := datemaki.Parse(e) 18 | if err != nil { 19 | fmt.Println(err) 20 | continue 21 | } 22 | fmt.Println(t) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /format.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015-2017 Yoshi Yamaguchi, Yoshiki Shibukawa 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package datemaki 16 | 17 | import ( 18 | "fmt" 19 | "time" 20 | ) 21 | 22 | type formatType int 23 | 24 | const ( 25 | relative formatType = iota 26 | absolute 27 | ) 28 | 29 | // FormatDuration is almost shortcut of the following function call: 30 | // 31 | // FormatRelativeDurationFrom(time.Now(), time.Now().Add(duration)) 32 | // 33 | func FormatDuration(duration time.Duration) string { 34 | now := time.Now() 35 | return formatDurationFrom(now, now.Add(duration), relative) 36 | } 37 | 38 | // FormatRelativeDurationFrom generates human readable string of time.Duration like GitHub. 39 | // 40 | // For example, duration between src and dst is less than 60 seconds, 41 | // this function returns "now". 42 | // 43 | // It returns text like "1 minute ago", "5 minutes later", "2 days ago". 44 | func FormatRelativeDurationFrom(src, dst time.Time) string { 45 | return formatDurationFrom(src, dst, relative) 46 | } 47 | 48 | // FormatDurationFrom generates human readable string of time.Duration like GitHub. 49 | // 50 | // Compare with FormatRelativeDurationFrom, it returns "1 day ago" instead of "yesterday". 51 | func FormatDurationFrom(src, dst time.Time) string { 52 | return formatDurationFrom(src, dst, absolute) 53 | } 54 | 55 | func daysOfYear(t time.Time) int { 56 | year := time.Date(t.Year(), time.December, 31, 0, 0, 0, 0, time.Local) 57 | return year.YearDay() 58 | } 59 | 60 | func formatDurationFrom(src, dst time.Time, ft formatType) string { 61 | suffix := "later" 62 | inverted := false 63 | if src.After(dst) { 64 | suffix = "ago" 65 | inverted = true 66 | src, dst = dst, src 67 | } 68 | dur := dst.Sub(src) 69 | 70 | if dur < time.Minute { 71 | return "now" 72 | } 73 | 74 | if dur < time.Hour { 75 | minutes := (dst.Minute() - src.Minute()) % 60 76 | if minutes < 0 { 77 | minutes += 60 78 | } 79 | if minutes == 1 { 80 | return "1 minute " + suffix 81 | } 82 | return fmt.Sprintf("%d minutes %s", minutes, suffix) 83 | } 84 | 85 | if dur < time.Hour*24 { 86 | hours := (dst.Hour() - src.Hour()) % 24 87 | if hours < 0 { 88 | hours += 24 89 | } 90 | if hours == 1 { 91 | return "1 hour " + suffix 92 | } 93 | return fmt.Sprintf("%d hours %s", hours, suffix) 94 | } 95 | 96 | if dur < time.Hour*24*31 { 97 | days := dst.YearDay() - src.YearDay() 98 | if days < 0 { 99 | days += daysOfYear(src) 100 | } 101 | if days == 1 { 102 | if ft == relative { 103 | if inverted { 104 | return "yesterday" 105 | } 106 | return "tomorrow" 107 | } else { 108 | return "1 day " + suffix 109 | } 110 | } 111 | return fmt.Sprintf("%d days %s", days, suffix) 112 | } 113 | 114 | if dur < time.Hour*24*365 { 115 | months := int(dst.Month()) - int(src.Month()) 116 | if months < 0 { 117 | months += 12 118 | } 119 | if months == 1 { 120 | if ft == relative { 121 | if inverted { 122 | return "last month" 123 | } 124 | return "next month" 125 | } else { 126 | return "1 month " + suffix 127 | } 128 | } 129 | return fmt.Sprintf("%d months %s", months, suffix) 130 | } 131 | 132 | years := dst.Year() - src.Year() 133 | if years == 1 { 134 | if ft == relative { 135 | if inverted { 136 | return "last year" 137 | } 138 | return "next year" 139 | } else { 140 | return "1 year " + suffix 141 | } 142 | } 143 | return fmt.Sprintf("%d years %s", years, suffix) 144 | } 145 | 146 | func FormatRalative(date time.Time) string { 147 | return "" 148 | } 149 | -------------------------------------------------------------------------------- /format_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015-2017 Yoshi Yamaguchi, Yoshiki Shibukawa 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package datemaki 16 | 17 | import ( 18 | "testing" 19 | "time" 20 | ) 21 | 22 | const ( 23 | sec time.Duration = time.Second 24 | min = time.Minute 25 | hour = time.Hour 26 | day = time.Hour * 24 27 | month = time.Hour * 31 * 24 28 | year = time.Hour * 365 * 24 29 | ) 30 | 31 | var ( 32 | mid = time.Date(2017, time.January, 15, 12, 30, 30, 0, time.Local) 33 | earlyEdge = time.Date(2017, time.January, 1, 0, 0, 0, 0, time.Local) 34 | lateEdge = time.Date(2017, time.December, 31, 23, 59, 59, 0, time.Local) 35 | ) 36 | 37 | var formatAbsoluteDurationTests = []struct { 38 | label string 39 | expect string 40 | src time.Time 41 | duration time.Duration 42 | }{ 43 | {"now", "now", mid, sec * 1}, 44 | 45 | {"1 minute", "1 minute ago", mid, -min * 1}, 46 | {"1 minute 2", "1 minute ago", mid, -min*1 - sec}, 47 | {"1 minute 3", "1 minute later", mid, min * 1}, 48 | {"1 minute 4", "1 minute later", mid, min*1 + sec}, 49 | 50 | {"2 minutes 1", "2 minutes ago", mid, -min * 2}, 51 | {"2 minutes 2", "2 minutes ago", earlyEdge, -min - sec*2}, 52 | {"2 minutes 3", "2 minutes later", mid, min * 2}, 53 | {"2 minutes 4", "2 minutes later", lateEdge, min + sec*2}, 54 | 55 | {"1 hour", "1 hour ago", mid, -hour * 1}, 56 | {"1 hour 2", "1 hour ago", mid, -hour*1 - min}, 57 | {"1 hour 3", "1 hour later", mid, hour * 1}, 58 | {"1 hour 4", "1 hour later", mid, hour*1 + min}, 59 | 60 | {"2 hours 1", "2 hours ago", mid, -hour * 2}, 61 | {"2 hours 2", "2 hours ago", earlyEdge, -hour - min*2}, 62 | {"2 hours 3", "2 hours later", mid, hour * 2}, 63 | {"2 hours 4", "2 hours later", lateEdge, hour + min*2}, 64 | 65 | {"1 day", "1 day ago", mid, -day * 1}, 66 | {"1 day 2", "1 day ago", mid, -day*1 - hour}, 67 | {"1 day 3", "1 day later", mid, day * 1}, 68 | {"1 day 4", "1 day later", mid, day*1 + hour}, 69 | 70 | {"2 days 1", "2 days ago", mid, -day * 2}, 71 | {"2 days 2", "2 days ago", earlyEdge, -day - hour*2}, 72 | {"2 days 3", "2 days later", mid, day * 2}, 73 | {"2 days 4", "2 days later", lateEdge, day + hour*2}, 74 | 75 | {"1 month", "1 month ago", mid, -month * 1}, 76 | {"1 month 2", "1 month ago", mid, -month*1 - day}, 77 | {"1 month 3", "1 month later", mid, month * 1}, 78 | {"1 month 4", "1 month later", mid, month*1 + day}, 79 | 80 | {"2 months 1", "2 months ago", mid, -month * 2}, 81 | {"2 months 2", "2 months ago", earlyEdge, -month - day*2}, 82 | {"2 months 3", "2 months later", mid, month * 2}, 83 | {"2 months 4", "2 months later", lateEdge, month + day*2}, 84 | 85 | {"1 year", "1 year ago", mid, -year * 1}, 86 | {"1 year 2", "1 year ago", mid, -year*1 - day}, 87 | {"1 year 3", "1 year later", mid, year * 1}, 88 | {"1 year 4", "1 year later", mid, year*1 + day}, 89 | 90 | {"2 years 1", "2 years ago", mid, -year * 2}, 91 | {"2 years 2", "2 years ago", earlyEdge, -year - day*2}, 92 | {"2 years 3", "2 years later", mid, year * 2}, 93 | {"2 years 4", "2 years later", lateEdge, year + day*2}, 94 | } 95 | 96 | func TestFormatAbsoluteDuration(t *testing.T) { 97 | for _, tt := range formatAbsoluteDurationTests { 98 | dst := tt.src.Add(tt.duration) 99 | actual := FormatDurationFrom(tt.src, dst) 100 | if actual != tt.expect { 101 | t.Errorf("%s fails: expected='%s' actual='%s'", tt.label, tt.expect, actual) 102 | } 103 | } 104 | } 105 | 106 | var formatRelativeDurationTests = []struct { 107 | label string 108 | expect string 109 | src time.Time 110 | duration time.Duration 111 | }{ 112 | {"1 day", "yesterday", mid, -day * 1}, 113 | {"1 day 2", "yesterday", mid, -day*1 - hour}, 114 | {"1 day 3", "tomorrow", mid, day * 1}, 115 | {"1 day 4", "tomorrow", mid, day*1 + hour}, 116 | 117 | {"2 days 1", "2 days ago", mid, -day * 2}, 118 | {"2 days 2", "2 days ago", earlyEdge, -day - hour*2}, 119 | {"2 days 3", "2 days later", mid, day * 2}, 120 | {"2 days 4", "2 days later", lateEdge, day + hour*2}, 121 | 122 | {"1 month", "last month", mid, -month * 1}, 123 | {"1 month 2", "last month", mid, -month*1 - day}, 124 | {"1 month 3", "next month", mid, month * 1}, 125 | {"1 month 4", "next month", mid, month*1 + day}, 126 | 127 | {"2 months 1", "2 months ago", mid, -month * 2}, 128 | {"2 months 2", "2 months ago", earlyEdge, -month - day*2}, 129 | {"2 months 3", "2 months later", mid, month * 2}, 130 | {"2 months 4", "2 months later", lateEdge, month + day*2}, 131 | 132 | {"1 year", "last year", mid, -year * 1}, 133 | {"1 year 2", "last year", mid, -year*1 - day}, 134 | {"1 year 3", "next year", mid, year * 1}, 135 | {"1 year 4", "next year", mid, year*1 + day}, 136 | 137 | {"2 years 1", "2 years ago", mid, -year * 2}, 138 | {"2 years 2", "2 years ago", earlyEdge, -year - day*2}, 139 | {"2 years 3", "2 years later", mid, year * 2}, 140 | {"2 years 4", "2 years later", lateEdge, year + day*2}, 141 | } 142 | 143 | func TestFormatRelativeDuration(t *testing.T) { 144 | for _, tt := range formatRelativeDurationTests { 145 | dst := tt.src.Add(tt.duration) 146 | actual := FormatRelativeDurationFrom(tt.src, dst) 147 | if actual != tt.expect { 148 | t.Errorf("%s fails: expected='%s' actual='%s'", tt.label, tt.expect, actual) 149 | } 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/ymotongpoo/datemaki 2 | 3 | go 1.16 4 | -------------------------------------------------------------------------------- /parse.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015-2017 Yoshi Yamaguchi 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package datemaki 16 | 17 | import ( 18 | "fmt" 19 | "regexp" 20 | "strconv" 21 | "strings" 22 | "time" 23 | "unicode" 24 | "unicode/utf8" 25 | ) 26 | 27 | var ( 28 | numericExp = regexp.MustCompile(`^[0-9/\.\ \-:]+$`) 29 | hhmmExp = regexp.MustCompile(`[0-9]{1,2}:[0-9]{1,2}`) 30 | hhmmssExp = regexp.MustCompile(`[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}`) 31 | ordinalDayExp = regexp.MustCompile(`[0-9]{1,2}(th|st|nd|rd)`) 32 | timezoneExp = regexp.MustCompile(`^[0-9]{1,4}-[0-9]{1,2}-[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}(:[0-9]{1,2})? (\+|-)([0-9]{1,2})?:?[0-9]{2}$`) 33 | unixZero = time.Unix(0, 0) 34 | ) 35 | 36 | var fullMonth = map[string]time.Month{ 37 | "january": time.January, 38 | "february": time.February, 39 | "march": time.March, 40 | "april": time.April, 41 | "may": time.May, 42 | "june": time.June, 43 | "july": time.July, 44 | "august": time.August, 45 | "september": time.September, 46 | "october": time.October, 47 | "november": time.November, 48 | "december": time.December, 49 | } 50 | 51 | var shortMonth = map[string]time.Month{ 52 | "jan": time.January, 53 | "feb": time.February, 54 | "mar": time.March, 55 | "apr": time.April, 56 | "may": time.May, 57 | "jun": time.June, 58 | "jul": time.July, 59 | "aug": time.August, 60 | "sep": time.September, 61 | "oct": time.October, 62 | "nov": time.November, 63 | "dec": time.December, 64 | } 65 | 66 | // Parse accepts contextful date format and returns absolute time.Time value. 67 | func Parse(value string) (time.Time, error) { 68 | value = strings.TrimSpace(value) 69 | switch { 70 | case strings.HasSuffix(value, "ago"): 71 | return ParseAgo(value) 72 | case hasRelative(value): 73 | return ParseRelative(value) 74 | default: 75 | return ParseAbsolute(value) 76 | } 77 | return time.Now().In(time.Local), nil // TODO(ymotongpoo): replace actual time. 78 | } 79 | 80 | // MustParse is like Parse but panics if the passed valuecannot be parsed. 81 | // It simplifies safe initialization of global variables holding parsed time. 82 | func MustParse(value string) time.Time { 83 | result, err := Parse(value) 84 | if err != nil { 85 | panic(err) 86 | } 87 | return result 88 | } 89 | 90 | // splitTokens splits value with commas, periods and spaces. 91 | // Currently, it only expects single byte character tokenizer. 92 | func splitTokens(value string) []string { 93 | f := func(c rune) bool { 94 | return c == rune(' ') || c == rune(',') || c == rune('.') || c == rune('/') || c == rune('-') 95 | } 96 | return strings.FieldsFunc(value, f) 97 | } 98 | 99 | // hasRelative confirms if value contains relative datatime words, such as 100 | // "now", "today", "last xxx", "noon", "pm", "am" and so on. 101 | func hasRelative(value string) bool { 102 | keywords := []string{"now", "today", "yesterday", "last"} 103 | for _, k := range keywords { 104 | if strings.Contains(value, k) { 105 | return true 106 | } 107 | } 108 | return false 109 | } 110 | 111 | // ParseAgo parse "xxxx ago" format and returns corresponding absolute datetime. 112 | func ParseAgo(value string) (time.Time, error) { 113 | tokens := splitTokens(value) 114 | now := time.Now().In(time.Local) 115 | for i := 0; i < len(tokens); i++ { 116 | t := tokens[i] 117 | if t == "ago" { 118 | return now, nil 119 | } 120 | if i%2 == 0 { 121 | var err error 122 | n, err := strconv.Atoi(t) 123 | if err != nil { 124 | return unixZero, fmt.Errorf("Format error: %v", t) 125 | } 126 | now, err = subDate(now, n, tokens[i+1]) 127 | if err != nil { 128 | return unixZero, err 129 | } 130 | i++ 131 | } 132 | } 133 | return now, nil 134 | } 135 | 136 | // subDate subtracts n*unit duration from t and return the result. 137 | // supportes units are "year", "month", "week", "day", "hour", "minute", "second", and those plurals. 138 | func subDate(t time.Time, n int, unit string) (time.Time, error) { 139 | if strings.HasSuffix(unit, "s") { 140 | unit = string([]byte(unit)[:len(unit)-1]) 141 | } 142 | switch unit { 143 | case "year": 144 | return t.AddDate(-1*n, 0, 0), nil 145 | case "month": 146 | return t.AddDate(0, -1*n, 0), nil 147 | case "week": 148 | return t.AddDate(0, 0, -7*n), nil 149 | case "day": 150 | return t.AddDate(0, 0, -1*n), nil 151 | case "hour": 152 | return t.Add(time.Duration(-1*n) * time.Hour), nil 153 | case "minute": 154 | return t.Add(time.Duration(-1*n) * time.Minute), nil 155 | case "second": 156 | return t.Add(time.Duration(-1*n) * time.Second), nil 157 | default: 158 | return t, fmt.Errorf("Unsupported time unit: %v", unit) 159 | } 160 | } 161 | 162 | // ParseRelative returns absolute datetime corresponding to relative date expressed in value. 163 | func ParseRelative(value string) (time.Time, error) { 164 | tokens := splitTokens(value) 165 | var t time.Time 166 | t = time.Now().In(time.Local) 167 | for i := 0; i < len(tokens); i++ { 168 | switch tokens[i] { 169 | case "last": 170 | days, err := daysFromLast(tokens[i+1]) 171 | if err != nil { 172 | return t, err 173 | } 174 | i++ 175 | t = t.Add(time.Duration(-1*days) * time.Hour * 24) 176 | case "yesterday": 177 | t = t.Add(time.Duration(-24 * time.Hour)) 178 | case "today": 179 | // pass 180 | case "noon", "tea", "midnight": 181 | var err error 182 | t, err = convertTimeWord(tokens[i]) 183 | if err != nil { 184 | return t, err 185 | } 186 | case "now", "never": 187 | return convertTimeWord(tokens[i]) 188 | default: 189 | var t1 time.Time 190 | var err error 191 | t1, err = parse12HourClock(tokens[i]) 192 | if err != nil { 193 | t1, err = parseNumericTime(tokens[i]) 194 | if err != nil { 195 | return unixZero, fmt.Errorf("Unexpected time value, %v: %v", value, err) 196 | } 197 | } 198 | t = time.Date(t.Year(), t.Month(), t.Day(), t1.Hour(), t1.Minute(), t1.Second(), 0, time.Local) 199 | } 200 | } 201 | return t, nil 202 | } 203 | 204 | // daysFromLast returns days from last weekday passed to 205 | func daysFromLast(weekday string) (int, error) { 206 | now := time.Now().In(time.Local) 207 | var day int 208 | switch strings.ToLower(weekday) { // time.Weekday defines value. 209 | case "sunday": 210 | day = 0 211 | case "monday": 212 | day = 1 213 | case "tuesday": 214 | day = 2 215 | case "wednesday": 216 | day = 3 217 | case "thursday": 218 | day = 4 219 | case "friday": 220 | day = 5 221 | case "saturday": 222 | day = 6 223 | default: 224 | return 0, fmt.Errorf("%v is not weekday", weekday) 225 | } 226 | return int(now.Weekday()) - day + 7, nil 227 | } 228 | 229 | // convertTimeWord converts words of time of day to numerial expression. 230 | func convertTimeWord(word string) (time.Time, error) { 231 | now := time.Now().In(time.Local) 232 | switch word { 233 | case "now": 234 | return now, nil 235 | case "noon": 236 | return time.Date(now.Year(), now.Month(), now.Day(), 12, 0, 0, 0, time.Local), nil 237 | case "tea": 238 | return time.Date(now.Year(), now.Month(), now.Day(), 15, 0, 0, 0, time.Local), nil 239 | case "midnight": 240 | return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local), nil 241 | case "never": 242 | return unixZero, nil 243 | } 244 | return now, fmt.Errorf("Unsupported time word: %v", word) 245 | } 246 | 247 | // parse12HourClock convers 12-hour clock time to 24-hour one. 248 | func parse12HourClock(word string) (time.Time, error) { 249 | lower := strings.ToLower(word) 250 | now := time.Now().In(time.Local) 251 | 252 | start := 0 253 | hour := 0 254 | var err error 255 | for width := 0; start < len(lower); start += width { 256 | var r rune 257 | r, width = utf8.DecodeRuneInString(lower[start:]) 258 | if !unicode.IsNumber(r) { 259 | hour, err = strconv.Atoi(lower[:start]) 260 | if err != nil || hour > 12 || hour < 0 { 261 | return time.Now(), fmt.Errorf("Wrong hour: %v", word) 262 | } 263 | if string(lower[start:]) == "am" { 264 | break 265 | } 266 | if string(lower[start:]) == "pm" { 267 | hour += 12 268 | break 269 | } 270 | return time.Now(), fmt.Errorf("Unsupported 12 hour clock notation: %v", word) 271 | } 272 | } 273 | return time.Date(now.Year(), now.Month(), now.Day(), hour, 0, 0, 0, time.Local), nil 274 | } 275 | 276 | // ParseAbsolute converts absolute datetime into time.Time. Basic idea is same as time.Parse(), 277 | // but this detects the format of value and convert it automatically. 278 | func ParseAbsolute(value string) (time.Time, error) { 279 | if numericExp.MatchString(value) { 280 | return parseNumeric(value) 281 | } 282 | 283 | tokens := splitTokens(strings.ToLower(value)) 284 | year, day := 0, 0 285 | var month time.Month 286 | monthParsed := false 287 | var t time.Time 288 | for _, token := range tokens { 289 | var ok bool 290 | var err error 291 | switch { 292 | case len(token) == 4 && numericExp.MatchString(token): 293 | year, err = strconv.Atoi(token) 294 | if err != nil { 295 | return unixZero, fmt.Errorf("%v, Unexpected year value: %v", value, err) 296 | } 297 | case ordinalDayExp.MatchString(token): 298 | day, err = strconv.Atoi(token[:len(token)-2]) 299 | if err != nil { 300 | return unixZero, fmt.Errorf("%v, Unexpected day value: %v", value, err) 301 | } 302 | case strings.HasSuffix(token, "am") || strings.HasSuffix(token, "pm"): 303 | t, err = parse12HourClock(token) 304 | if err != nil { 305 | return unixZero, fmt.Errorf("%v, Unexpected 12-hour clock time: %v", value, err) 306 | } 307 | case strings.Index(token, ":") != -1: 308 | t, err = parseNumericTime(token) 309 | if err != nil { 310 | return unixZero, fmt.Errorf("%v, Unexpected numeric time: %v", value, err) 311 | } 312 | case monthParsed: 313 | day, err = strconv.Atoi(token) 314 | if err != nil || day > 31 || day < 0 { 315 | return unixZero, fmt.Errorf("%v, Unexpected day value: %v", value, err) 316 | } 317 | default: 318 | month, ok = fullMonth[token] 319 | if ok { 320 | monthParsed = true 321 | break 322 | } 323 | month, ok = shortMonth[token] 324 | if ok { 325 | monthParsed = true 326 | break 327 | } 328 | m, err := strconv.Atoi(token) 329 | if err != nil || m > 12 || m < 0 { 330 | return unixZero, fmt.Errorf("%v, Unexpected month value, %v, error: %v", value, m, err) 331 | } 332 | month = time.Month(m) 333 | monthParsed = true 334 | } 335 | } 336 | 337 | if year == 0 { 338 | year = time.Now().Year() 339 | } 340 | 341 | return time.Date(year, month, day, t.Hour(), t.Minute(), t.Second(), 0, time.Local), nil 342 | } 343 | 344 | // parseNumericTime converts a time expressed in digits to time.Time. 345 | func parseNumericTime(value string) (time.Time, error) { 346 | now := time.Now() 347 | var t time.Time 348 | var err error 349 | if hhmmssExp.MatchString(value) { 350 | t, err = time.Parse("15:04:05", value) 351 | if err != nil { 352 | return parseBroadcasterTime(value) 353 | } 354 | return t, nil 355 | } else if hhmmExp.MatchString(value) { 356 | t, err = time.Parse("15:04", value) 357 | if err != nil { 358 | return parseBroadcasterTime(value) 359 | } 360 | return t, nil 361 | } 362 | return time.Date(now.Year(), now.Month(), now.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.Local), nil 363 | } 364 | 365 | // parseBroadcasterTime converts a non-standard time expression used in TV schedules into normal one. 366 | // eg. 30:00 (= 6am tomorrow) 367 | // 368 | // TODO(ymotongpoo): tentative implementation for Go Advent Calendar 2015 369 | func parseBroadcasterTime(value string) (time.Time, error) { 370 | day := time.Now() 371 | var err error 372 | var hh, mm, ss int 373 | if hhmmssExp.MatchString(value) { 374 | tokens := strings.Split(value, ":") 375 | hh, err = strconv.Atoi(tokens[0]) 376 | if err != nil { 377 | return day, fmt.Errorf("HHMMSS Unexpected format: %v", value) 378 | } 379 | mm, err = strconv.Atoi(tokens[1]) 380 | if err != nil { 381 | return day, fmt.Errorf("HHMMSS Unexpected format: %v", value) 382 | } 383 | ss, err = strconv.Atoi(tokens[2]) 384 | if err != nil { 385 | return day, fmt.Errorf("HHMMSS Unexpected format: %v", value) 386 | } 387 | if ss > 60 { 388 | mm += ss / 60 389 | ss = ss % 60 390 | } 391 | if mm > 60 { 392 | hh += mm / 60 393 | mm = mm % 60 394 | } 395 | if hh > 24 { 396 | day = day.AddDate(0, 0, hh/24) 397 | hh = hh % 24 398 | } 399 | return time.Date(day.Year(), day.Month(), day.Day(), hh, mm, ss, 0, time.Local), nil 400 | } else if hhmmExp.MatchString(value) { // TODO(ymotongpoo): merge into the condition above by treating this as HH:MM:00. 401 | tokens := strings.Split(value, ":") 402 | hh, err = strconv.Atoi(tokens[0]) 403 | if err != nil { 404 | return day, fmt.Errorf("HHMM Unexpected format: %v", value) 405 | } 406 | mm, err = strconv.Atoi(tokens[1]) 407 | if err != nil { 408 | return day, fmt.Errorf("HHMM Unexpected format: %v", value) 409 | } 410 | ss = 0 411 | if mm > 60 { 412 | hh += mm / 60 413 | mm = mm % 60 414 | } 415 | if hh > 24 { 416 | day = day.AddDate(0, 0, hh/24) 417 | hh = hh % 24 418 | } 419 | } 420 | return time.Date(day.Year(), day.Month(), day.Day(), hh, mm, ss, 0, time.Local), nil 421 | } 422 | 423 | // parseNumeric convers a datetime expressed all in digits to time.Time. 424 | func parseNumeric(value string) (time.Time, error) { 425 | tokens := splitTokens(value) 426 | now := time.Now() 427 | year, month, day := 0, 0, 0 428 | var t time.Time 429 | for _, token := range tokens { 430 | var err error 431 | switch { 432 | case len(token) == 4: 433 | year, err = strconv.Atoi(token) 434 | if err != nil { // time package can handle days before unixtime 0. 435 | return now, fmt.Errorf("Error on parsing year: %v", value) 436 | } 437 | case len(token) == 2 || len(token) == 1: 438 | if month == 0 { 439 | month, err = strconv.Atoi(token) 440 | if err != nil || month < 0 || month > 12 { 441 | return now, fmt.Errorf("Error on parsing month: %v", value) 442 | } 443 | } else if day == 0 { 444 | day, err = strconv.Atoi(token) 445 | if err != nil || day < 0 || day > 31 { 446 | return now, fmt.Errorf("Error on parsing day: %v", value) 447 | } 448 | } 449 | case hhmmssExp.MatchString(token) || hhmmExp.MatchString(token): 450 | t, err = parseNumericTime(token) 451 | if err != nil { 452 | return now, fmt.Errorf("HHMMSS Unexpected format: %v, error: %v", value, err) 453 | } 454 | } 455 | } 456 | if year == 0 { 457 | year = now.Year() 458 | } 459 | return time.Date(year, time.Month(month), day, t.Hour(), t.Minute(), t.Second(), 0, time.Local), nil 460 | } 461 | -------------------------------------------------------------------------------- /parse_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015-2017 Yoshi Yamaguchi 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package datemaki 16 | 17 | import ( 18 | "strings" 19 | "testing" 20 | "time" 21 | ) 22 | 23 | var agoTests = []string{ 24 | "2 seconds ago", 25 | "3 minutes ago", 26 | "4 hours ago", 27 | "5 days ago", 28 | "1 week ago", 29 | "2 months ago", 30 | "1 year, 3 months ago", 31 | "1.year.4.months.ago", 32 | "2.years.ago", 33 | } 34 | 35 | func TestSplitTokens(t *testing.T) { 36 | for i, test := range agoTests { 37 | pre1 := strings.Replace(test, ",", " ", -1) 38 | pre2 := strings.Replace(pre1, ".", " ", -1) 39 | words := strings.Fields(pre2) 40 | tokens := splitTokens(test) 41 | if len(words) != len(tokens) { 42 | t.Errorf("#%d: word counts are different, %d is expected, got %d", i, len(words), len(tokens)) 43 | continue 44 | } 45 | } 46 | } 47 | 48 | func TestParseAgo(t *testing.T) { 49 | for i, test := range agoTests { 50 | parsed, err := ParseAgo(test) 51 | if err != nil { 52 | t.Errorf("#%v: %v", i, err) 53 | continue 54 | } 55 | t.Logf("#%v: parsed: %v (%v)", i, parsed, test) 56 | } 57 | } 58 | 59 | var relativeTests = []string{ 60 | "now", 61 | "today", 62 | "yesterday", 63 | "last friday", 64 | "noon yesterday", 65 | "tea yesterday", 66 | "midnight today", 67 | "3pm today", 68 | "2am last friday", 69 | "19:00 yesterday", 70 | } 71 | 72 | func TestHasRelative(t *testing.T) { 73 | for i, test := range relativeTests { 74 | if !hasRelative(test) { 75 | t.Errorf("#%v: %v", i, test) 76 | continue 77 | } 78 | } 79 | } 80 | 81 | func TestParseRelative(t *testing.T) { 82 | for i, test := range relativeTests { 83 | parsed, err := ParseRelative(test) 84 | if err != nil { 85 | t.Errorf("#%v: %v", i, err) 86 | continue 87 | } 88 | t.Logf("#%v: %v (%v)", i, parsed, test) 89 | } 90 | } 91 | 92 | var TwelveHourClockTests = []string{ 93 | "10am", 94 | "3pm", 95 | "1AM", 96 | "5PM", 97 | } 98 | 99 | func TestParse12HourClock(t *testing.T) { 100 | for i, test := range TwelveHourClockTests { 101 | parsed, err := parse12HourClock(test) 102 | if err != nil { 103 | t.Errorf("#%v: %v", i, err) 104 | continue 105 | } 106 | t.Logf("#%v: %v (%v)", i, parsed, test) 107 | } 108 | } 109 | 110 | func TestNumericDate(t *testing.T) { 111 | now := time.Now().In(time.Local) 112 | tests := map[string]time.Time{ 113 | "2008-12-01": time.Date(2008, 12, 1, 0, 0, 0, 0, time.Local), 114 | "06/05/2009": time.Date(2009, 6, 5, 0, 0, 0, 0, time.Local), 115 | "06.05.2009": time.Date(2009, 6, 5, 0, 0, 0, 0, time.Local), 116 | "06 05 2009": time.Date(2009, 6, 5, 0, 0, 0, 0, time.Local), 117 | "10/30": time.Date(now.Year(), 10, 30, 0, 0, 0, 0, time.Local), 118 | "01 02 2010 11:12": time.Date(2010, 1, 2, 11, 12, 0, 0, time.Local), 119 | "8 9 1999 1:22:33": time.Date(1999, 8, 9, 1, 22, 33, 0, time.Local), 120 | } 121 | 122 | for test, expected := range tests { 123 | parsed, err := parseNumeric(test) 124 | if err != nil { 125 | t.Errorf("%v: error parsing: %v", test, err) 126 | continue 127 | } 128 | if !parsed.Equal(expected) { 129 | t.Errorf("%v: wrongly parsed, got %v, %v expected", test, parsed, expected) 130 | continue 131 | } 132 | t.Logf("%v: %v (%v)", test, parsed, expected) 133 | } 134 | } 135 | 136 | func TestParseAbsolute(t *testing.T) { 137 | now := time.Now().In(time.Local) 138 | tests := map[string]time.Time{ 139 | "August 6th": time.Date(now.Year(), 8, 6, 0, 0, 0, 0, time.Local), 140 | "Feb 28, 4AM": time.Date(now.Year(), 2, 28, 4, 0, 0, 0, time.Local), 141 | "2AM Jun 4": time.Date(now.Year(), 6, 4, 2, 0, 0, 0, time.Local), 142 | "6AM, June 7, 2009": time.Date(2009, 6, 7, 6, 0, 0, 0, time.Local), 143 | } 144 | for test, expected := range tests { 145 | parsed, err := ParseAbsolute(test) 146 | if err != nil { 147 | t.Errorf("%v: error parsing: %v", test, err) 148 | continue 149 | } 150 | if !parsed.Equal(expected) { 151 | t.Errorf("%v: wrongly parsed, got %v, %v expected", test, parsed, expected) 152 | continue 153 | } 154 | t.Logf("%v: %v (%v)", test, parsed, expected) 155 | } 156 | } 157 | 158 | func TestTimezoneExp(t *testing.T) { 159 | tests := []string{ 160 | "2012-01-04 20:30:45 -05:00", 161 | "2013-02-04 20:30:45 +1100", 162 | "2014-04-03 20:30:45 -:30", 163 | } 164 | for i, test := range tests { 165 | if !timezoneExp.MatchString(test) { 166 | t.Errorf("#%v: not matched (%v)", i, test) 167 | } 168 | } 169 | } 170 | 171 | func TestParseBroadcasterTime(t *testing.T) { 172 | now := time.Now() 173 | tests := map[string]time.Time{ 174 | "30:00:00": time.Date(now.Year(), now.Month(), now.Day()+1, 6, 0, 0, 0, time.Local), 175 | "25:30:00": time.Date(now.Year(), now.Month(), now.Day()+1, 1, 30, 0, 0, time.Local), 176 | "24:10:00": time.Date(now.Year(), now.Month(), now.Day()+1, 0, 10, 0, 0, time.Local), 177 | "24:10:80": time.Date(now.Year(), now.Month(), now.Day()+1, 0, 11, 20, 0, time.Local), 178 | "20:70:80": time.Date(now.Year(), now.Month(), now.Day(), 21, 11, 20, 0, time.Local), 179 | "26:70:80": time.Date(now.Year(), now.Month(), now.Day()+1, 3, 11, 20, 0, time.Local), 180 | "25:00": time.Date(now.Year(), now.Month(), now.Day()+1, 1, 00, 00, 0, time.Local), 181 | "25:70": time.Date(now.Year(), now.Month(), now.Day()+1, 2, 10, 00, 0, time.Local), 182 | } 183 | for test, expected := range tests { 184 | parsed, err := parseBroadcasterTime(test) 185 | if err != nil { 186 | t.Errorf("%v: error parsing: %v", test, err) 187 | continue 188 | } 189 | if !parsed.Equal(expected) { 190 | t.Errorf("%v: wrongly parsed, got %v, %v expected", test, parsed, expected) 191 | continue 192 | } 193 | t.Logf("%v: %v (%v)", test, parsed, expected) 194 | } 195 | } 196 | -------------------------------------------------------------------------------- /wercker.yml: -------------------------------------------------------------------------------- 1 | box: google/golang 2 | 3 | build: 4 | steps: 5 | - setup-go-workspace 6 | - wercker/golint 7 | - script: 8 | name: go test -test.v 9 | code: | 10 | go test 11 | 12 | --------------------------------------------------------------------------------