1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-17 19:22:28 +08:00

Compare commits

...

2 Commits

Author SHA1 Message Date
CyJaySong
4edefcca67 expand BeginOfWeek、EndOfWeek (#59) 2022-10-10 15:44:22 +08:00
dudaodong
604acd9b07 doc:add lancet api doc website 2022-09-29 18:41:22 +08:00
3 changed files with 46 additions and 30 deletions

View File

@@ -1,5 +1,5 @@
<div align=center> <div align=center>
<img src="./logo.png" width="200" height="200"/> <a href="https://uvdream.github.io/lancet-docs/en/"><img src="./logo.png" width="200" height="200"/></a>
<br/> <br/>
@@ -68,7 +68,7 @@ func main() {
``` ```
## API Documentation ## API Documentation
## [lancet API doc](https://uvdream.github.io/lancet-docs/) Thanks [@UvDream](https://github.com/UvDream) for contributing.
### 1. Algorithm package implements some basic algorithm. eg. sort, search. ### 1. Algorithm package implements some basic algorithm. eg. sort, search.
```go ```go

View File

@@ -1,5 +1,5 @@
<div align=center> <div align=center>
<img src="./logo.png" width="200" height="200"/> <a href="https://uvdream.github.io/lancet-docs/"><img src="./logo.png" width="200" height="200"/><a/>
<br/> <br/>
@@ -68,7 +68,7 @@ func main() {
``` ```
## API文档 ## API文档
## [lancet API doc](https://uvdream.github.io/lancet-docs/) 感谢[@UvDream](https://github.com/UvDream)整理
### 1. algorithm算法包实现一些基本算法。eg. sort, search. ### 1. algorithm算法包实现一些基本算法。eg. sort, search.
```go ```go

View File

@@ -4,24 +4,24 @@
// Package datetime implements some functions to format date and time. // Package datetime implements some functions to format date and time.
// Note: // Note:
// 1. `format` param in FormatTimeToStr function should be as flow: // 1. `format` param in FormatTimeToStr function should be as flow:
//"yyyy-mm-dd hh:mm:ss" // "yyyy-mm-dd hh:mm:ss"
//"yyyy-mm-dd hh:mm" // "yyyy-mm-dd hh:mm"
//"yyyy-mm-dd hh" // "yyyy-mm-dd hh"
//"yyyy-mm-dd" // "yyyy-mm-dd"
//"yyyy-mm" // "yyyy-mm"
//"mm-dd" // "mm-dd"
//"dd-mm-yy hh:mm:ss" // "dd-mm-yy hh:mm:ss"
//"yyyy/mm/dd hh:mm:ss" // "yyyy/mm/dd hh:mm:ss"
//"yyyy/mm/dd hh:mm" // "yyyy/mm/dd hh:mm"
//"yyyy/mm/dd hh" // "yyyy/mm/dd hh"
//"yyyy/mm/dd" // "yyyy/mm/dd"
//"yyyy/mm" // "yyyy/mm"
//"mm/dd" // "mm/dd"
//"dd/mm/yy hh:mm:ss" // "dd/mm/yy hh:mm:ss"
//"yyyy" // "yyyy"
//"mm" // "mm"
//"hh:mm:ss" // "hh:mm:ss"
//"mm:ss" // "mm:ss"
package datetime package datetime
import ( import (
@@ -147,16 +147,32 @@ func EndOfDay(t time.Time) time.Time {
return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location()) return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location())
} }
// BeginOfWeek return beginning week, week begin from Sunday // BeginOfWeek return beginning week, default week begin from Sunday
func BeginOfWeek(t time.Time) time.Time { func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time {
y, m, d := t.AddDate(0, 0, 0-int(BeginOfDay(t).Weekday())).Date() var beginFromWeekday = time.Sunday
return time.Date(y, m, d, 0, 0, 0, 0, t.Location()) if len(beginFrom) > 0 {
beginFromWeekday = beginFrom[0]
}
y, m, d := t.AddDate(0, 0, int(beginFromWeekday-t.Weekday())).Date()
beginOfWeek := time.Date(y, m, d, 0, 0, 0, 0, t.Location())
if beginOfWeek.After(t) {
return beginOfWeek.AddDate(0, 0, -7)
}
return beginOfWeek
} }
// EndOfWeek return end week time, week end with Saturday // EndOfWeek return end week time, default week end with Saturday
func EndOfWeek(t time.Time) time.Time { func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time {
y, m, d := BeginOfWeek(t).AddDate(0, 0, 7).Add(-time.Nanosecond).Date() var endWithWeekday = time.Saturday
return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location()) if len(endWith) > 0 {
endWithWeekday = endWith[0]
}
y, m, d := t.AddDate(0, 0, int(endWithWeekday-t.Weekday())).Date()
var endWithWeek = time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location())
if endWithWeek.Before(t) {
endWithWeek = endWithWeek.AddDate(0, 0, 7)
}
return endWithWeek
} }
// BeginOfMonth return beginning of month // BeginOfMonth return beginning of month