|
| 1 | +# Advanced Usage of Eones |
| 2 | + |
| 3 | +This file contains advanced examples of date and time manipulation with Eones. |
| 4 | + |
| 5 | +## Advanced Date Manipulation |
| 6 | + |
| 7 | +### Truncation and Rounding |
| 8 | + |
| 9 | +```python |
| 10 | +date = Eones("2024-06-15 14:30:45") |
| 11 | + |
| 12 | +# Truncate to the start of different units |
| 13 | +date.floor("day") # 2024-06-15 00:00:00 |
| 14 | +date.floor("month") # 2024-06-01 00:00:00 |
| 15 | +date.floor("year") # 2024-01-01 00:00:00 |
| 16 | +date.floor("hour") # 2024-06-15 14:00:00 |
| 17 | + |
| 18 | +# Advance to the end of different units |
| 19 | +date.ceil("day") # 2024-06-15 23:59:59 |
| 20 | +date.ceil("month") # 2024-06-30 23:59:59 |
| 21 | +date.ceil("year") # 2024-12-31 23:59:59 |
| 22 | + |
| 23 | +# Round to the nearest unit |
| 24 | +date.round("hour") # Round to the nearest hour |
| 25 | +date.round("day") # Round to the nearest day |
| 26 | +``` |
| 27 | + |
| 28 | +### Start and End of Periods |
| 29 | + |
| 30 | +```python |
| 31 | +date = Eones("2024-06-15 14:30:45") |
| 32 | + |
| 33 | +# Go to the start of different periods |
| 34 | +date.start_of("day") # 2024-06-15 00:00:00 |
| 35 | +date.start_of("week") # Monday of that week |
| 36 | +date.start_of("month") # 2024-06-01 00:00:00 |
| 37 | +date.start_of("year") # 2024-01-01 00:00:00 |
| 38 | + |
| 39 | +# Go to the end of different periods |
| 40 | +date.end_of("day") # 2024-06-15 23:59:59 |
| 41 | +date.end_of("week") # Sunday of that week |
| 42 | +date.end_of("month") # 2024-06-30 23:59:59 |
| 43 | +date.end_of("year") # 2024-12-31 23:59:59 |
| 44 | +``` |
| 45 | + |
| 46 | +### Component Replacement |
| 47 | + |
| 48 | +```python |
| 49 | +date = Eones("2024-06-15 14:30:45") |
| 50 | + |
| 51 | +# Change specific components |
| 52 | +date.replace(day=1) # Change to day 1 of the month |
| 53 | +date.replace(month=12) # Change to December |
| 54 | +date.replace(year=2025) # Change to 2025 |
| 55 | +date.replace(hour=0, minute=0, second=0) # Midnight |
| 56 | + |
| 57 | +print(date.format("%Y-%m-%d %H:%M:%S")) |
| 58 | +``` |
| 59 | + |
| 60 | +## Comparisons and Validations |
| 61 | + |
| 62 | +### Date Comparisons |
| 63 | + |
| 64 | +```python |
| 65 | +date1 = Eones("2024-01-01") |
| 66 | +date2 = Eones("2024-12-31") |
| 67 | +date3 = Eones("2024-06-15") |
| 68 | + |
| 69 | +# Check if a date is between two others |
| 70 | +is_between = date3.is_between(date1, date2) |
| 71 | +print(is_between) # True |
| 72 | + |
| 73 | +# Check if they are in the same week |
| 74 | +same_week = date1.is_same_week(date2) |
| 75 | +print(same_week) # False |
| 76 | + |
| 77 | +# Check if they are in the same month/year |
| 78 | +same_month = date1.is_within(date3, check_month=True) |
| 79 | +same_year = date1.is_within(date3, check_month=False) |
| 80 | +``` |
| 81 | + |
| 82 | +### Next Weekdays |
| 83 | + |
| 84 | +```python |
| 85 | +date = Eones("2024-06-15") # Assume it's Saturday |
| 86 | + |
| 87 | +# Find the next Monday (0=Monday, 6=Sunday) |
| 88 | +next_monday = date.next_weekday(0) |
| 89 | +print(next_monday.format("%Y-%m-%d")) # Date of next Monday |
| 90 | + |
| 91 | +# Next Friday |
| 92 | +next_friday = date.next_weekday(4) |
| 93 | +print(next_friday.format("%Y-%m-%d")) |
| 94 | +``` |
| 95 | + |
| 96 | +## Date Ranges |
| 97 | + |
| 98 | +### Get period ranges |
| 99 | + |
| 100 | +```python |
| 101 | +date = Eones("2024-06-15") |
| 102 | + |
| 103 | +# Get ranges for different periods |
| 104 | +day_range = date.range("day") # (day_start, day_end) |
| 105 | +month_range = date.range("month") # (month_start, month_end) |
| 106 | +year_range = date.range("year") # (year_start, year_end) |
| 107 | + |
| 108 | +# Ranges return datetime tuples |
| 109 | +start, end = month_range |
| 110 | +print(f"Month: from {start} to {end}") |
| 111 | +``` |
| 112 | + |
| 113 | +## Additional Methods of the Date Class |
| 114 | + |
| 115 | +### Navigation by Weekdays |
| 116 | + |
| 117 | +```python |
| 118 | +date = Eones("2024-06-15") # Assume it's Saturday |
| 119 | + |
| 120 | +# Find the next specific weekday |
| 121 | +next_monday = date.next_weekday(0) # 0=Monday, 1=Tuesday, ..., 6=Sunday |
| 122 | +next_friday = date.next_weekday(4) |
| 123 | + |
| 124 | +# Find the previous specific weekday |
| 125 | +previous_wednesday = date.previous_weekday(2) |
| 126 | + |
| 127 | +print(next_monday.format("%Y-%m-%d")) # Date of next Monday |
| 128 | +print(previous_wednesday.format("%Y-%m-%d")) # Date of previous Wednesday |
| 129 | +``` |
| 130 | + |
| 131 | +### Dictionary Conversion |
| 132 | + |
| 133 | +```python |
| 134 | +date = Eones("2024-06-15 14:30:45") |
| 135 | + |
| 136 | +# Convert to dictionary with all components |
| 137 | +date_dict = date.to_dict() |
| 138 | +print(date_dict) |
| 139 | +# { |
| 140 | +# 'year': 2024, |
| 141 | +# 'month': 6, |
| 142 | +# 'day': 15, |
| 143 | +# 'hour': 14, |
| 144 | +# 'minute': 30, |
| 145 | +# 'second': 45, |
| 146 | +# 'microsecond': 0, |
| 147 | +# 'timezone': 'UTC' |
| 148 | +# } |
| 149 | +``` |
| 150 | + |
| 151 | +### Specific Period Ranges |
| 152 | + |
| 153 | +```python |
| 154 | +date = Eones("2024-06-15") |
| 155 | + |
| 156 | +# Get specific ranges using the Range class |
| 157 | +quarter_range = date.quarter_range() # Start and end of current quarter |
| 158 | +iso_week_range = date.week_range() # ISO week (Monday to Sunday) |
| 159 | + |
| 160 | +# Ranges return datetime tuples |
| 161 | +quarter_start, quarter_end = quarter_range |
| 162 | +print(f"Quarter: {quarter_start} - {quarter_end}") |
| 163 | +``` |
0 commit comments