0
# Date and Time Handling
1
2
Specialized date and time functionality designed for Exchange compatibility, including timezone handling, UTC conversion, and Exchange-specific datetime formats.
3
4
## Capabilities
5
6
### EWS DateTime
7
8
```python { .api }
9
class EWSDateTime:
10
def __init__(self, year: int, month: int, day: int, hour: int = 0, minute: int = 0, second: int = 0, microsecond: int = 0, tzinfo: EWSTimeZone = None):
11
"""Create an Exchange-aware datetime."""
12
13
@classmethod
14
def from_datetime(cls, dt: datetime) -> EWSDateTime:
15
"""Create EWSDateTime from Python datetime."""
16
17
@classmethod
18
def now(cls, tz: EWSTimeZone = None) -> EWSDateTime:
19
"""Get current datetime."""
20
21
def to_timezone(self, tz: EWSTimeZone) -> EWSDateTime:
22
"""Convert to different timezone."""
23
24
def astimezone(self, tz: EWSTimeZone) -> EWSDateTime:
25
"""Convert to timezone (alias for to_timezone)."""
26
27
def to_datetime(self) -> datetime:
28
"""Convert to Python datetime."""
29
30
class EWSDate:
31
def __init__(self, year: int, month: int, day: int):
32
"""Create an Exchange-aware date."""
33
34
@classmethod
35
def from_date(cls, d: date) -> EWSDate:
36
"""Create EWSDate from Python date."""
37
38
@classmethod
39
def today(cls) -> EWSDate:
40
"""Get today's date."""
41
42
def to_date(self) -> date:
43
"""Convert to Python date."""
44
```
45
46
### Timezone Handling
47
48
```python { .api }
49
class EWSTimeZone:
50
def __init__(self, key: str, name: str = None, bias: int = None):
51
"""Create an Exchange timezone."""
52
53
@classmethod
54
def from_pytz(cls, tz) -> EWSTimeZone:
55
"""Create from pytz timezone."""
56
57
@classmethod
58
def localzone(cls) -> EWSTimeZone:
59
"""Get local system timezone."""
60
61
key: str
62
name: str
63
bias: int
64
65
# UTC timezone constant
66
UTC: EWSTimeZone
67
68
def UTC_NOW() -> EWSDateTime:
69
"""Get current UTC datetime."""
70
```
71
72
Usage examples:
73
74
```python
75
from exchangelib import EWSDateTime, EWSTimeZone, UTC, UTC_NOW
76
from datetime import datetime, timedelta
77
78
# Create Exchange datetimes
79
meeting_time = EWSDateTime.from_datetime(
80
datetime(2024, 12, 15, 14, 30, 0)
81
)
82
83
# Work with timezones
84
eastern = EWSTimeZone.from_pytz(pytz.timezone('US/Eastern'))
85
pacific = EWSTimeZone.from_pytz(pytz.timezone('US/Pacific'))
86
87
eastern_time = meeting_time.to_timezone(eastern)
88
pacific_time = meeting_time.to_timezone(pacific)
89
90
# Use UTC
91
utc_now = UTC_NOW()
92
utc_meeting = EWSDateTime.now(UTC)
93
94
# Calendar item with timezone
95
appointment = CalendarItem(
96
account=account,
97
subject='Cross-timezone Meeting',
98
start=eastern_time,
99
end=eastern_time + timedelta(hours=1),
100
start_timezone=eastern,
101
end_timezone=eastern
102
)
103
```