class DateTimeFormat
An enumeration-style class that defines constants for specifying date and time format options.
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/fields/date_time_format.py
1 - 11
simple
Purpose
DateTimeFormat serves as a simple enumeration to specify whether a date/time value should include both date and time components, only the date, or only the time. It provides three class-level integer constants (DateTime=0, DateOnly=1, TimeOnly=2) that can be used as format specifiers in date/time processing operations. This pattern is commonly used for configuration or as parameters to formatting functions.
Source Code
class DateTimeFormat:
"""Specifies the date and time format."""
DateTime = 0
"""Specifies that both date and time are included."""
DateOnly = 1
"""Specifies that only the date is included."""
TimeOnly = 2
"""Specifies that only time is included."""
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
- | - |
Parameter Details
bases: This class does not define an __init__ method and is not intended to be instantiated. It serves as a namespace for constants. The 'bases' mentioned in the docstring refers to the implicit inheritance from object, which is the default base class in Python.
Return Value
This class is not meant to be instantiated. It serves as a container for class-level constants. Users should access the constants directly via the class name (e.g., DateTimeFormat.DateTime) rather than creating instances. If instantiated, it would return a DateTimeFormat object, but this would serve no practical purpose.
Class Interface
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
DateTime |
int | Constant value (0) indicating that both date and time components should be included in the format | class |
DateOnly |
int | Constant value (1) indicating that only the date component should be included in the format | class |
TimeOnly |
int | Constant value (2) indicating that only the time component should be included in the format | class |
Usage Example
# Access the format constants directly from the class
format_type = DateTimeFormat.DateTime # Returns 0
date_only = DateTimeFormat.DateOnly # Returns 1
time_only = DateTimeFormat.TimeOnly # Returns 2
# Typical usage in a function
def format_datetime(value, format_type):
if format_type == DateTimeFormat.DateTime:
return value.strftime('%Y-%m-%d %H:%M:%S')
elif format_type == DateTimeFormat.DateOnly:
return value.strftime('%Y-%m-%d')
elif format_type == DateTimeFormat.TimeOnly:
return value.strftime('%H:%M:%S')
# Using in conditional logic
from datetime import datetime
now = datetime.now()
formatted = format_datetime(now, DateTimeFormat.DateOnly)
print(formatted) # Outputs: 2024-01-15
Best Practices
- Do not instantiate this class - use the class-level constants directly (e.g., DateTimeFormat.DateTime)
- This class follows an enumeration pattern but predates Python's enum.Enum. Consider using enum.IntEnum for new code for better type safety and IDE support
- The integer values (0, 1, 2) can be used in comparisons and switch-like logic
- These constants are immutable and should not be modified at runtime
- When using in function parameters, consider type hints like 'format_type: int' or create a custom type alias
- This pattern is thread-safe as it only contains class-level constants with no mutable state
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class DateTimeFieldFormatType 88.7% similar
-
class UrlFieldFormatType 55.7% similar
-
function format_datetime 52.1% similar
-
class FieldDateTime 51.7% similar
-
class DateTimeTimeZone 49.2% similar