carbon/constellation.go

247 lines
4.4 KiB
Go
Raw Permalink Normal View History

2021-02-23 09:32:55 +08:00
package carbon
2021-08-05 19:39:19 +08:00
import (
"strings"
)
2021-02-23 09:32:55 +08:00
2024-02-01 11:25:01 +08:00
var constellations = []struct {
startMonth, startDay int
endMonth, endDay int
}{
2024-02-18 22:42:13 +08:00
{3, 21, 4, 19}, // Aries
{4, 20, 5, 20}, // Taurus
{5, 21, 6, 21}, // Gemini
{6, 22, 7, 22}, // Cancer
{7, 23, 8, 22}, // Leo
{8, 23, 9, 22}, // Virgo
{9, 23, 10, 23}, // Libra
{10, 24, 11, 22}, // Scorpio
{11, 23, 12, 21}, // Sagittarius
{12, 22, 1, 19}, // Capricorn
{1, 20, 2, 18}, // Aquarius
{2, 19, 3, 20}, // Pisces
2024-02-01 11:25:01 +08:00
}
2022-04-14 10:43:07 +08:00
// Constellation gets constellation name like "Aries", i18n is supported.
2021-07-28 15:18:05 +08:00
// 获取星座支持i18n
2021-02-23 09:32:55 +08:00
func (c Carbon) Constellation() string {
2021-07-23 10:55:38 +08:00
if c.IsInvalid() {
2021-02-23 09:32:55 +08:00
return ""
}
if len(c.lang.resources) == 0 {
c.lang.SetLocale(defaultLocale)
2021-02-23 09:32:55 +08:00
}
index := -1
2022-04-12 17:17:31 +08:00
_, month, day := c.Date()
2024-02-01 11:25:01 +08:00
for i := 0; i < len(constellations); i++ {
constellation := constellations[i]
if month == constellation.startMonth && day >= constellation.startDay {
2024-02-18 22:42:13 +08:00
index = i
2024-02-01 11:25:01 +08:00
}
if month == constellation.endMonth && day <= constellation.endDay {
2024-02-18 22:42:13 +08:00
index = i
2024-02-01 11:25:01 +08:00
}
2021-02-23 09:32:55 +08:00
}
2024-11-25 16:16:15 +08:00
c.lang.rw.RLock()
defer c.lang.rw.RUnlock()
2024-02-01 11:25:01 +08:00
if resources, ok := c.lang.resources["constellations"]; ok {
slice := strings.Split(resources, "|")
2024-01-07 20:03:58 +08:00
if len(slice) == MonthsPerYear {
2021-08-02 10:22:23 +08:00
return slice[index]
}
2021-02-23 09:32:55 +08:00
}
return ""
}
2021-09-16 09:52:35 +08:00
// IsAries reports whether is Aries.
2021-07-28 15:18:05 +08:00
// 是否是白羊座
2021-02-23 09:32:55 +08:00
func (c Carbon) IsAries() bool {
2021-07-23 10:55:38 +08:00
if c.IsInvalid() {
2021-07-09 15:12:20 +08:00
return false
}
2022-04-12 17:17:31 +08:00
_, month, day := c.Date()
if month == 3 && day >= 21 {
2021-02-23 09:32:55 +08:00
return true
}
2022-04-12 17:17:31 +08:00
if month == 4 && day <= 19 {
2021-02-23 09:32:55 +08:00
return true
}
return false
}
2021-09-16 09:52:35 +08:00
// IsTaurus reports whether is Taurus.
2021-07-28 15:18:05 +08:00
// 是否是金牛座
2021-02-23 09:32:55 +08:00
func (c Carbon) IsTaurus() bool {
2021-07-23 10:55:38 +08:00
if c.IsInvalid() {
2021-07-09 15:12:20 +08:00
return false
}
2022-04-12 17:17:31 +08:00
_, month, day := c.Date()
if month == 4 && day >= 20 {
2021-02-23 09:32:55 +08:00
return true
}
2022-04-12 17:17:31 +08:00
if month == 5 && day <= 20 {
2021-02-23 09:32:55 +08:00
return true
}
return false
}
2021-09-16 09:52:35 +08:00
// IsGemini reports whether is Gemini.
2021-07-28 15:18:05 +08:00
// 是否是双子座
2021-02-23 09:32:55 +08:00
func (c Carbon) IsGemini() bool {
2021-07-23 10:55:38 +08:00
if c.IsInvalid() {
2021-07-09 15:12:20 +08:00
return false
}
2022-04-12 17:17:31 +08:00
_, month, day := c.Date()
if month == 5 && day >= 21 {
2021-02-23 09:32:55 +08:00
return true
}
2022-04-12 17:17:31 +08:00
if month == 6 && day <= 21 {
2021-02-23 09:32:55 +08:00
return true
}
return false
}
2021-09-16 09:52:35 +08:00
// IsCancer reports whether is Cancer.
2021-07-28 15:18:05 +08:00
// 是否是巨蟹座
2021-02-23 09:32:55 +08:00
func (c Carbon) IsCancer() bool {
2021-07-23 10:55:38 +08:00
if c.IsInvalid() {
2021-07-09 15:12:20 +08:00
return false
}
2022-04-12 17:17:31 +08:00
_, month, day := c.Date()
if month == 6 && day >= 22 {
2021-02-23 09:32:55 +08:00
return true
}
2022-04-12 17:17:31 +08:00
if month == 7 && day <= 22 {
2021-02-23 09:32:55 +08:00
return true
}
return false
}
2021-09-16 09:52:35 +08:00
// IsLeo reports whether is Leo.
2021-07-28 15:18:05 +08:00
// 是否是狮子座
2021-02-23 09:32:55 +08:00
func (c Carbon) IsLeo() bool {
2021-07-23 10:55:38 +08:00
if c.IsInvalid() {
2021-07-09 15:12:20 +08:00
return false
}
2022-04-12 17:17:31 +08:00
_, month, day := c.Date()
if month == 7 && day >= 23 {
2021-02-23 09:32:55 +08:00
return true
}
2022-04-12 17:17:31 +08:00
if month == 8 && day <= 22 {
2021-02-23 09:32:55 +08:00
return true
}
return false
}
2021-09-16 09:52:35 +08:00
// IsVirgo reports whether is Virgo.
2021-07-28 15:18:05 +08:00
// 是否是处女座
2021-02-23 09:32:55 +08:00
func (c Carbon) IsVirgo() bool {
2021-07-23 10:55:38 +08:00
if c.IsInvalid() {
2021-07-09 15:12:20 +08:00
return false
}
2022-04-12 17:17:31 +08:00
_, month, day := c.Date()
if month == 8 && day >= 23 {
2021-02-23 09:32:55 +08:00
return true
}
2022-04-12 17:17:31 +08:00
if month == 9 && day <= 22 {
2021-02-23 09:32:55 +08:00
return true
}
return false
}
2021-09-16 09:52:35 +08:00
// IsLibra reports whether is Libra.
2021-07-28 15:18:05 +08:00
// 是否是天秤座
2021-02-23 09:32:55 +08:00
func (c Carbon) IsLibra() bool {
2021-07-23 10:55:38 +08:00
if c.IsInvalid() {
2021-07-09 15:12:20 +08:00
return false
}
2022-04-12 17:17:31 +08:00
_, month, day := c.Date()
if month == 9 && day >= 23 {
2021-02-23 09:32:55 +08:00
return true
}
2022-04-12 17:17:31 +08:00
if month == 10 && day <= 23 {
2021-02-23 09:32:55 +08:00
return true
}
return false
}
2021-09-16 09:52:35 +08:00
// IsScorpio reports whether is Scorpio.
2021-07-28 15:18:05 +08:00
// 是否是天蝎座
2021-02-23 09:32:55 +08:00
func (c Carbon) IsScorpio() bool {
2021-07-23 10:55:38 +08:00
if c.IsInvalid() {
2021-07-09 15:12:20 +08:00
return false
}
2022-04-12 17:17:31 +08:00
_, month, day := c.Date()
if month == 10 && day >= 24 {
2021-02-23 09:32:55 +08:00
return true
}
2022-04-12 17:17:31 +08:00
if month == 11 && day <= 22 {
2021-02-23 09:32:55 +08:00
return true
}
return false
}
2021-09-16 09:52:35 +08:00
// IsSagittarius reports whether is Sagittarius.
2021-07-28 15:18:05 +08:00
// 是否是射手座
2021-02-23 09:32:55 +08:00
func (c Carbon) IsSagittarius() bool {
2021-07-23 10:55:38 +08:00
if c.IsInvalid() {
2021-07-09 15:12:20 +08:00
return false
}
2022-04-12 17:17:31 +08:00
_, month, day := c.Date()
if month == 11 && day >= 22 {
2021-02-23 09:32:55 +08:00
return true
}
2022-04-12 17:17:31 +08:00
if month == 12 && day <= 21 {
2021-02-23 09:32:55 +08:00
return true
}
return false
}
2021-09-16 09:52:35 +08:00
// IsCapricorn reports whether is Capricorn.
2021-07-28 15:18:05 +08:00
// 是否是摩羯座
2021-02-23 09:32:55 +08:00
func (c Carbon) IsCapricorn() bool {
2021-07-23 10:55:38 +08:00
if c.IsInvalid() {
2021-07-09 15:12:20 +08:00
return false
}
2022-04-12 17:17:31 +08:00
_, month, day := c.Date()
if month == 12 && day >= 22 {
2021-02-23 09:32:55 +08:00
return true
}
2022-04-12 17:17:31 +08:00
if month == 1 && day <= 19 {
2021-02-23 09:32:55 +08:00
return true
}
return false
}
2021-09-16 09:52:35 +08:00
// IsAquarius reports whether is Aquarius.
2021-07-28 15:18:05 +08:00
// 是否是水瓶座
2021-02-23 09:32:55 +08:00
func (c Carbon) IsAquarius() bool {
2021-07-23 10:55:38 +08:00
if c.IsInvalid() {
2021-07-09 15:12:20 +08:00
return false
}
2022-04-12 17:17:31 +08:00
_, month, day := c.Date()
if month == 1 && day >= 20 {
2021-02-23 09:32:55 +08:00
return true
}
2022-04-12 17:17:31 +08:00
if month == 2 && day <= 18 {
2021-02-23 09:32:55 +08:00
return true
}
return false
}
2021-09-16 09:52:35 +08:00
// IsPisces reports whether is Pisces.
2021-07-28 15:18:05 +08:00
// 是否是双鱼座
2021-02-23 09:32:55 +08:00
func (c Carbon) IsPisces() bool {
2021-07-23 10:55:38 +08:00
if c.IsInvalid() {
2021-07-09 15:12:20 +08:00
return false
}
2022-04-12 17:17:31 +08:00
_, month, day := c.Date()
if month == 2 && day >= 19 {
2021-02-23 09:32:55 +08:00
return true
}
2022-04-12 17:17:31 +08:00
if month == 3 && day <= 20 {
2021-02-23 09:32:55 +08:00
return true
}
return false
}