-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimezones.py
More file actions
176 lines (139 loc) · 5.68 KB
/
timezones.py
File metadata and controls
176 lines (139 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""
Timezone (fixed offset) examples for the fasttime library.
This example demonstrates working with fixed UTC offsets
using OffsetDateTime.
"""
import fasttime
def main():
print("=== Timezone (Fixed Offset) Examples ===\n")
# 1. Creating UTC Offsets
print("1. Creating UTC Offsets")
print("-" * 40)
utc = fasttime.UtcOffset.from_seconds(0)
plus_five_thirty = fasttime.UtcOffset.from_hours_minutes(True, 5, 30)
minus_eight = fasttime.UtcOffset.from_hours_minutes(False, 8, 0)
plus_one = fasttime.UtcOffset.from_seconds(3600)
print(f"UTC: {utc} ({utc.as_seconds()} seconds)")
print(f"+05:30: {plus_five_thirty} ({plus_five_thirty.as_seconds()} seconds)")
print(f"-08:00: {minus_eight} ({minus_eight.as_seconds()} seconds)")
print(f"+01:00: {plus_one} ({plus_one.as_seconds()} seconds)")
print(f"UTC check: {utc.is_utc()}")
print(f"+05:30 UTC?: {plus_five_thirty.is_utc()}")
print()
# 2. Creating OffsetDateTime from UTC
print("2. Creating OffsetDateTime from UTC")
print("-" * 40)
utc_dt = fasttime.DateTime.parse("2024-06-15T12:00:00Z")
offset = fasttime.UtcOffset.from_hours_minutes(True, 5, 30)
odt = fasttime.OffsetDateTime.from_utc(utc_dt, offset)
print(f"UTC DateTime: {utc_dt}")
print(f"With offset: {odt}")
print(f"Local DateTime: {odt.to_local()}")
print()
# 3. Creating OffsetDateTime from Local Time
print("3. Creating OffsetDateTime from Local Time")
print("-" * 40)
date = fasttime.Date(2024, 6, 15)
time = fasttime.Time(18, 0, 0) # 6:00 PM local
offset = fasttime.UtcOffset.from_hours_minutes(True, 5, 30) # +05:30
odt = fasttime.OffsetDateTime.from_local(date, time, offset)
print(f"Local Date: {date}")
print(f"Local Time: {time}")
print(f"Offset: {offset}")
print(f"OffsetDateTime: {odt}")
print(f"UTC equivalent: {odt.utc}")
print()
# 4. Common Timezone Offsets
print("4. Common Timezone Offsets")
print("-" * 40)
common_offsets = [
("UTC", 0, 0, True),
("EST", 5, 0, False), # -05:00
("PST", 8, 0, False), # -08:00
("CET", 1, 0, True), # +01:00
("IST", 5, 30, True), # +05:30 (India)
("JST", 9, 0, True), # +09:00 (Japan)
]
date = fasttime.Date(2024, 6, 15)
time = fasttime.Time(12, 0, 0) # Noon local
for name, hours, minutes, positive in common_offsets:
offset = fasttime.UtcOffset.from_hours_minutes(positive, hours, minutes)
odt = fasttime.OffsetDateTime.from_local(date, time, offset)
print(f"{name:5} ({offset}): {odt} -> UTC: {odt.utc}")
print()
# 5. Converting Between Offsets
print("5. Converting Between Offsets")
print("-" * 40)
# Start with a time in New York (EST)
ny_offset = fasttime.UtcOffset.from_hours_minutes(False, 5, 0)
ny_date = fasttime.Date(2024, 6, 15)
ny_time = fasttime.Time(14, 30, 0) # 2:30 PM in NY
ny_dt = fasttime.OffsetDateTime.from_local(ny_date, ny_time, ny_offset)
print(f"New York time: {ny_dt}")
# Convert to UTC
utc_dt = ny_dt.utc
print(f"UTC time: {utc_dt}")
# Convert to Tokyo (JST)
tokyo_offset = fasttime.UtcOffset.from_hours_minutes(True, 9, 0)
tokyo_dt = fasttime.OffsetDateTime.from_utc(utc_dt, tokyo_offset)
print(f"Tokyo time: {tokyo_dt}")
print(f"Tokyo local: {tokyo_dt.to_local()}")
print()
# 6. Parsing RFC 3339 with Offsets
print("6. Parsing RFC 3339 with Offsets")
print("-" * 40)
rfc3339_examples = [
"2024-06-15T12:00:00Z",
"2024-06-15T12:00:00+00:00",
"2024-06-15T12:00:00-05:00",
"2024-06-15T12:00:00+05:30",
]
for s in rfc3339_examples:
odt = fasttime.OffsetDateTime.parse(s)
print(f"{s:35} -> {odt}")
print(f" UTC: {odt.utc}, Offset: {odt.offset}")
print()
# 7. Duration with Offsets
print("7. Duration with Offsets")
print("-" * 40)
start = fasttime.OffsetDateTime.parse("2024-06-15T10:00:00+05:30")
duration = fasttime.Duration.seconds(7200) # 2 hours
end = start.add_duration(duration)
print(f"Start: {start}")
print(f"Duration: {duration.total_seconds() / 3600} hours")
print(f"End: {end}")
print(f"Difference: {end.difference(start).total_seconds()} seconds")
print()
# 8. Comparing Across Offsets
print("8. Comparing Across Offsets")
print("-" * 40)
# 12:00 in New York
ny = fasttime.OffsetDateTime.parse("2024-06-15T12:00:00-05:00")
# 17:00 in UTC (same instant)
utc = fasttime.OffsetDateTime.parse("2024-06-15T17:00:00Z")
# 22:30 in India (same instant)
india = fasttime.OffsetDateTime.parse("2024-06-15T22:30:00+05:30")
print(f"New York: {ny}")
print(f"UTC: {utc}")
print(f"India: {india}")
print(f"\nAll represent the same instant:")
print(f"NY == UTC: {ny == utc}")
print(f"UTC == India: {utc == india}")
print(f"NY == India: {ny == india}")
print()
# 9. Unix Timestamps with Offsets
print("9. Unix Timestamps with Offsets")
print("-" * 40)
odt = fasttime.OffsetDateTime.parse("2024-06-15T18:00:00+05:30")
timestamp = odt.unix_timestamp()
timestamp_nanos = odt.unix_timestamp_nanos()
print(f"OffsetDateTime: {odt}")
print(f"UTC equivalent: {odt.utc}")
print(f"Unix timestamp: {timestamp}")
print(f"Unix timestamp ns: {timestamp_nanos}")
# Convert back
utc_dt = fasttime.DateTime.from_unix_timestamp(timestamp)
print(f"From timestamp: {utc_dt}")
print()
if __name__ == "__main__":
main()