Skip to content

Commit 801d798

Browse files
committed
♻️ refactor: update codebase #2
1 parent 1132830 commit 801d798

2 files changed

Lines changed: 371 additions & 0 deletions

File tree

format.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package timefy
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
// TimeAgo returns a human-readable string representing the time difference between the current time and the specified time.
9+
// It uses the local time zone for calculations and returns a relative time string (e.g., "just now", "5 minutes ago", "2 weeks ago").
10+
//
11+
// Parameters:
12+
// - `t`: A pointer to the Timex instance containing the time to compare.
13+
//
14+
// Returns:
15+
// - A string representing the relative time difference.
16+
//
17+
// Example:
18+
//
19+
// t := Timex{Time: time.Now().Add(-5 * time.Minute)}
20+
// fmt.Println(t.TimeAgo()) // Output: "5 minutes ago"
21+
func (t *Timex) TimeAgo() string {
22+
now := time.Now()
23+
diff := now.Sub(t.Time)
24+
25+
seconds := int(diff.Seconds())
26+
minutes := int(diff.Minutes())
27+
hours := int(diff.Hours())
28+
days := int(diff.Hours() / 24)
29+
30+
switch {
31+
case seconds < 60:
32+
return "just now"
33+
case minutes < 2:
34+
return "1 minute ago"
35+
case minutes < 60:
36+
return fmt.Sprintf("%d minutes ago", minutes)
37+
case hours < 2:
38+
return "1 hour ago"
39+
case hours < 24:
40+
return fmt.Sprintf("%d hours ago", hours)
41+
case days < 2:
42+
return "1 day ago"
43+
case days < 7:
44+
return fmt.Sprintf("%d days ago", days)
45+
case days < 14:
46+
return "1 week ago"
47+
case days < 30:
48+
return fmt.Sprintf("%d weeks ago", days/7)
49+
case days < 60:
50+
return "1 month ago"
51+
case days < 365:
52+
return fmt.Sprintf("%d months ago", days/30)
53+
case days < 730:
54+
return "1 year ago"
55+
default:
56+
return fmt.Sprintf("%d years ago", days/365)
57+
}
58+
}
59+
60+
// TimeUntil returns a human-readable string representing the time difference between the current time and the specified time.
61+
// It uses the local time zone for calculations and returns a relative time string (e.g., "in a few seconds", "in 5 minutes", "in 2 weeks").
62+
//
63+
// Parameters:
64+
// - `t`: A pointer to the Timex instance containing the time to compare.
65+
//
66+
// Returns:
67+
// - A string representing the relative time difference.
68+
//
69+
// Example:
70+
//
71+
// t := Timex{Time: time.Now().Add(5 * time.Minute)}
72+
// fmt.Println(t.TimeUntil()) // Output: "in 5 minutes"
73+
func (t *Timex) TimeUntil() string {
74+
diff := time.Until(t.Time)
75+
76+
if diff < 0 {
77+
return "in the past"
78+
}
79+
80+
seconds := int(diff.Seconds())
81+
minutes := int(diff.Minutes())
82+
hours := int(diff.Hours())
83+
days := int(diff.Hours() / 24)
84+
85+
switch {
86+
case seconds < 60:
87+
return "in a few seconds"
88+
case minutes < 2:
89+
return "in 1 minute"
90+
case minutes < 60:
91+
return fmt.Sprintf("in %d minutes", minutes)
92+
case hours < 2:
93+
return "in 1 hour"
94+
case hours < 24:
95+
return fmt.Sprintf("in %d hours", hours)
96+
case days < 2:
97+
return "in 1 day"
98+
case days < 7:
99+
return fmt.Sprintf("in %d days", days)
100+
case days < 14:
101+
return "in 1 week"
102+
case days < 30:
103+
return fmt.Sprintf("in %d weeks", days/7)
104+
case days < 60:
105+
return "in 1 month"
106+
case days < 365:
107+
return fmt.Sprintf("in %d months", days/30)
108+
case days < 730:
109+
return "in 1 year"
110+
default:
111+
return fmt.Sprintf("in %d years", days/365)
112+
}
113+
}

time.go

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,264 @@ func (t *Timex) DefaultFormatRFC() string {
752752
return t.FormatRFC(TimeFormat20060102150405999999)
753753
}
754754

755+
// DurationInDays returns the duration between the current time and another time in days.
756+
//
757+
// Parameters:
758+
// - `other`: The other time to calculate the duration from.
759+
//
760+
// Returns:
761+
// - An integer representing the duration in days.
762+
//
763+
// Example:
764+
//
765+
// t := Timex{Time: time.Now()}
766+
// formatted := t.DurationInDays(time.Now().AddDate(0, 0, 1)) // Returns 1
767+
func (t *Timex) DurationInDays(other time.Time) int {
768+
return int(t.Sub(other).Hours() / 24)
769+
}
770+
771+
// DurationInWeeks returns the duration between the current time and another time in weeks.
772+
//
773+
// Parameters:
774+
// - `other`: The other time to calculate the duration from.
775+
//
776+
// Returns:
777+
// - An integer representing the duration in weeks.
778+
//
779+
// Example:
780+
//
781+
// t := Timex{Time: time.Now()}
782+
// formatted := t.DurationInWeeks(time.Now().AddDate(0, 0, 7)) // Returns 1
783+
func (t *Timex) DurationInWeeks(other time.Time) int {
784+
return t.DurationInDays(other) / 7
785+
}
786+
787+
// DurationInMonths returns the duration between the current time and another time in months.
788+
//
789+
// Parameters:
790+
// - `other`: The other time to calculate the duration from.
791+
//
792+
// Returns:
793+
// - An integer representing the duration in months.
794+
//
795+
// Example:
796+
//
797+
// t := Timex{Time: time.Now()}
798+
// formatted := t.DurationInMonths(time.Now().AddDate(0, 1, 0)) // Returns 1
799+
func (t *Timex) DurationInMonths(other time.Time) int {
800+
years := t.Year() - other.Year()
801+
months := int(t.Month() - other.Month())
802+
return years*12 + months
803+
}
804+
805+
// DurationInYears returns the duration between the current time and another time in years.
806+
//
807+
// Parameters:
808+
// - `other`: The other time to calculate the duration from.
809+
//
810+
// Returns:
811+
// - An integer representing the duration in years.
812+
//
813+
// Example:
814+
//
815+
// t := Timex{Time: time.Now()}
816+
// formatted := t.DurationInYears(time.Now().AddDate(1, 0, 0)) // Returns 1
817+
func (t *Timex) DurationInYears(other time.Time) int {
818+
return t.Year() - other.Year()
819+
}
820+
821+
// IsWeekend checks if the current time is a weekend (Saturday or Sunday).
822+
//
823+
// Returns:
824+
// - A boolean value indicating whether the current time is a weekend.
825+
//
826+
// Example:
827+
//
828+
// t := Timex{Time: time.Now()}
829+
// isWeekend := t.IsWeekend() // Returns true if the current time is a weekend
830+
func (t *Timex) IsWeekend() bool {
831+
weekday := t.Weekday()
832+
return weekday == time.Saturday || weekday == time.Sunday
833+
}
834+
835+
// IsWeekday checks if the current time is a weekday (Monday to Friday).
836+
//
837+
// Returns:
838+
// - A boolean value indicating whether the current time is a weekday.
839+
//
840+
// Example:
841+
//
842+
// t := Timex{Time: time.Now()}
843+
// isWeekday := t.IsWeekday() // Returns true if the current time is a weekday
844+
func (t *Timex) IsWeekday() bool {
845+
return !t.IsWeekend()
846+
}
847+
848+
// IsBefore checks if the current time is before the specified time.
849+
//
850+
// Parameters:
851+
// - `other`: The other time to compare against.
852+
//
853+
// Returns:
854+
// - A boolean value indicating whether the current time is before the specified time.
855+
//
856+
// Example:
857+
//
858+
// t := Timex{Time: time.Now()}
859+
// isBefore := t.IsBefore(time.Now().AddDate(0, 0, 1)) // Returns true if the current time is before the specified time
860+
func (t *Timex) IsBefore(other time.Time) bool {
861+
return t.Time.Before(other)
862+
}
863+
864+
// IsAfter checks if the current time is after the specified time.
865+
//
866+
// Parameters:
867+
// - `other`: The other time to compare against.
868+
//
869+
// Returns:
870+
// - A boolean value indicating whether the current time is after the specified time.
871+
//
872+
// Example:
873+
//
874+
// t := Timex{Time: time.Now()}
875+
// isAfter := t.IsAfter(time.Now().AddDate(0, 0, 1)) // Returns true if the current time is after the specified time
876+
func (t *Timex) IsAfter(other time.Time) bool {
877+
return t.Time.After(other)
878+
}
879+
880+
// IsBetween checks if the current time is between the specified start and end times (inclusive).
881+
//
882+
// Parameters:
883+
// - `start`: The start time of the range.
884+
// - `end`: The end time of the range.
885+
//
886+
// Returns:
887+
// - A boolean value indicating whether the current time is between the specified start and end times.
888+
//
889+
// Example:
890+
//
891+
// t := Timex{Time: time.Now()}
892+
// isBetween := t.IsBetween(time.Now().AddDate(0, 0, 1), time.Now().AddDate(0, 0, 2)) // Returns true if the current time is between the specified start and end times
893+
func (t *Timex) IsBetween(start, end time.Time) bool {
894+
return (t.Equal(start) || t.After(start)) && (t.Equal(end) || t.Before(end))
895+
}
896+
897+
// IsSameDay checks if the current time is on the same day as the specified time.
898+
//
899+
// Parameters:
900+
// - `other`: The other time to compare against.
901+
//
902+
// Returns:
903+
// - A boolean value indicating whether the current time is on the same day as the specified time.
904+
//
905+
// Example:
906+
//
907+
// t := Timex{Time: time.Now()}
908+
// isSameDay := t.IsSameDay(time.Now().AddDate(0, 0, 1)) // Returns true if the current time is on the same day as the specified time
909+
func (t *Timex) IsSameDay(other time.Time) bool {
910+
return t.Year() == other.Year() && t.YearDay() == other.YearDay()
911+
}
912+
913+
// IsSameMonth checks if the current time is on the same month as the specified time.
914+
//
915+
// Parameters:
916+
// - `other`: The other time to compare against.
917+
//
918+
// Returns:
919+
// - A boolean value indicating whether the current time is on the same month as the specified time.
920+
//
921+
// Example:
922+
//
923+
// t := Timex{Time: time.Now()}
924+
// isSameMonth := t.IsSameMonth(time.Now().AddDate(0, 1, 0)) // Returns true if the current time is on the same month as the specified time
925+
func (t *Timex) IsSameMonth(other time.Time) bool {
926+
return t.Year() == other.Year() && t.Month() == other.Month()
927+
}
928+
929+
// IsSameYear checks if the current time is on the same year as the specified time.
930+
//
931+
// Parameters:
932+
// - `other`: The other time to compare against.
933+
//
934+
// Returns:
935+
// - A boolean value indicating whether the current time is on the same year as the specified time.
936+
//
937+
// Example:
938+
//
939+
// t := Timex{Time: time.Now()}
940+
// isSameYear := t.IsSameYear(time.Now().AddDate(1, 0, 0)) // Returns true if the current time is on the same year as the specified time
941+
func (t *Timex) IsSameYear(other time.Time) bool {
942+
return t.Year() == other.Year()
943+
}
944+
945+
// IsToday checks if the current time is today.
946+
//
947+
// Returns:
948+
// - A boolean value indicating whether the current time is today.
949+
//
950+
// Example:
951+
//
952+
// t := Timex{Time: time.Now()}
953+
// isToday := t.IsToday() // Returns true if the current time is today
954+
func (t *Timex) IsToday() bool {
955+
now := time.Now()
956+
return t.IsSameDay(now)
957+
}
958+
959+
// IsYesterday checks if the current time is yesterday.
960+
//
961+
// Returns:
962+
// - A boolean value indicating whether the current time is yesterday.
963+
//
964+
// Example:
965+
//
966+
// t := Timex{Time: time.Now()}
967+
// isYesterday := t.IsYesterday() // Returns true if the current time is yesterday
968+
func (t *Timex) IsYesterday() bool {
969+
yesterday := time.Now().AddDate(0, 0, -1)
970+
return t.IsSameDay(yesterday)
971+
}
972+
973+
// IsTomorrow checks if the current time is tomorrow.
974+
//
975+
// Returns:
976+
// - A boolean value indicating whether the current time is tomorrow.
977+
//
978+
// Example:
979+
//
980+
// t := Timex{Time: time.Now()}
981+
// isTomorrow := t.IsTomorrow() // Returns true if the current time is tomorrow
982+
func (t *Timex) IsTomorrow() bool {
983+
tomorrow := time.Now().AddDate(0, 0, 1)
984+
return t.IsSameDay(tomorrow)
985+
}
986+
987+
// IsPast checks if the current time is in the past.
988+
//
989+
// Returns:
990+
// - A boolean value indicating whether the current time is in the past.
991+
//
992+
// Example:
993+
//
994+
// t := Timex{Time: time.Now()}
995+
// isPast := t.IsPast() // Returns true if the current time is in the past
996+
func (t *Timex) IsPast() bool {
997+
return t.Before(time.Now())
998+
}
999+
1000+
// IsFuture checks if the current time is in the future.
1001+
//
1002+
// Returns:
1003+
// - A boolean value indicating whether the current time is in the future.
1004+
//
1005+
// Example:
1006+
//
1007+
// t := Timex{Time: time.Now()}
1008+
// isFuture := t.IsFuture() // Returns true if the current time is in the future
1009+
func (t *Timex) IsFuture() bool {
1010+
return t.After(time.Now())
1011+
}
1012+
7551013
// parseWithFormat attempts to parse a given date/time string `s` using a series of predefined formats
7561014
// specified in the `TimeFormats` slice of the Timex instance. It tries to parse the string in the provided
7571015
// `location` and returns the parsed time value or an error if parsing fails.

0 commit comments

Comments
 (0)