This commit is contained in:
Ladebeze66 2025-03-27 14:54:06 +01:00
parent 1c4375e070
commit 33d5848f7c
2885 changed files with 201200 additions and 899 deletions

View File

@ -0,0 +1,65 @@
# SpecStory Artifacts Directory
This directory is automatically created and maintained by the SpecStory extension to preserve your Cursor composer and chat history.
## What's Here?
- `.specstory/history`: Contains markdown files of your AI coding sessions
- Each file represents a separate chat or composer session
- Files are automatically updated as you work
- `.specstory/cursor_rules_backups`: Contains backups of the `.cursor/rules/derived-cursor-rules.mdc` file
- Backups are automatically created each time the `.cursor/rules/derived-cursor-rules.mdc` file is updated
- You can enable/disable the Cursor Rules feature in the SpecStory settings, it is disabled by default
## Valuable Uses
- Capture: Keep your context window up-to-date when starting new Chat/Composer sessions via @ references
- Search: For previous prompts and code snippets
- Learn: Meta-analyze your patterns and learn from your past experiences
- Derive: Keep Cursor on course with your past decisions by automatically deriving Cursor rules from your AI interactions
## Version Control
We recommend keeping this directory under version control to maintain a history of your AI interactions. However, if you prefer not to version these files, you can exclude them by adding this to your `.gitignore`:
```
.specstory
```
We recommend not keeping the `.specstory/cursor_rules_backups` directory under version control if you are already using git to version the `.cursor/rules` directory, and committing regularly. You can exclude it by adding this to your `.gitignore`:
```
.specstory/cursor_rules_backups
```
## Searching Your Codebase
When searching your codebase in Cursor, search results may include your previous AI coding interactions. To focus solely on your actual code files, you can exclude the AI interaction history from search results.
To exclude AI interaction history:
1. Open the "Find in Files" search in Cursor (Cmd/Ctrl + Shift + F)
2. Navigate to the "files to exclude" section
3. Add the following pattern:
```
.specstory/*
```
This will ensure your searches only return results from your working codebase files.
## Notes
- Auto-save only works when Cursor/sqlite flushes data to disk. This results in a small delay after the AI response is complete before SpecStory can save the history.
- Auto-save does not yet work on remote WSL workspaces.
## Settings
You can control auto-saving behavior in Cursor:
1. Open Cursor → Settings → VS Code Settings (Cmd/Ctrl + ,)
2. Search for "SpecStory"
3. Find "Auto Save" setting to enable/disable
Auto-save occurs when changes are detected in Cursor's sqlite database, or every 2 minutes as a safety net.

File diff suppressed because it is too large Load Diff

View File

@ -1,180 +0,0 @@
# Système d'Agents Ollama
Ce projet fournit un framework pour créer et gérer des agents d'IA basés sur les modèles Ollama. Il permet d'utiliser facilement différents types de modèles (texte, code, vision, embeddings) et de paramétrer leur comportement.
## Fonctionnalités
- Utilisation de l'API Ollama pour interagir avec les modèles locaux
- Classes spécialisées pour différents types de modèles :
- Génération de texte (TextGenerationModel)
- Multimodal avec support d'images (MultiModalModel)
- Embeddings (EmbeddingModel)
- Système de gestion d'agents pour organiser plusieurs modèles
- Contrôle précis des paramètres de génération (température, top_p, top_k, etc.)
- Support du streaming pour des réponses progressives
- Support de l'API chat pour des conversations contextuelles
## Prérequis
- Python 3.8+
- [Ollama](https://ollama.ai/) installé et en cours d'exécution
- Modèles Ollama téléchargés (voir [ollama.ai/library](https://ollama.ai/library))
- Module `requests` de Python
## Installation
1. Clonez ce dépôt :
```
git clone <url-du-depot>
cd AIagent
```
2. Installez les dépendances :
```
pip install requests
```
3. Assurez-vous qu'Ollama est en cours d'exécution sur votre serveur.
## Structure du projet
```
AIagent/
├── src/
│ ├── ollama_models.py # Classes de base et spécialisées pour les modèles
│ └── exemple_usage.py # Exemples d'utilisation
└── README.md
```
## Utilisation
### Configuration de base
```python
from src.ollama_models import AgentSystem, create_text_agent
# Créer le système d'agents
system = AgentSystem()
# Ajouter un agent de génération de texte
create_text_agent(
agent_id="assistant",
model_name="llama3.3:70b-instruct-q8_0",
system=system,
temperature=0.7,
top_p=0.9
)
# Récupérer l'agent et l'utiliser
agent = system.get_agent("assistant")
response = agent.generate("Explique-moi le fonctionnement des grands modèles de langage.")
```
### Génération de texte
```python
# Créer directement un modèle sans système d'agents
from src.ollama_models import TextGenerationModel
model = TextGenerationModel(
model_name="llama3.3:70b-instruct-q8_0",
temperature=0.7
)
# Générer du texte
response = model.generate("Écris un poème sur l'intelligence artificielle.")
```
### Chat conversationnel
```python
# Utiliser l'API chat
messages = [
{"role": "system", "content": "Tu es un assistant IA serviable et concis."},
{"role": "user", "content": "Quels sont les avantages des modèles Ollama?"}
]
response = model.chat(messages)
# Continuer la conversation
messages.append({"role": "assistant", "content": response})
messages.append({"role": "user", "content": "Et quels sont les inconvénients?"})
response = model.chat(messages)
```
### Utilisation de modèles multimodaux
```python
from src.ollama_models import MultiModalModel
vision_model = MultiModalModel(
model_name="llava:34b-v1.6-fp16",
temperature=0.8
)
# Analyser une image
response = vision_model.analyze_image(
prompt="Décris cette image en détail.",
image_path="chemin/vers/image.jpg"
)
```
### Génération d'embeddings
```python
from src.ollama_models import EmbeddingModel
embedding_model = EmbeddingModel(
model_name="nomic-embed-text:137m-v1.5-fp16"
)
# Obtenir des embeddings
embeddings = embedding_model.get_embeddings([
"Premier texte à encoder",
"Second texte à encoder"
])
```
## Paramètres des modèles
Voici les principaux paramètres que vous pouvez ajuster :
- `temperature` (0.0-1.0) : Contrôle la créativité/aléatoire du modèle
- `top_p` (0.0-1.0) : Échantillonnage du nucleus, affecte la diversité
- `top_k` (1-100) : Limite les tokens considérés aux k plus probables
- `num_ctx` (int) : Taille du contexte en tokens
- `num_predict` (int) : Nombre maximum de tokens à générer
- `repeat_penalty` (float) : Pénalité pour les répétitions
Pour mettre à jour les paramètres d'un modèle :
```python
model.update_options(
temperature=0.2,
top_p=0.8,
num_predict=500
)
```
## Exemple complet
Consultez le fichier `src/exemple_usage.py` pour un exemple complet d'utilisation.
Pour exécuter l'exemple :
```
python src/exemple_usage.py
```
## Modèles supportés
Ce système prend en charge tous les modèles disponibles via Ollama. Voici quelques exemples :
- Modèles de texte : llama3.3, qwen2.5, etc.
- Modèles de vision : llava, llama3.2-vision, etc.
- Modèles d'embeddings : nomic-embed-text, etc.
## Licence
[MIT](LICENSE)

View File

@ -1 +0,0 @@
requests>=2.31.0

View File

@ -1,259 +0,0 @@
"""
Exemple d'utilisation du système d'agents Ollama.
Ce script montre comment configurer et utiliser différents types d'agents
basés sur les modèles Ollama disponibles sur votre serveur.
"""
from ollama_models import (
AgentSystem,
create_text_agent,
create_multimodal_agent,
create_embedding_agent,
OllamaModel
)
def configurer_systeme_agents():
"""Configure et retourne un système d'agents avec différents modèles."""
# Créer le système d'agents
system = AgentSystem()
# Ajouter un agent de génération de texte avec Llama 3.3
create_text_agent(
agent_id="assistant_texte",
model_name="llama3.3:70b-instruct-q8_0",
system=system,
temperature=0.7,
top_p=0.9,
num_ctx=4096,
stream=True
)
# Ajouter un agent spécialisé pour le code avec température basse
create_text_agent(
agent_id="assistant_code",
model_name="qwen2.5-coder:32b-instruct-fp16",
system=system,
temperature=0.2, # Température basse pour des réponses plus déterministes
top_p=0.8,
top_k=40,
num_ctx=8192, # Contexte plus large pour les tâches de code
stream=True
)
# Ajouter un agent multimodal pour l'analyse d'images
create_multimodal_agent(
agent_id="assistant_vision",
model_name="llama3.2-vision:90b-instruct-q8_0",
system=system,
temperature=0.8,
num_ctx=8192,
stream=False # Désactiver le streaming pour les modèles vision
)
# Ajouter un agent d'embedding
create_embedding_agent(
agent_id="agent_embedding",
model_name="nomic-embed-text:137m-v1.5-fp16",
system=system,
stream=False # Les embeddings ne nécessitent pas de streaming
)
return system
def exemple_generation_texte(system):
"""Exemple d'utilisation d'un agent de génération de texte."""
print("\n=== EXEMPLE GÉNÉRATION DE TEXTE ===")
# Récupérer l'agent
agent = system.get_agent("assistant_texte")
if agent:
# Générer du texte
prompt = "Explique-moi les avantages des architectures basées sur les transformers en IA."
print(f"Demande: {prompt}")
print("\nRéponse:")
# Utiliser l'API de génération
agent.generate(prompt)
else:
print("Agent 'assistant_texte' non trouvé.")
def exemple_generation_code(system):
"""Exemple d'utilisation d'un agent spécialisé pour le code."""
print("\n=== EXEMPLE GÉNÉRATION DE CODE ===")
# Récupérer l'agent
agent = system.get_agent("assistant_code")
if agent:
# Modifier temporairement certains paramètres pour cette requête
agent.update_options(temperature=0.1, num_predict=500)
# Générer du code
prompt = "Écris une fonction Python pour calculer le nombre de Fibonacci de manière récursive avec mémoïsation."
print(f"Demande: {prompt}")
print("\nRéponse:")
# Utiliser l'API de génération
agent.generate(prompt)
# Remettre les paramètres par défaut
agent.update_options(temperature=0.2, num_predict=128)
else:
print("Agent 'assistant_code' non trouvé.")
def exemple_chat(system):
"""Exemple d'utilisation de l'API chat."""
print("\n=== EXEMPLE CHAT ===")
# Récupérer l'agent
agent = system.get_agent("assistant_texte")
if agent:
# Créer une conversation
messages = [
{"role": "system", "content": "Tu es un assistant IA serviable, concis et précis."},
{"role": "user", "content": "Quels sont les 3 principaux avantages des modèles Ollama par rapport aux API cloud?"}
]
print("Conversation:")
for msg in messages:
print(f"{msg['role']}: {msg['content']}")
print("\nRéponse:")
# Utiliser l'API chat
response = agent.chat(messages)
# Ajouter la réponse à l'historique
if isinstance(response, str):
messages.append({"role": "assistant", "content": response})
elif isinstance(response, dict) and "message" in response and "content" in response["message"]:
messages.append({"role": "assistant", "content": response["message"]["content"]})
# Continuer la conversation
messages.append({"role": "user", "content": "Et quels sont les inconvénients?"})
print("\nUtilisateur: Et quels sont les inconvénients?")
print("\nRéponse:")
# Continuer avec l'API chat
agent.chat(messages)
else:
print("Agent 'assistant_texte' non trouvé.")
def exemple_embeddings(system):
"""Exemple d'utilisation de l'agent d'embedding."""
print("\n=== EXEMPLE EMBEDDINGS ===")
# Récupérer l'agent
agent = system.get_agent("agent_embedding")
if agent:
# Textes à encoder
textes = [
"Les modèles de langage sont très utiles pour l'IA moderne.",
"Les embeddings permettent de représenter du texte en vecteurs numériques."
]
print("Génération d'embeddings pour les textes suivants:")
for i, texte in enumerate(textes):
print(f"{i+1}. {texte}")
# Obtenir les embeddings
embeddings = agent.get_embeddings(textes)
# Afficher les dimensions des embeddings
if embeddings:
print(f"\nEmbeddings générés avec succès!")
print(f"Nombre d'embeddings: {len(embeddings)}")
print(f"Dimension de chaque embedding: {len(embeddings[0])}")
print(f"Premiers éléments du premier embedding: {embeddings[0][:5]}...")
else:
print("Échec de la génération des embeddings.")
else:
print("Agent 'agent_embedding' non trouvé.")
def exemple_vision(system):
"""Exemple d'utilisation de l'agent de vision (nécessite un fichier image)."""
print("\n=== EXEMPLE VISION (NÉCESSITE UNE IMAGE) ===")
# Récupérer l'agent
agent = system.get_agent("assistant_vision")
if agent:
print("Pour tester l'agent de vision, vous devez fournir un chemin vers une image.")
print("Exemple d'utilisation:")
print("```python")
print("image_path = 'chemin/vers/votre/image.jpg'")
print("prompt = 'Décris cette image en détail.'")
print("response = agent.analyze_image(prompt, image_path)")
print("```")
else:
print("Agent 'assistant_vision' non trouvé.")
def afficher_modeles_disponibles():
"""Affiche la liste des modèles Ollama disponibles."""
print("\n=== MODÈLES OLLAMA DISPONIBLES ===")
# Obtenir la liste des modèles
models = OllamaModel.list_models()
if "error" in models:
print(f"Erreur lors de la récupération des modèles: {models['error']}")
return
# Afficher les modèles
if "models" in models:
for i, model in enumerate(models["models"]):
print(f"{i+1}. {model['name']} ({model['size']})")
else:
print("Aucun modèle trouvé ou format de réponse inattendu.")
def main():
"""Fonction principale."""
print("=== SYSTÈME D'AGENTS OLLAMA ===")
# Afficher les modèles disponibles
afficher_modeles_disponibles()
# Configurer le système d'agents
system = configurer_systeme_agents()
# Lister les agents configurés
agents = system.list_agents()
print("\n=== AGENTS CONFIGURÉS ===")
for agent_id, model_name in agents.items():
print(f"Agent: {agent_id}, Modèle: {model_name}")
# Exemples d'utilisation
try:
exemple_generation_texte(system)
exemple_generation_code(system)
exemple_chat(system)
exemple_embeddings(system)
exemple_vision(system)
except Exception as e:
print(f"\nUne erreur s'est produite: {str(e)}")
print("\n=== FIN DES EXEMPLES ===")
if __name__ == "__main__":
main()

View File

@ -1,442 +0,0 @@
"""
Module de gestion des modèles Ollama pour systèmes d'agents IA.
Ce module fournit des classes pour interagir avec différents modèles Ollama
et paramétrer leurs comportements.
"""
import json
import requests
from typing import List, Dict, Any, Union, Optional
class OllamaModel:
"""Classe de base pour interagir avec les modèles Ollama."""
def __init__(self,
model_name: str,
base_url: str = "http://localhost:11434",
temperature: float = 0.7,
top_p: float = 0.9,
top_k: int = 40,
num_ctx: int = 4096,
num_predict: int = 128,
repeat_penalty: float = 1.1,
stream: bool = True):
"""
Initialise un modèle Ollama.
Args:
model_name: Nom du modèle Ollama à utiliser
base_url: URL de base de l'API Ollama
temperature: Température pour la génération (0.0 à 1.0)
top_p: Paramètre top_p pour la génération (0.0 à 1.0)
top_k: Paramètre top_k pour la génération (1 à 100)
num_ctx: Taille du contexte en tokens
num_predict: Nombre maximum de tokens à générer
repeat_penalty: Pénalité pour les répétitions
stream: Si True, utilise le streaming pour les réponses
"""
self.model_name = model_name
self.base_url = base_url
self.api_url = f"{base_url}/api"
# Paramètres du modèle
self.options = {
"temperature": temperature,
"top_p": top_p,
"top_k": top_k,
"num_ctx": num_ctx,
"num_predict": num_predict,
"repeat_penalty": repeat_penalty
}
self.stream = stream
def generate(self, prompt: str) -> Union[str, Dict[str, Any]]:
"""
Génère une réponse à partir d'un prompt.
Args:
prompt: Le texte à soumettre au modèle
Returns:
La réponse du modèle (texte ou dictionnaire complet)
"""
url = f"{self.api_url}/generate"
payload = {
"model": self.model_name,
"prompt": prompt,
"stream": self.stream,
"options": self.options
}
try:
if self.stream:
return self._stream_response(url, payload)
else:
response = requests.post(url, json=payload)
response.raise_for_status()
return response.json()
except Exception as e:
return f"Erreur lors de la génération: {str(e)}"
def _stream_response(self, url: str, payload: Dict[str, Any]) -> str:
"""
Gère le streaming de la réponse et renvoie le texte complet.
Args:
url: URL de l'API
payload: Données pour la requête
Returns:
La réponse complète sous forme de texte
"""
full_response = ""
try:
with requests.post(url, json=payload, stream=True) as response:
response.raise_for_status()
for line in response.iter_lines():
if line:
chunk = json.loads(line)
if "response" in chunk:
print(chunk["response"], end="", flush=True)
full_response += chunk["response"]
if chunk.get("done", False):
break
print() # Nouvelle ligne après la réponse
return full_response
except Exception as e:
return f"Erreur lors du streaming: {str(e)}"
def chat(self, messages: List[Dict[str, str]]) -> Union[str, Dict[str, Any]]:
"""
Utilise l'API chat pour communiquer avec le modèle.
Args:
messages: Liste de messages au format [{"role": "user", "content": "..."}]
Returns:
La réponse du modèle
"""
url = f"{self.api_url}/chat"
payload = {
"model": self.model_name,
"messages": messages,
"stream": self.stream,
"options": self.options
}
try:
if self.stream:
return self._stream_chat_response(url, payload)
else:
response = requests.post(url, json=payload)
response.raise_for_status()
return response.json()
except Exception as e:
return f"Erreur lors du chat: {str(e)}"
def _stream_chat_response(self, url: str, payload: Dict[str, Any]) -> str:
"""
Gère le streaming de la réponse de chat et renvoie le texte complet.
Args:
url: URL de l'API
payload: Données pour la requête
Returns:
La réponse complète sous forme de texte
"""
full_response = ""
try:
with requests.post(url, json=payload, stream=True) as response:
response.raise_for_status()
for line in response.iter_lines():
if line:
chunk = json.loads(line)
if "message" in chunk and "content" in chunk["message"]:
content = chunk["message"]["content"]
print(content, end="", flush=True)
full_response += content
if chunk.get("done", False):
break
print() # Nouvelle ligne après la réponse
return full_response
except Exception as e:
return f"Erreur lors du streaming du chat: {str(e)}"
def embed(self, input_text: Union[str, List[str]]) -> Dict[str, Any]:
"""
Génère des embeddings pour un texte.
Args:
input_text: Texte ou liste de textes pour lesquels générer des embeddings
Returns:
Réponse contenant les embeddings
"""
url = f"{self.api_url}/embed"
payload = {
"model": self.model_name,
"prompt": input_text
}
try:
response = requests.post(url, json=payload)
response.raise_for_status()
return response.json()
except Exception as e:
return {"error": str(e)}
def update_options(self, **kwargs):
"""
Met à jour les options du modèle.
Args:
**kwargs: Paires clé-valeur de paramètres à mettre à jour
"""
for key, value in kwargs.items():
if key in self.options:
self.options[key] = value
@staticmethod
def list_models() -> Dict[str, Any]:
"""
Liste tous les modèles disponibles.
Returns:
Dictionnaire contenant les informations sur les modèles disponibles
"""
try:
response = requests.get("http://localhost:11434/api/tags")
response.raise_for_status()
return response.json()
except Exception as e:
return {"error": str(e)}
class TextGenerationModel(OllamaModel):
"""Classe spécialisée pour les modèles de génération de texte."""
def __init__(self, model_name: str, **kwargs):
super().__init__(model_name, **kwargs)
def create_completion(self, prompt: str, max_tokens: Optional[int] = None) -> str:
"""
Crée une complétion de texte simple.
Args:
prompt: Le texte à compléter
max_tokens: Nombre maximum de tokens à générer (remplace num_predict si spécifié)
Returns:
Le texte généré
"""
# Sauvegarde des options existantes
original_options = self.options.copy()
if max_tokens is not None:
self.options["num_predict"] = max_tokens
result = self.generate(prompt)
# Restauration des options d'origine
self.options = original_options
if isinstance(result, str):
return result
elif isinstance(result, dict) and "response" in result:
return result["response"]
else:
return str(result)
class MultiModalModel(OllamaModel):
"""Classe pour les modèles multimodaux prenant en charge les images."""
def __init__(self, model_name: str, **kwargs):
super().__init__(model_name, **kwargs)
def analyze_image(self, prompt: str, image_path: str) -> str:
"""
Analyse une image et répond à une question à son sujet.
Args:
prompt: La question ou instruction concernant l'image
image_path: Chemin vers le fichier image à analyser
Returns:
La réponse du modèle à propos de l'image
"""
import base64
# Lire et encoder l'image en base64
try:
with open(image_path, "rb") as image_file:
image_data = base64.b64encode(image_file.read()).decode("utf-8")
except Exception as e:
return f"Erreur lors de la lecture de l'image: {str(e)}"
url = f"{self.api_url}/generate"
payload = {
"model": self.model_name,
"prompt": prompt,
"stream": self.stream,
"options": self.options,
"images": [image_data]
}
try:
if self.stream:
return self._stream_response(url, payload)
else:
response = requests.post(url, json=payload)
response.raise_for_status()
result = response.json()
return result.get("response", str(result))
except Exception as e:
return f"Erreur lors de l'analyse de l'image: {str(e)}"
class EmbeddingModel(OllamaModel):
"""Classe spécialisée pour les modèles d'embedding."""
def __init__(self, model_name: str, **kwargs):
super().__init__(model_name, **kwargs)
def get_embeddings(self, texts: Union[str, List[str]]) -> List[List[float]]:
"""
Obtient des embeddings pour un ou plusieurs textes.
Args:
texts: Texte unique ou liste de textes
Returns:
Liste des vecteurs d'embedding
"""
result = self.embed(texts)
if "error" in result:
print(f"Erreur: {result['error']}")
return []
if "embedding" in result:
# Cas d'un seul texte
return [result["embedding"]]
elif "embeddings" in result:
# Cas d'une liste de textes
return result["embeddings"]
else:
print(f"Format de réponse inattendu: {result}")
return []
class AgentSystem:
"""Système de gestion d'agents basés sur les modèles Ollama."""
def __init__(self):
self.agents = {}
def add_agent(self, agent_id: str, model: OllamaModel):
"""
Ajoute un agent au système.
Args:
agent_id: Identifiant unique pour l'agent
model: Modèle Ollama à utiliser pour cet agent
"""
self.agents[agent_id] = model
def get_agent(self, agent_id: str) -> Optional[OllamaModel]:
"""
Récupère un agent par son ID.
Args:
agent_id: Identifiant de l'agent
Returns:
L'instance du modèle associée à l'agent, ou None si non trouvé
"""
return self.agents.get(agent_id)
def remove_agent(self, agent_id: str) -> bool:
"""
Supprime un agent du système.
Args:
agent_id: Identifiant de l'agent à supprimer
Returns:
True si l'agent a été supprimé, False sinon
"""
if agent_id in self.agents:
del self.agents[agent_id]
return True
return False
def list_agents(self) -> Dict[str, str]:
"""
Liste tous les agents disponibles.
Returns:
Dictionnaire avec ID d'agent comme clé et nom du modèle comme valeur
"""
return {agent_id: agent.model_name for agent_id, agent in self.agents.items()}
# Fonctions auxiliaires pour créer facilement des agents
def create_text_agent(agent_id: str, model_name: str, system: AgentSystem, **kwargs) -> OllamaModel:
"""
Crée un agent de génération de texte et l'ajoute au système.
Args:
agent_id: Identifiant de l'agent
model_name: Nom du modèle Ollama à utiliser
system: Système d'agents auquel ajouter l'agent
**kwargs: Paramètres supplémentaires pour configurer le modèle
Returns:
L'instance du modèle créée
"""
agent = TextGenerationModel(model_name, **kwargs)
system.add_agent(agent_id, agent)
return agent
def create_multimodal_agent(agent_id: str, model_name: str, system: AgentSystem, **kwargs) -> OllamaModel:
"""
Crée un agent multimodal et l'ajoute au système.
Args:
agent_id: Identifiant de l'agent
model_name: Nom du modèle Ollama à utiliser
system: Système d'agents auquel ajouter l'agent
**kwargs: Paramètres supplémentaires pour configurer le modèle
Returns:
L'instance du modèle créée
"""
agent = MultiModalModel(model_name, **kwargs)
system.add_agent(agent_id, agent)
return agent
def create_embedding_agent(agent_id: str, model_name: str, system: AgentSystem, **kwargs) -> OllamaModel:
"""
Crée un agent d'embedding et l'ajoute au système.
Args:
agent_id: Identifiant de l'agent
model_name: Nom du modèle Ollama à utiliser
system: Système d'agents auquel ajouter l'agent
**kwargs: Paramètres supplémentaires pour configurer le modèle
Returns:
L'instance du modèle créée
"""
agent = EmbeddingModel(model_name, **kwargs)
system.add_agent(agent_id, agent)
return agent

19
list_models_h100.txt Normal file
View File

@ -0,0 +1,19 @@
mistral:latest f974a74358d6 4.1 GB 11 minutes ago
hf.co/mradermacher/EraX-multilingual-Rerank-3B-V2.0-GGUF:F16 d91cc1ed0d2f 7.2 GB 7 weeks ago
cline_deepseek:latest 0bf9bdf65e71 74 GB 7 weeks ago
deepseek-r1:70b-llama-distill-q8_0 cc1ba146470d 74 GB 7 weeks ago
llava:34b-v1.6-fp16 89c50af82d5e 69 GB 2 months ago
llama3.2-vision:90b-instruct-q8_0 e65e1af5e383 95 GB 2 months ago
linux6200/bge-reranker-v2-m3:latest abf5c6d8bc56 1.2 GB 2 months ago
nomic-embed-text:137m-v1.5-fp16 0a109f422b47 274 MB 2 months ago
cline_qwen_fp16:latest 82800e2e3c2e 65 GB 2 months ago
cline_qwen_32k_Q8:latest 9a1b09922a53 34 GB 2 months ago
llama3.3:70b-instruct-q6_K 992c05c90cd8 57 GB 2 months ago
llama3.3:70b-instruct-q8_0 d5b5e1b84868 74 GB 2 months ago
cline_qwen_128k_Q8:latest 8675a48afcaf 34 GB 2 months ago
hf.co/unsloth/Qwen2.5-Coder-32B-Instruct-128K-GGUF:Q8_0 b249cb8f57ca 34 GB 2 months ago
hf.co/bartowski/Rombos-Coder-V2.5-Qwen-32b-GGUF:Q8_0 9a943e73021c 34 GB 2 months ago
qwen2.5-coder:32b-instruct-fp16 5955f7d140cd 65 GB 2 months ago
qwen2.5-coder:32b-base-q8_0 2888b8e6d633 34 GB 2 months ago
qwen2.5:72b-instruct-q8_0 23f2cb48bb9a 77 GB 2 months ago
qwen2.5:72b-instruct-q6_K 5f611a49224d 64 GB 2 months ago

3
log_index.md Normal file
View File

@ -0,0 +1,3 @@
- **2025-03-25 15:54:16** | `d0212b27` | `mistral:latest` | temperature=0.1 | [Voir le log](logs/mistral_2025-03-25_15-54-16_d0212b27.md)
- **2025-03-25 15:54:18** | `386c8fc4` | `mistral:latest` | temperature=0.5 | [Voir le log](logs/mistral_2025-03-25_15-54-18_386c8fc4.md)
- **2025-03-25 15:54:20** | `31471718` | `mistral:latest` | temperature=1.0 | [Voir le log](logs/mistral_2025-03-25_15-54-20_31471718.md)

View File

@ -0,0 +1,26 @@
# Résultat génération Mistral
** Test ID :** d0212b27
** Date :** 2025-03-25 15:54:16
** Modèle :** mistral:latest
** Prompt :** Explique-moi ce qu'et l'intelligence artificielle.
** Combinaison :** temperature=0.1
---
## Paramètres complets
- Temperature : 0.1
- Top P : 0.9
- Top K : 50
- Repeat Penalty : 1.1
- Num Predict : 512
- Stop :
- Seed : *Aucun*
- Stream : *Aucun*
---
## Réponse du modèle
L'intelligence artificielle (IA) est une branche des sciences qui étudie le fonctionnement de l'intelligence humaine et tente d'imiter ces processus en créant des systèmes informatiques capables de résoudre des problèmes, d'apprendre et de prendre des décisions. Cela comprend également la conception d'agents intelligents qui peuvent interagir avec leur environnement pour atteindre un objectif donné. L'IA couvre une variété de domaines tels que le traitement du langage naturel, l'apprentissage automatique, les réseaux neuronaux, la reconnaissance de formes et des images, ainsi que d'autres domaines tels que l'optimisation, la robotique, etc. Les applications de l'IA sont omniprésentes dans notre vie quotidienne et ils vont de simplement automatiser certains processus à l'aide de logiciels intelligents comme les moteurs de recherche ou les systèmes d'exploitation, jusqu'à des applications plus avancées telles que la reconnaissance vocale, le jeu, la vision artificielle, etc.

View File

@ -0,0 +1,30 @@
# Résultat génération Mistral
** Test ID :** 386c8fc4
** Date :** 2025-03-25 15:54:18
** Modèle :** mistral:latest
** Prompt :** Explique-moi ce qu'et l'intelligence artificielle.
** Combinaison :** temperature=0.5
---
## Paramètres complets
- Temperature : 0.5
- Top P : 0.9
- Top K : 50
- Repeat Penalty : 1.1
- Num Predict : 512
- Stop :
- Seed : *Aucun*
- Stream : *Aucun*
---
## Réponse du modèle
L'Intelligence Artificielle (IA) est une discipline de l'informatique qui concerne le développement de systèmes capables d'effectuer des tâches qui impliquent un certain niveau de raisonnement et d'apprentissage, en s'appuyant sur les connaissances humaines pour résoudre des problèmes, prendre des décisions ou apprendre à se développer sans nécessiter une intervention directe humaine.
Cela implique l'utilisation de différentes techniques informatiques et mathématiques, tels que la programmation logique, les algorithmes d'apprentissage automatique, les réseaux neuronaux artificiels, etc., pour créer des systèmes intelligents qui peuvent comprendre des langages naturels, reconnaître des objets ou des personnes dans une image, apprendre à jouer aux jeux, prendre des décisions et effectuer d'autres tâches semblables à celles des humains.
Cependant, il est important de souligner que l'IA n'a pas atteint le niveau de compréhension ou de capacité cognitive du cerveau humain actuellement. Les systèmes IA existants sont capables d'effectuer des tâches spécifiques avec un certain degré de réussite, mais ils ne possèdent pas une conscience ou une personnalité réelle, et ils peuvent avoir des limites importantes dans leur capacité à comprendre le contexte, les émotions et les nuances des langages naturels.

View File

@ -0,0 +1,30 @@
# Résultat génération Mistral
** Test ID :** 31471718
** Date :** 2025-03-25 15:54:20
** Modèle :** mistral:latest
** Prompt :** Explique-moi ce qu'et l'intelligence artificielle.
** Combinaison :** temperature=1.0
---
## Paramètres complets
- Temperature : 1.0
- Top P : 0.9
- Top K : 50
- Repeat Penalty : 1.1
- Num Predict : 512
- Stop :
- Seed : *Aucun*
- Stream : *Aucun*
---
## Réponse du modèle
L'Intelligence Artificielle, ou IA pour les abrégations, est un domaine de recherche qui étudie la conception de systèmes intelligents capables d'effectuer des tâches habituellement associées à l'intelligence humaine, tels que le raisonnement, le dialogue, la perception et la déduction. Cela comprend la simulation de comportements intelligents à partir de données ou algorithmes spécifiques conçus par les humains. Les objectifs de l'IA sont de créer des systèmes qui sont plus efficaces que les humains dans certaines tâches et de mieux nous aider à comprendre comment fonctionne notre intelligence.
Il y a plusieurs sous-domaines importants dans l'IA, notamment le Machine Learning (apprentissage automatique), la Robotique (construire des robots autonomes), les Systèmes d'Expertise Artificielle (SAE) et la Vision Artificielle. Chaque sous-domaine a des applications pratiques telles que l'analyse de données, le traitement automatique du langage naturel, la reconnaissance de parole, la reconnaissance faciale, la robotique industrielle, etc.
En résumé, l'IA est une branche de la science qui vise à créer des systèmes intelligents artificiels capables de répondre aux besoins humains en les rendant plus efficaces et adaptés pour accomplir certaines tâches.

22
test_ollama_mistral.py Normal file
View File

@ -0,0 +1,22 @@
import requests
#adresse du serveur ollama
OLLAMA_URL = "http://217.182.105.173:11434/api/generate"
# Corps de la requête
payload = {
"model" : "mistral:latest",
"prompt": "Explique-moi ce qu'et l'intelligence artificielle.",
"stream": False #réponse d'un seul bloc
}
# Envoi de la requête à ollama
response = requests.post(OLLAMA_URL, json=payload)
# Affichage du résultat
if response.ok:
print("Réponse de modèle :\n")
print(response.json()["response"])
else:
print("Erreur :", response.status_code)
print(response.text)

81
test_ollama_mistral2.py Normal file
View File

@ -0,0 +1,81 @@
import requests
import os
from datetime import datetime
import uuid #pour générer un identifiant unique
from itertools import product #pour générer toutes les combinaisons de paramètres
#=== Configuration ===
model_name = "mistral:latest"
prompt = "Explique-moi ce qu'et l'intelligence artificielle."
#Paramètres par défaut
base_params = {
"model": model_name,
"prompt": prompt,
"temperature": 0.5, # Créativité (0.1 = précis, 1.5 = créatif)
"top_p": 0.9, # Probabilité cumulée (1.0 = complet)
"top_k": 50, # Top-k tokens à considérer (0 = désactivé)
"repeat_penalty": 1.1, # Pénalise les répétitions (>1 = punit)
"num_predict": 512, # Max tokens générés
"stop": [], # Liste de tokens d'arrêt (optionnel)
"seed": None, # Pour reproduction des résultats
"stream": False
}
#Paramètres à faire varier
parameters_to_test = {
"temperature": [0.1, 0.5, 1.0],
#"top_p": [0.5, 0.9, 1.0],
#"top_k": [10, 50, 100],
#"repeat_penalty": [1.0, 1.1, 1.2],
}
#Préparation des combinaisons
varying_keys = list(parameters_to_test.keys())
combinations = list(product(*[parameters_to_test[k] for k in varying_keys]))
#Création des dossiers de logs
os.makedirs("logs", exist_ok=True)
index_path = "log_index.md"
# Boucle de test
for combo in combinations:
test_params = base_params.copy()
combo_desc = []
for i, key in enumerate(varying_keys):
test_params[key] = combo[i]
combo_desc.append(f"{key}={combo[i]}")
#Requête à ollama
response = requests.post("http://217.182.105.173:11434/api/generate", json=test_params)
result = response.json()["response"] if response.ok else f"Erreur: {response.text}"
#enregistrement des résultats
now = datetime.now()
now_str = now.strftime("%Y-%m-%d_%H-%M-%S")
uid = str(uuid.uuid4())[:8] #générer un identifiant unique
log_name = f"mistral_{now_str}_{uid}.md" #nom du fichier de log
# === Fichier principal .md (réponse) ===
log_path = os.path.join("logs", log_name)
# Sauvegarde Markdown
with open(log_path, "w", encoding="utf-8") as f:
f.write(f"# Résultat génération Mistral\n\n")
f.write(f"** Test ID :** {uid}\n")
f.write(f"** Date :** {now.strftime('%Y-%m-%d %H:%M:%S')}\n")
f.write(f"** Modèle :** {model_name}\n")
f.write(f"** Prompt :** {prompt}\n")
f.write(f"** Combinaison :** {', '.join(combo_desc)}\n\n")
f.write("---\n\n## Paramètres complets\n\n")
for key in test_params:
if key not in ["model", "prompt"]:
val = test_params[key]
val = " / ".join(val) if isinstance(val, list) else val or "*Aucun*"
f.write(f"- {key.replace('_', ' ').title()} : {val}\n")
f.write("\n---\n\n## Réponse du modèle\n\n")
f.write(result.strip() + "\n")
# Mise à jour de lindex
with open(index_path, "a", encoding="utf-8") as idx:
idx.write(f"- **{now.strftime('%Y-%m-%d %H:%M:%S')}** | `{uid}` | ")
idx.write(f"`{model_name}` | {' | '.join(combo_desc)} | [Voir le log](logs/{log_name})\n")

View File

@ -39,10 +39,10 @@ deactivate nondestructive
if [ "${OSTYPE:-}" = "cygwin" ] || [ "${OSTYPE:-}" = "msys" ] ; then
# transform D:\path\to\venv to /d/path/to/venv on MSYS
# and to /cygdrive/d/path/to/venv on Cygwin
export VIRTUAL_ENV=$(cygpath /home/fgras-ca/AI_agent/AIagent)
export VIRTUAL_ENV=$(cygpath /home/fgras-ca/AI_agent/testcline)
else
# use the path as-is
export VIRTUAL_ENV=/home/fgras-ca/AI_agent/AIagent
export VIRTUAL_ENV=/home/fgras-ca/AI_agent/testcline
fi
_OLD_VIRTUAL_PATH="$PATH"
@ -59,9 +59,9 @@ fi
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
_OLD_VIRTUAL_PS1="${PS1:-}"
PS1='(AIagent) '"${PS1:-}"
PS1='(testcline) '"${PS1:-}"
export PS1
VIRTUAL_ENV_PROMPT='(AIagent) '
VIRTUAL_ENV_PROMPT='(testcline) '
export VIRTUAL_ENV_PROMPT
fi

View File

@ -9,7 +9,7 @@ alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PA
# Unset irrelevant variables.
deactivate nondestructive
setenv VIRTUAL_ENV /home/fgras-ca/AI_agent/AIagent
setenv VIRTUAL_ENV /home/fgras-ca/AI_agent/testcline
set _OLD_VIRTUAL_PATH="$PATH"
setenv PATH "$VIRTUAL_ENV/"bin":$PATH"
@ -18,8 +18,8 @@ setenv PATH "$VIRTUAL_ENV/"bin":$PATH"
set _OLD_VIRTUAL_PROMPT="$prompt"
if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then
set prompt = '(AIagent) '"$prompt"
setenv VIRTUAL_ENV_PROMPT '(AIagent) '
set prompt = '(testcline) '"$prompt"
setenv VIRTUAL_ENV_PROMPT '(testcline) '
endif
alias pydoc python -m pydoc

View File

@ -33,7 +33,7 @@ end
# Unset irrelevant variables.
deactivate nondestructive
set -gx VIRTUAL_ENV /home/fgras-ca/AI_agent/AIagent
set -gx VIRTUAL_ENV /home/fgras-ca/AI_agent/testcline
set -gx _OLD_VIRTUAL_PATH $PATH
set -gx PATH "$VIRTUAL_ENV/"bin $PATH
@ -56,7 +56,7 @@ if test -z "$VIRTUAL_ENV_DISABLE_PROMPT"
set -l old_status $status
# Output the venv prompt; color taken from the blue of the Python logo.
printf "%s%s%s" (set_color 4B8BBE) '(AIagent) ' (set_color normal)
printf "%s%s%s" (set_color 4B8BBE) '(testcline) ' (set_color normal)
# Restore the return status of the previous command.
echo "exit $old_status" | .
@ -65,5 +65,5 @@ if test -z "$VIRTUAL_ENV_DISABLE_PROMPT"
end
set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV"
set -gx VIRTUAL_ENV_PROMPT '(AIagent) '
set -gx VIRTUAL_ENV_PROMPT '(testcline) '
end

8
testcline/bin/normalizer Executable file
View File

@ -0,0 +1,8 @@
#!/home/fgras-ca/AI_agent/testcline/bin/python3
# -*- coding: utf-8 -*-
import re
import sys
from charset_normalizer import cli
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(cli.cli_detect())

View File

@ -1,4 +1,4 @@
#!/home/fgras-ca/AI_agent/AIagent/bin/python3
#!/home/fgras-ca/AI_agent/testcline/bin/python3
# -*- coding: utf-8 -*-
import re
import sys

View File

@ -1,4 +1,4 @@
#!/home/fgras-ca/AI_agent/AIagent/bin/python3
#!/home/fgras-ca/AI_agent/testcline/bin/python3
# -*- coding: utf-8 -*-
import re
import sys

View File

@ -1,4 +1,4 @@
#!/home/fgras-ca/AI_agent/AIagent/bin/python3
#!/home/fgras-ca/AI_agent/testcline/bin/python3
# -*- coding: utf-8 -*-
import re
import sys

View File

@ -0,0 +1,44 @@
Zope Public License (ZPL) Version 2.1
A copyright notice accompanies this license document that identifies the
copyright holders.
This license has been certified as open source. It has also been designated as
GPL compatible by the Free Software Foundation (FSF).
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions in source code must retain the accompanying copyright
notice, this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the accompanying copyright
notice, this list of conditions, and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Names of the copyright holders must not be used to endorse or promote
products derived from this software without prior written permission from the
copyright holders.
4. The right to distribute this software or to use it for any purpose does not
give you the right to use Servicemarks (sm) or Trademarks (tm) of the
copyright
holders. Use of them is covered by separate agreement with the copyright
holders.
5. If any files are modified, you must cause the modified files to carry
prominent notices stating that you changed the files and the date of any
change.
Disclaimer
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESSED
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,22 @@
DateTime-5.5.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
DateTime-5.5.dist-info/LICENSE.txt,sha256=PmcdsR32h1FswdtbPWXkqjg-rKPCDOo_r1Og9zNdCjw,2070
DateTime-5.5.dist-info/METADATA,sha256=W1k0PqPJ6SU6QTJAu40JPtHK8XeQRL0GGEpfVGPjWGI,33735
DateTime-5.5.dist-info/RECORD,,
DateTime-5.5.dist-info/REQUESTED,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
DateTime-5.5.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
DateTime-5.5.dist-info/top_level.txt,sha256=iVdUvuV_RIkkMzsnPGNfwojRWvuonInryaK3hA5Hh0o,9
DateTime/DateTime.py,sha256=65LbTcnrCSsDPGoGLVkk7NC3H8Kq-PjkC1fQVR33gE8,71364
DateTime/DateTime.txt,sha256=KZFzxoQItLsar1ZDd2vZN74Y6L4a04H8jXMwqc8KjmY,22487
DateTime/__init__.py,sha256=trlFzEmNkmUpxZT7krPSVDayDK1bRxToccg3CcCF8wg,714
DateTime/__pycache__/DateTime.cpython-312.pyc,,
DateTime/__pycache__/__init__.cpython-312.pyc,,
DateTime/__pycache__/interfaces.cpython-312.pyc,,
DateTime/__pycache__/pytz_support.cpython-312.pyc,,
DateTime/interfaces.py,sha256=n47sexf1eQ6YMdYB_60PgHtSzYIj4FND-RmHFiNpm1E,12187
DateTime/pytz.txt,sha256=9Phns9ESXs9MaOKxXztX6sJ09QczGxsbYoSRSllKUfk,5619
DateTime/pytz_support.py,sha256=inR1SO0X17fp9C2GsRw99S_MhxKiEt5dOV3-TGsBxDI,11853
DateTime/tests/__init__.py,sha256=H7Ixo1xp-8BlJ65u14hk5i_TKEmETyi2FmLMD6H-mpo,683
DateTime/tests/__pycache__/__init__.cpython-312.pyc,,
DateTime/tests/__pycache__/test_datetime.cpython-312.pyc,,
DateTime/tests/julian_testdata.txt,sha256=qxvLvabVB9ayhh5UHBvPhuqW5mRL_lizzbUh6lc3d4I,1397
DateTime/tests/test_datetime.py,sha256=dsrfAqQpz1od1bOVPvSYfZAlduJpJIpc2F_hdN7WRAU,30385

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,785 @@
The DateTime package
====================
Encapsulation of date/time values.
Function Timezones()
--------------------
Returns the list of recognized timezone names:
>>> from DateTime import Timezones
>>> zones = set(Timezones())
Almost all of the standard pytz timezones are included, with the exception
of some commonly-used but ambiguous abbreviations, where historical Zope
usage conflicts with the name used by pytz:
>>> import pytz
>>> [x for x in pytz.all_timezones if x not in zones]
['CET', 'EET', 'EST', 'MET', 'MST', 'WET']
Class DateTime
--------------
DateTime objects represent instants in time and provide interfaces for
controlling its representation without affecting the absolute value of
the object.
DateTime objects may be created from a wide variety of string or
numeric data, or may be computed from other DateTime objects.
DateTimes support the ability to convert their representations to many
major timezones, as well as the ability to create a DateTime object
in the context of a given timezone.
DateTime objects provide partial numerical behavior:
* Two date-time objects can be subtracted to obtain a time, in days
between the two.
* A date-time object and a positive or negative number may be added to
obtain a new date-time object that is the given number of days later
than the input date-time object.
* A positive or negative number and a date-time object may be added to
obtain a new date-time object that is the given number of days later
than the input date-time object.
* A positive or negative number may be subtracted from a date-time
object to obtain a new date-time object that is the given number of
days earlier than the input date-time object.
DateTime objects may be converted to integer, long, or float numbers
of days since January 1, 1901, using the standard int, long, and float
functions (Compatibility Note: int, long and float return the number
of days since 1901 in GMT rather than local machine timezone).
DateTime objects also provide access to their value in a float format
usable with the Python time module, provided that the value of the
object falls in the range of the epoch-based time module.
A DateTime object should be considered immutable; all conversion and numeric
operations return a new DateTime object rather than modify the current object.
A DateTime object always maintains its value as an absolute UTC time,
and is represented in the context of some timezone based on the
arguments used to create the object. A DateTime object's methods
return values based on the timezone context.
Note that in all cases the local machine timezone is used for
representation if no timezone is specified.
Constructor for DateTime
------------------------
DateTime() returns a new date-time object. DateTimes may be created
with from zero to seven arguments:
* If the function is called with no arguments, then the current date/
time is returned, represented in the timezone of the local machine.
* If the function is invoked with a single string argument which is a
recognized timezone name, an object representing the current time is
returned, represented in the specified timezone.
* If the function is invoked with a single string argument
representing a valid date/time, an object representing that date/
time will be returned.
As a general rule, any date-time representation that is recognized
and unambiguous to a resident of North America is acceptable. (The
reason for this qualification is that in North America, a date like:
2/1/1994 is interpreted as February 1, 1994, while in some parts of
the world, it is interpreted as January 2, 1994.) A date/ time
string consists of two components, a date component and an optional
time component, separated by one or more spaces. If the time
component is omitted, 12:00am is assumed.
Any recognized timezone name specified as the final element of the
date/time string will be used for computing the date/time value.
(If you create a DateTime with the string,
"Mar 9, 1997 1:45pm US/Pacific", the value will essentially be the
same as if you had captured time.time() at the specified date and
time on a machine in that timezone). If no timezone is passed, then
the timezone configured on the local machine will be used, **except**
that if the date format matches ISO 8601 ('YYYY-MM-DD'), the instance
will use UTC / GMT+0 as the timezone.
o Returns current date/time, represented in US/Eastern:
>>> from DateTime import DateTime
>>> e = DateTime('US/Eastern')
>>> e.timezone()
'US/Eastern'
o Returns specified time, represented in local machine zone:
>>> x = DateTime('1997/3/9 1:45pm')
>>> x.parts() # doctest: +ELLIPSIS
(1997, 3, 9, 13, 45, ...)
o Specified time in local machine zone, verbose format:
>>> y = DateTime('Mar 9, 1997 13:45:00')
>>> y.parts() # doctest: +ELLIPSIS
(1997, 3, 9, 13, 45, ...)
>>> y == x
True
o Specified time in UTC via ISO 8601 rule:
>>> z = DateTime('2014-03-24')
>>> z.parts() # doctest: +ELLIPSIS
(2014, 3, 24, 0, 0, ...)
>>> z.timezone()
'GMT+0'
The date component consists of year, month, and day values. The
year value must be a one-, two-, or four-digit integer. If a one-
or two-digit year is used, the year is assumed to be in the
twentieth century. The month may an integer, from 1 to 12, a month
name, or a month abbreviation, where a period may optionally follow
the abbreviation. The day must be an integer from 1 to the number of
days in the month. The year, month, and day values may be separated
by periods, hyphens, forward slashes, or spaces. Extra spaces are
permitted around the delimiters. Year, month, and day values may be
given in any order as long as it is possible to distinguish the
components. If all three components are numbers that are less than
13, then a month-day-year ordering is assumed.
The time component consists of hour, minute, and second values
separated by colons. The hour value must be an integer between 0
and 23 inclusively. The minute value must be an integer between 0
and 59 inclusively. The second value may be an integer value
between 0 and 59.999 inclusively. The second value or both the
minute and second values may be omitted. The time may be followed
by am or pm in upper or lower case, in which case a 12-hour clock is
assumed.
* If the DateTime function is invoked with a single numeric argument,
the number is assumed to be either a floating point value such as
that returned by time.time(), or a number of days after January 1,
1901 00:00:00 UTC.
A DateTime object is returned that represents either the GMT value
of the time.time() float represented in the local machine's
timezone, or that number of days after January 1, 1901. Note that
the number of days after 1901 need to be expressed from the
viewpoint of the local machine's timezone. A negative argument will
yield a date-time value before 1901.
* If the function is invoked with two numeric arguments, then the
first is taken to be an integer year and the second argument is
taken to be an offset in days from the beginning of the year, in the
context of the local machine timezone. The date-time value returned
is the given offset number of days from the beginning of the given
year, represented in the timezone of the local machine. The offset
may be positive or negative. Two-digit years are assumed to be in
the twentieth century.
* If the function is invoked with two arguments, the first a float
representing a number of seconds past the epoch in GMT (such as
those returned by time.time()) and the second a string naming a
recognized timezone, a DateTime with a value of that GMT time will
be returned, represented in the given timezone.
>>> import time
>>> t = time.time()
Time t represented as US/Eastern:
>>> now_east = DateTime(t, 'US/Eastern')
Time t represented as US/Pacific:
>>> now_west = DateTime(t, 'US/Pacific')
Only their representations are different:
>>> now_east.equalTo(now_west)
True
* If the function is invoked with three or more numeric arguments,
then the first is taken to be an integer year, the second is taken
to be an integer month, and the third is taken to be an integer day.
If the combination of values is not valid, then a DateTimeError is
raised. One- or two-digit years up to 69 are assumed to be in the
21st century, whereas values 70-99 are assumed to be 20th century.
The fourth, fifth, and sixth arguments are floating point, positive
or negative offsets in units of hours, minutes, and days, and
default to zero if not given. An optional string may be given as
the final argument to indicate timezone (the effect of this is as if
you had taken the value of time.time() at that time on a machine in
the specified timezone).
If a string argument passed to the DateTime constructor cannot be
parsed, it will raise SyntaxError. Invalid date, time, or
timezone components will raise a DateTimeError.
The module function Timezones() will return a list of the timezones
recognized by the DateTime module. Recognition of timezone names is
case-insensitive.
Instance Methods for DateTime (IDateTime interface)
---------------------------------------------------
Conversion and comparison methods
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* ``timeTime()`` returns the date/time as a floating-point number in
UTC, in the format used by the Python time module. Note that it is
possible to create date /time values with DateTime that have no
meaningful value to the time module, and in such cases a
DateTimeError is raised. A DateTime object's value must generally
be between Jan 1, 1970 (or your local machine epoch) and Jan 2038 to
produce a valid time.time() style value.
>>> dt = DateTime('Mar 9, 1997 13:45:00 US/Eastern')
>>> dt.timeTime()
857933100.0
>>> DateTime('2040/01/01 UTC').timeTime()
2208988800.0
>>> DateTime('1900/01/01 UTC').timeTime()
-2208988800.0
* ``toZone(z)`` returns a DateTime with the value as the current
object, represented in the indicated timezone:
>>> dt.toZone('UTC')
DateTime('1997/03/09 18:45:00 UTC')
>>> dt.toZone('UTC').equalTo(dt)
True
* ``isFuture()`` returns true if this object represents a date/time
later than the time of the call:
>>> dt.isFuture()
False
>>> DateTime('Jan 1 3000').isFuture() # not time-machine safe!
True
* ``isPast()`` returns true if this object represents a date/time
earlier than the time of the call:
>>> dt.isPast()
True
>>> DateTime('Jan 1 3000').isPast() # not time-machine safe!
False
* ``isCurrentYear()`` returns true if this object represents a
date/time that falls within the current year, in the context of this
object's timezone representation:
>>> dt.isCurrentYear()
False
>>> DateTime().isCurrentYear()
True
* ``isCurrentMonth()`` returns true if this object represents a
date/time that falls within the current month, in the context of
this object's timezone representation:
>>> dt.isCurrentMonth()
False
>>> DateTime().isCurrentMonth()
True
* ``isCurrentDay()`` returns true if this object represents a
date/time that falls within the current day, in the context of this
object's timezone representation:
>>> dt.isCurrentDay()
False
>>> DateTime().isCurrentDay()
True
* ``isCurrentHour()`` returns true if this object represents a
date/time that falls within the current hour, in the context of this
object's timezone representation:
>>> dt.isCurrentHour()
False
>>> DateTime().isCurrentHour()
True
* ``isCurrentMinute()`` returns true if this object represents a
date/time that falls within the current minute, in the context of
this object's timezone representation:
>>> dt.isCurrentMinute()
False
>>> DateTime().isCurrentMinute()
True
* ``isLeapYear()`` returns true if the current year (in the context of
the object's timezone) is a leap year:
>>> dt.isLeapYear()
False
>>> DateTime('Mar 8 2004').isLeapYear()
True
* ``earliestTime()`` returns a new DateTime object that represents the
earliest possible time (in whole seconds) that still falls within
the current object's day, in the object's timezone context:
>>> dt.earliestTime()
DateTime('1997/03/09 00:00:00 US/Eastern')
* ``latestTime()`` return a new DateTime object that represents the
latest possible time (in whole seconds) that still falls within the
current object's day, in the object's timezone context
>>> dt.latestTime()
DateTime('1997/03/09 23:59:59 US/Eastern')
Component access
~~~~~~~~~~~~~~~~
* ``parts()`` returns a tuple containing the calendar year, month,
day, hour, minute second and timezone of the object
>>> dt.parts() # doctest: +ELLIPSIS
(1997, 3, 9, 13, 45, ... 'US/Eastern')
* ``timezone()`` returns the timezone in which the object is represented:
>>> dt.timezone() in Timezones()
True
* ``tzoffset()`` returns the timezone offset for the objects timezone:
>>> dt.tzoffset()
-18000
* ``year()`` returns the calendar year of the object:
>>> dt.year()
1997
* ``month()`` returns the month of the object as an integer:
>>> dt.month()
3
* ``Month()`` returns the full month name:
>>> dt.Month()
'March'
* ``aMonth()`` returns the abbreviated month name:
>>> dt.aMonth()
'Mar'
* ``pMonth()`` returns the abbreviated (with period) month name:
>>> dt.pMonth()
'Mar.'
* ``day()`` returns the integer day:
>>> dt.day()
9
* ``Day()`` returns the full name of the day of the week:
>>> dt.Day()
'Sunday'
* ``dayOfYear()`` returns the day of the year, in context of the
timezone representation of the object:
>>> dt.dayOfYear()
68
* ``aDay()`` returns the abbreviated name of the day of the week:
>>> dt.aDay()
'Sun'
* ``pDay()`` returns the abbreviated (with period) name of the day of
the week:
>>> dt.pDay()
'Sun.'
* ``dow()`` returns the integer day of the week, where Sunday is 0:
>>> dt.dow()
0
* ``dow_1()`` returns the integer day of the week, where sunday is 1:
>>> dt.dow_1()
1
* ``h_12()`` returns the 12-hour clock representation of the hour:
>>> dt.h_12()
1
* ``h_24()`` returns the 24-hour clock representation of the hour:
>>> dt.h_24()
13
* ``ampm()`` returns the appropriate time modifier (am or pm):
>>> dt.ampm()
'pm'
* ``hour()`` returns the 24-hour clock representation of the hour:
>>> dt.hour()
13
* ``minute()`` returns the minute:
>>> dt.minute()
45
* ``second()`` returns the second:
>>> dt.second() == 0
True
* ``millis()`` returns the milliseconds since the epoch in GMT.
>>> dt.millis() == 857933100000
True
strftime()
~~~~~~~~~~
See ``tests/test_datetime.py``.
General formats from previous DateTime
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* ``Date()`` return the date string for the object:
>>> dt.Date()
'1997/03/09'
* ``Time()`` returns the time string for an object to the nearest
second:
>>> dt.Time()
'13:45:00'
* ``TimeMinutes()`` returns the time string for an object not showing
seconds:
>>> dt.TimeMinutes()
'13:45'
* ``AMPM()`` returns the time string for an object to the nearest second:
>>> dt.AMPM()
'01:45:00 pm'
* ``AMPMMinutes()`` returns the time string for an object not showing
seconds:
>>> dt.AMPMMinutes()
'01:45 pm'
* ``PreciseTime()`` returns the time string for the object:
>>> dt.PreciseTime()
'13:45:00.000'
* ``PreciseAMPM()`` returns the time string for the object:
>>> dt.PreciseAMPM()
'01:45:00.000 pm'
* ``yy()`` returns the calendar year as a 2 digit string
>>> dt.yy()
'97'
* ``mm()`` returns the month as a 2 digit string
>>> dt.mm()
'03'
* ``dd()`` returns the day as a 2 digit string:
>>> dt.dd()
'09'
* ``rfc822()`` returns the date in RFC 822 format:
>>> dt.rfc822()
'Sun, 09 Mar 1997 13:45:00 -0500'
New formats
~~~~~~~~~~~
* ``fCommon()`` returns a string representing the object's value in
the format: March 9, 1997 1:45 pm:
>>> dt.fCommon()
'March 9, 1997 1:45 pm'
* ``fCommonZ()`` returns a string representing the object's value in
the format: March 9, 1997 1:45 pm US/Eastern:
>>> dt.fCommonZ()
'March 9, 1997 1:45 pm US/Eastern'
* ``aCommon()`` returns a string representing the object's value in
the format: Mar 9, 1997 1:45 pm:
>>> dt.aCommon()
'Mar 9, 1997 1:45 pm'
* ``aCommonZ()`` return a string representing the object's value in
the format: Mar 9, 1997 1:45 pm US/Eastern:
>>> dt.aCommonZ()
'Mar 9, 1997 1:45 pm US/Eastern'
* ``pCommon()`` returns a string representing the object's value in
the format Mar. 9, 1997 1:45 pm:
>>> dt.pCommon()
'Mar. 9, 1997 1:45 pm'
* ``pCommonZ()`` returns a string representing the object's value in
the format: Mar. 9, 1997 1:45 pm US/Eastern:
>>> dt.pCommonZ()
'Mar. 9, 1997 1:45 pm US/Eastern'
* ``ISO()`` returns a string with the date/time in ISO format. Note:
this is not ISO 8601-format! See the ISO8601 and HTML4 methods below
for ISO 8601-compliant output. Dates are output as: YYYY-MM-DD HH:MM:SS
>>> dt.ISO()
'1997-03-09 13:45:00'
* ``ISO8601()`` returns the object in ISO 8601-compatible format
containing the date, time with seconds-precision and the time zone
identifier - see http://www.w3.org/TR/NOTE-datetime. Dates are
output as: YYYY-MM-DDTHH:MM:SSTZD (T is a literal character, TZD is
Time Zone Designator, format +HH:MM or -HH:MM).
The ``HTML4()`` method below offers the same formatting, but
converts to UTC before returning the value and sets the TZD"Z"
>>> dt.ISO8601()
'1997-03-09T13:45:00-05:00'
* ``HTML4()`` returns the object in the format used in the HTML4.0
specification, one of the standard forms in ISO8601. See
http://www.w3.org/TR/NOTE-datetime. Dates are output as:
YYYY-MM-DDTHH:MM:SSZ (T, Z are literal characters, the time is in
UTC.):
>>> dt.HTML4()
'1997-03-09T18:45:00Z'
* ``JulianDay()`` returns the Julian day according to
http://www.tondering.dk/claus/cal/node3.html#sec-calcjd
>>> dt.JulianDay()
2450517
* ``week()`` returns the week number according to ISO
see http://www.tondering.dk/claus/cal/node6.html#SECTION00670000000000000000
>>> dt.week()
10
Deprecated API
~~~~~~~~~~~~~~
* DayOfWeek(): see Day()
* Day_(): see pDay()
* Mon(): see aMonth()
* Mon_(): see pMonth
General Services Provided by DateTime
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DateTimes can be repr()'ed; the result will be a string indicating how
to make a DateTime object like this:
>>> repr(dt)
"DateTime('1997/03/09 13:45:00 US/Eastern')"
When we convert them into a string, we get a nicer string that could
actually be shown to a user:
>>> str(dt)
'1997/03/09 13:45:00 US/Eastern'
The hash value of a DateTime is based on the date and time and is
equal for different representations of the DateTime:
>>> hash(dt)
3618678
>>> hash(dt.toZone('UTC'))
3618678
DateTime objects can be compared to other DateTime objects OR floating
point numbers such as the ones which are returned by the Python time
module by using the equalTo method. Using this API, True is returned if the
object represents a date/time equal to the specified DateTime or time module
style time:
>>> dt.equalTo(dt)
True
>>> dt.equalTo(dt.toZone('UTC'))
True
>>> dt.equalTo(dt.timeTime())
True
>>> dt.equalTo(DateTime())
False
Same goes for inequalities:
>>> dt.notEqualTo(dt)
False
>>> dt.notEqualTo(dt.toZone('UTC'))
False
>>> dt.notEqualTo(dt.timeTime())
False
>>> dt.notEqualTo(DateTime())
True
Normal equality operations only work with DateTime objects and take the
timezone setting into account:
>>> dt == dt
True
>>> dt == dt.toZone('UTC')
False
>>> dt == DateTime()
False
>>> dt != dt
False
>>> dt != dt.toZone('UTC')
True
>>> dt != DateTime()
True
But the other comparison operations compare the referenced moment in time and
not the representation itself:
>>> dt > dt
False
>>> DateTime() > dt
True
>>> dt > DateTime().timeTime()
False
>>> DateTime().timeTime() > dt
True
>>> dt.greaterThan(dt)
False
>>> DateTime().greaterThan(dt)
True
>>> dt.greaterThan(DateTime().timeTime())
False
>>> dt >= dt
True
>>> DateTime() >= dt
True
>>> dt >= DateTime().timeTime()
False
>>> DateTime().timeTime() >= dt
True
>>> dt.greaterThanEqualTo(dt)
True
>>> DateTime().greaterThanEqualTo(dt)
True
>>> dt.greaterThanEqualTo(DateTime().timeTime())
False
>>> dt < dt
False
>>> DateTime() < dt
False
>>> dt < DateTime().timeTime()
True
>>> DateTime().timeTime() < dt
False
>>> dt.lessThan(dt)
False
>>> DateTime().lessThan(dt)
False
>>> dt.lessThan(DateTime().timeTime())
True
>>> dt <= dt
True
>>> DateTime() <= dt
False
>>> dt <= DateTime().timeTime()
True
>>> DateTime().timeTime() <= dt
False
>>> dt.lessThanEqualTo(dt)
True
>>> DateTime().lessThanEqualTo(dt)
False
>>> dt.lessThanEqualTo(DateTime().timeTime())
True
Numeric Services Provided by DateTime
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A DateTime may be added to a number and a number may be added to a
DateTime:
>>> dt + 5
DateTime('1997/03/14 13:45:00 US/Eastern')
>>> 5 + dt
DateTime('1997/03/14 13:45:00 US/Eastern')
Two DateTimes cannot be added:
>>> from DateTime.interfaces import DateTimeError
>>> try:
... dt + dt
... print('fail')
... except DateTimeError:
... print('ok')
ok
Either a DateTime or a number may be subtracted from a DateTime,
however, a DateTime may not be subtracted from a number:
>>> DateTime('1997/03/10 13:45 US/Eastern') - dt
1.0
>>> dt - 1
DateTime('1997/03/08 13:45:00 US/Eastern')
>>> 1 - dt
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for -: 'int' and 'DateTime'
DateTimes can also be converted to integers (number of seconds since
the epoch) and floats:
>>> int(dt)
857933100
>>> float(dt)
857933100.0

View File

@ -0,0 +1,18 @@
##############################################################################
#
# Copyright (c) 2002 Zope Foundation and Contributors.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
from .DateTime import DateTime
from .DateTime import Timezones
__all__ = ('DateTime', 'Timezones')

View File

@ -0,0 +1,375 @@
##############################################################################
#
# Copyright (c) 2005 Zope Foundation and Contributors.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
from zope.interface import Interface
class DateTimeError(Exception):
pass
class SyntaxError(DateTimeError):
pass
class DateError(DateTimeError):
pass
class TimeError(DateTimeError):
pass
class IDateTime(Interface):
# Conversion and comparison methods
def localZone(ltm=None):
"""Returns the time zone on the given date. The time zone
can change according to daylight savings."""
def timeTime():
"""Return the date/time as a floating-point number in UTC, in
the format used by the Python time module. Note that it is
possible to create date/time values with DateTime that have no
meaningful value to the time module."""
def toZone(z):
"""Return a DateTime with the value as the current object,
represented in the indicated timezone."""
def isFuture():
"""Return true if this object represents a date/time later
than the time of the call"""
def isPast():
"""Return true if this object represents a date/time earlier
than the time of the call"""
def isCurrentYear():
"""Return true if this object represents a date/time that
falls within the current year, in the context of this
object's timezone representation"""
def isCurrentMonth():
"""Return true if this object represents a date/time that
falls within the current month, in the context of this
object's timezone representation"""
def isCurrentDay():
"""Return true if this object represents a date/time that
falls within the current day, in the context of this object's
timezone representation"""
def isCurrentHour():
"""Return true if this object represents a date/time that
falls within the current hour, in the context of this object's
timezone representation"""
def isCurrentMinute():
"""Return true if this object represents a date/time that
falls within the current minute, in the context of this
object's timezone representation"""
def isLeapYear():
"""Return true if the current year (in the context of the
object's timezone) is a leap year"""
def earliestTime():
"""Return a new DateTime object that represents the earliest
possible time (in whole seconds) that still falls within the
current object's day, in the object's timezone context"""
def latestTime():
"""Return a new DateTime object that represents the latest
possible time (in whole seconds) that still falls within the
current object's day, in the object's timezone context"""
def greaterThan(t):
"""Compare this DateTime object to another DateTime object OR
a floating point number such as that which is returned by the
Python time module. Returns true if the object represents a
date/time greater than the specified DateTime or time module
style time. Revised to give more correct results through
comparison of long integer milliseconds."""
__gt__ = greaterThan
def greaterThanEqualTo(t):
"""Compare this DateTime object to another DateTime object OR
a floating point number such as that which is returned by the
Python time module. Returns true if the object represents a
date/time greater than or equal to the specified DateTime or
time module style time. Revised to give more correct results
through comparison of long integer milliseconds."""
__ge__ = greaterThanEqualTo
def equalTo(t):
"""Compare this DateTime object to another DateTime object OR
a floating point number such as that which is returned by the
Python time module. Returns true if the object represents a
date/time equal to the specified DateTime or time module style
time. Revised to give more correct results through comparison
of long integer milliseconds."""
__eq__ = equalTo
def notEqualTo(t):
"""Compare this DateTime object to another DateTime object OR
a floating point number such as that which is returned by the
Python time module. Returns true if the object represents a
date/time not equal to the specified DateTime or time module
style time. Revised to give more correct results through
comparison of long integer milliseconds."""
__ne__ = notEqualTo
def lessThan(t):
"""Compare this DateTime object to another DateTime object OR
a floating point number such as that which is returned by the
Python time module. Returns true if the object represents a
date/time less than the specified DateTime or time module
style time. Revised to give more correct results through
comparison of long integer milliseconds."""
__lt__ = lessThan
def lessThanEqualTo(t):
"""Compare this DateTime object to another DateTime object OR
a floating point number such as that which is returned by the
Python time module. Returns true if the object represents a
date/time less than or equal to the specified DateTime or time
module style time. Revised to give more correct results
through comparison of long integer milliseconds."""
__le__ = lessThanEqualTo
# Component access
def parts():
"""Return a tuple containing the calendar year, month, day,
hour, minute second and timezone of the object"""
def timezone():
"""Return the timezone in which the object is represented."""
def tzoffset():
"""Return the timezone offset for the objects timezone."""
def year():
"""Return the calendar year of the object"""
def month():
"""Return the month of the object as an integer"""
def Month():
"""Return the full month name"""
def aMonth():
"""Return the abbreviated month name."""
def Mon():
"""Compatibility: see aMonth"""
def pMonth():
"""Return the abbreviated (with period) month name."""
def Mon_():
"""Compatibility: see pMonth"""
def day():
"""Return the integer day"""
def Day():
"""Return the full name of the day of the week"""
def DayOfWeek():
"""Compatibility: see Day"""
def dayOfYear():
"""Return the day of the year, in context of the timezone
representation of the object"""
def aDay():
"""Return the abbreviated name of the day of the week"""
def pDay():
"""Return the abbreviated (with period) name of the day of the
week"""
def Day_():
"""Compatibility: see pDay"""
def dow():
"""Return the integer day of the week, where sunday is 0"""
def dow_1():
"""Return the integer day of the week, where sunday is 1"""
def h_12():
"""Return the 12-hour clock representation of the hour"""
def h_24():
"""Return the 24-hour clock representation of the hour"""
def ampm():
"""Return the appropriate time modifier (am or pm)"""
def hour():
"""Return the 24-hour clock representation of the hour"""
def minute():
"""Return the minute"""
def second():
"""Return the second"""
def millis():
"""Return the millisecond since the epoch in GMT."""
def strftime(format):
"""Format the date/time using the *current timezone representation*."""
# General formats from previous DateTime
def Date():
"""Return the date string for the object."""
def Time():
"""Return the time string for an object to the nearest second."""
def TimeMinutes():
"""Return the time string for an object not showing seconds."""
def AMPM():
"""Return the time string for an object to the nearest second."""
def AMPMMinutes():
"""Return the time string for an object not showing seconds."""
def PreciseTime():
"""Return the time string for the object."""
def PreciseAMPM():
"""Return the time string for the object."""
def yy():
"""Return calendar year as a 2 digit string"""
def mm():
"""Return month as a 2 digit string"""
def dd():
"""Return day as a 2 digit string"""
def rfc822():
"""Return the date in RFC 822 format"""
# New formats
def fCommon():
"""Return a string representing the object's value in the
format: March 1, 1997 1:45 pm"""
def fCommonZ():
"""Return a string representing the object's value in the
format: March 1, 1997 1:45 pm US/Eastern"""
def aCommon():
"""Return a string representing the object's value in the
format: Mar 1, 1997 1:45 pm"""
def aCommonZ():
"""Return a string representing the object's value in the
format: Mar 1, 1997 1:45 pm US/Eastern"""
def pCommon():
"""Return a string representing the object's value in the
format: Mar. 1, 1997 1:45 pm"""
def pCommonZ():
"""Return a string representing the object's value
in the format: Mar. 1, 1997 1:45 pm US/Eastern"""
def ISO():
"""Return the object in ISO standard format. Note: this is
*not* ISO 8601-format! See the ISO8601 and HTML4 methods below
for ISO 8601-compliant output
Dates are output as: YYYY-MM-DD HH:MM:SS
"""
def ISO8601():
"""Return the object in ISO 8601-compatible format containing
the date, time with seconds-precision and the time zone
identifier - see http://www.w3.org/TR/NOTE-datetime
Dates are output as: YYYY-MM-DDTHH:MM:SSTZD
T is a literal character.
TZD is Time Zone Designator, format +HH:MM or -HH:MM
The HTML4 method below offers the same formatting, but
converts to UTC before returning the value and sets the TZD"Z"
"""
def HTML4():
"""Return the object in the format used in the HTML4.0
specification, one of the standard forms in ISO8601. See
http://www.w3.org/TR/NOTE-datetime
Dates are output as: YYYY-MM-DDTHH:MM:SSZ
T, Z are literal characters.
The time is in UTC.
"""
def JulianDay():
"""Return the Julian day according to
https://www.tondering.dk/claus/cal/julperiod.php#formula
"""
def week():
"""Return the week number according to ISO.
See https://www.tondering.dk/claus/cal/week.php#weekno
"""
# Python operator and conversion API
def __add__(other):
"""A DateTime may be added to a number and a number may be
added to a DateTime; two DateTimes cannot be added."""
__radd__ = __add__
def __sub__(other):
"""Either a DateTime or a number may be subtracted from a
DateTime, however, a DateTime may not be subtracted from a
number."""
def __repr__():
"""Convert a DateTime to a string that looks like a Python
expression."""
def __str__():
"""Convert a DateTime to a string."""
def __hash__():
"""Compute a hash value for a DateTime"""
def __int__():
"""Convert to an integer number of seconds since the epoch (gmt)"""
def __long__():
"""Convert to a long-int number of seconds since the epoch (gmt)"""
def __float__():
"""Convert to floating-point number of seconds since the epoch (gmt)"""

View File

@ -0,0 +1,192 @@
Pytz Support
============
Allows the pytz package to be used for time zone information. The
advantage of using pytz is that it has a more complete and up to date
time zone and daylight savings time database.
Usage
-----
You don't have to do anything special to make it work.
>>> from DateTime import DateTime, Timezones
>>> d = DateTime('March 11, 2007 US/Eastern')
Daylight Savings
----------------
In 2007 daylight savings time in the US was changed. The Energy Policy
Act of 2005 mandates that DST will start on the second Sunday in March
and end on the first Sunday in November.
In 2007, the start and stop dates are March 11 and November 4,
respectively. These dates are different from previous DST start and
stop dates. In 2006, the dates were the first Sunday in April (April
2, 2006) and the last Sunday in October (October 29, 2006).
Let's make sure that DateTime can deal with this, since the primary
motivation to use pytz for time zone information is the fact that it
is kept up to date with daylight savings changes.
>>> DateTime('March 11, 2007 US/Eastern').tzoffset()
-18000
>>> DateTime('March 12, 2007 US/Eastern').tzoffset()
-14400
>>> DateTime('November 4, 2007 US/Eastern').tzoffset()
-14400
>>> DateTime('November 5, 2007 US/Eastern').tzoffset()
-18000
Let's compare this to 2006.
>>> DateTime('April 2, 2006 US/Eastern').tzoffset()
-18000
>>> DateTime('April 3, 2006 US/Eastern').tzoffset()
-14400
>>> DateTime('October 29, 2006 US/Eastern').tzoffset()
-14400
>>> DateTime('October 30, 2006 US/Eastern').tzoffset()
-18000
Time Zones
---------
DateTime can use pytz's large database of time zones. Here are some
examples:
>>> d = DateTime('Pacific/Kwajalein')
>>> d = DateTime('America/Shiprock')
>>> d = DateTime('Africa/Ouagadougou')
Of course pytz doesn't know about everything.
>>> from DateTime.interfaces import SyntaxError
>>> try:
... d = DateTime('July 21, 1969 Moon/Eastern')
... print('fail')
... except SyntaxError:
... print('ok')
ok
You can still use zone names that DateTime defines that aren't part of
the pytz database.
>>> d = DateTime('eet')
>>> d = DateTime('iceland')
These time zones use DateTimes database. So it's preferable to use the
official time zone name.
One trickiness is that DateTime supports some zone name
abbreviations. Some of these map to pytz names, so these abbreviations
will give you time zone date from pytz. Notable among abbreviations
that work this way are 'est', 'cst', 'mst', and 'pst'.
Let's verify that 'est' picks up the 2007 daylight savings time changes.
>>> DateTime('March 11, 2007 est').tzoffset()
-18000
>>> DateTime('March 12, 2007 est').tzoffset()
-14400
>>> DateTime('November 4, 2007 est').tzoffset()
-14400
>>> DateTime('November 5, 2007 est').tzoffset()
-18000
You can get a list of time zones supported by calling the Timezones() function.
>>> Timezones() #doctest: +ELLIPSIS
['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', ...]
Note that you can mess with this list without hurting things.
>>> t = Timezones()
>>> t.remove('US/Eastern')
>>> d = DateTime('US/Eastern')
Internal Components
-------------------
The following are tests of internal components.
Cache
~~~~~
The DateTime class uses a new time zone cache.
>>> from DateTime.DateTime import _TZINFO
>>> _TZINFO #doctest: +ELLIPSIS
<DateTime.pytz_support.PytzCache ...>
The cache maps time zone names to time zone instances.
>>> cache = _TZINFO
>>> tz = cache['GMT+730']
>>> tz = cache['US/Mountain']
The cache also must provide a few attributes for use by the DateTime
class.
The _zlst attribute is a list of supported time zone names.
>>> cache._zlst #doctest: +ELLIPSIS
['Africa/Abidjan'... 'Africa/Accra'... 'IDLE'... 'NZST'... 'NZT'...]
The _zidx attribute is a list of lower-case and possibly abbreviated
time zone names that can be mapped to official zone names.
>>> 'australia/yancowinna' in cache._zidx
True
>>> 'europe/isle_of_man' in cache._zidx
True
>>> 'gmt+0500' in cache._zidx
True
Note that there are more items in _zidx than in _zlst since there are
multiple names for some time zones.
>>> len(cache._zidx) > len(cache._zlst)
True
Each entry in _zlst should also be present in _zidx in lower case form.
>>> for name in cache._zlst:
... if not name.lower() in cache._zidx:
... print("Error %s not in _zidx" % name.lower())
The _zmap attribute maps the names in _zidx to official names in _zlst.
>>> cache._zmap['africa/abidjan']
'Africa/Abidjan'
>>> cache._zmap['gmt+1']
'GMT+1'
>>> cache._zmap['gmt+0100']
'GMT+1'
>>> cache._zmap['utc']
'UTC'
Let's make sure that _zmap and _zidx agree.
>>> idx = set(cache._zidx)
>>> keys = set(cache._zmap.keys())
>>> idx == keys
True
Timezone objects
~~~~~~~~~~~~~~~~
The timezone instances have only one public method info(). It returns
a tuple of (offset, is_dst, name). The method takes a timestamp, which
is used to determine dst information.
>>> t1 = DateTime('November 4, 00:00 2007 US/Mountain').timeTime()
>>> t2 = DateTime('November 4, 02:00 2007 US/Mountain').timeTime()
>>> tz.info(t1)
(-21600, 1, 'MDT')
>>> tz.info(t2)
(-25200, 0, 'MST')
If you don't pass any arguments to info it provides daylight savings
time information as of today.
>>> tz.info() in ((-21600, 1, 'MDT'), (-25200, 0, 'MST'))
True

View File

@ -0,0 +1,269 @@
##############################################################################
#
# Copyright (c) 2007 Zope Foundation and Contributors.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
from datetime import datetime
from datetime import timedelta
import pytz
import pytz.reference
from pytz.tzinfo import StaticTzInfo
from pytz.tzinfo import memorized_timedelta
from .interfaces import DateTimeError
EPOCH = datetime.fromtimestamp(0, tz=pytz.utc)
_numeric_timezone_data = {
'GMT': ('GMT', 0, 1, [], '', [(0, 0, 0)], 'GMT\000'),
'GMT+0': ('GMT+0', 0, 1, [], '', [(0, 0, 0)], 'GMT+0000\000'),
'GMT+1': ('GMT+1', 0, 1, [], '', [(3600, 0, 0)], 'GMT+0100\000'),
'GMT+2': ('GMT+2', 0, 1, [], '', [(7200, 0, 0)], 'GMT+0200\000'),
'GMT+3': ('GMT+3', 0, 1, [], '', [(10800, 0, 0)], 'GMT+0300\000'),
'GMT+4': ('GMT+4', 0, 1, [], '', [(14400, 0, 0)], 'GMT+0400\000'),
'GMT+5': ('GMT+5', 0, 1, [], '', [(18000, 0, 0)], 'GMT+0500\000'),
'GMT+6': ('GMT+6', 0, 1, [], '', [(21600, 0, 0)], 'GMT+0600\000'),
'GMT+7': ('GMT+7', 0, 1, [], '', [(25200, 0, 0)], 'GMT+0700\000'),
'GMT+8': ('GMT+8', 0, 1, [], '', [(28800, 0, 0)], 'GMT+0800\000'),
'GMT+9': ('GMT+9', 0, 1, [], '', [(32400, 0, 0)], 'GMT+0900\000'),
'GMT+10': ('GMT+10', 0, 1, [], '', [(36000, 0, 0)], 'GMT+1000\000'),
'GMT+11': ('GMT+11', 0, 1, [], '', [(39600, 0, 0)], 'GMT+1100\000'),
'GMT+12': ('GMT+12', 0, 1, [], '', [(43200, 0, 0)], 'GMT+1200\000'),
'GMT+13': ('GMT+13', 0, 1, [], '', [(46800, 0, 0)], 'GMT+1300\000'),
'GMT-1': ('GMT-1', 0, 1, [], '', [(-3600, 0, 0)], 'GMT-0100\000'),
'GMT-2': ('GMT-2', 0, 1, [], '', [(-7200, 0, 0)], 'GMT-0200\000'),
'GMT-3': ('GMT-3', 0, 1, [], '', [(-10800, 0, 0)], 'GMT-0300\000'),
'GMT-4': ('GMT-4', 0, 1, [], '', [(-14400, 0, 0)], 'GMT-0400\000'),
'GMT-5': ('GMT-5', 0, 1, [], '', [(-18000, 0, 0)], 'GMT-0500\000'),
'GMT-6': ('GMT-6', 0, 1, [], '', [(-21600, 0, 0)], 'GMT-0600\000'),
'GMT-7': ('GMT-7', 0, 1, [], '', [(-25200, 0, 0)], 'GMT-0700\000'),
'GMT-8': ('GMT-8', 0, 1, [], '', [(-28800, 0, 0)], 'GMT-0800\000'),
'GMT-9': ('GMT-9', 0, 1, [], '', [(-32400, 0, 0)], 'GMT-0900\000'),
'GMT-10': ('GMT-10', 0, 1, [], '', [(-36000, 0, 0)], 'GMT-1000\000'),
'GMT-11': ('GMT-11', 0, 1, [], '', [(-39600, 0, 0)], 'GMT-1100\000'),
'GMT-12': ('GMT-12', 0, 1, [], '', [(-43200, 0, 0)], 'GMT-1200\000'),
'GMT+0130': ('GMT+0130', 0, 1, [], '', [(5400, 0, 0)], 'GMT+0130\000'),
'GMT+0230': ('GMT+0230', 0, 1, [], '', [(9000, 0, 0)], 'GMT+0230\000'),
'GMT+0330': ('GMT+0330', 0, 1, [], '', [(12600, 0, 0)], 'GMT+0330\000'),
'GMT+0430': ('GMT+0430', 0, 1, [], '', [(16200, 0, 0)], 'GMT+0430\000'),
'GMT+0530': ('GMT+0530', 0, 1, [], '', [(19800, 0, 0)], 'GMT+0530\000'),
'GMT+0630': ('GMT+0630', 0, 1, [], '', [(23400, 0, 0)], 'GMT+0630\000'),
'GMT+0730': ('GMT+0730', 0, 1, [], '', [(27000, 0, 0)], 'GMT+0730\000'),
'GMT+0830': ('GMT+0830', 0, 1, [], '', [(30600, 0, 0)], 'GMT+0830\000'),
'GMT+0930': ('GMT+0930', 0, 1, [], '', [(34200, 0, 0)], 'GMT+0930\000'),
'GMT+1030': ('GMT+1030', 0, 1, [], '', [(37800, 0, 0)], 'GMT+1030\000'),
'GMT+1130': ('GMT+1130', 0, 1, [], '', [(41400, 0, 0)], 'GMT+1130\000'),
'GMT+1230': ('GMT+1230', 0, 1, [], '', [(45000, 0, 0)], 'GMT+1230\000'),
'GMT-0130': ('GMT-0130', 0, 1, [], '', [(-5400, 0, 0)], 'GMT-0130\000'),
'GMT-0230': ('GMT-0230', 0, 1, [], '', [(-9000, 0, 0)], 'GMT-0230\000'),
'GMT-0330': ('GMT-0330', 0, 1, [], '', [(-12600, 0, 0)], 'GMT-0330\000'),
'GMT-0430': ('GMT-0430', 0, 1, [], '', [(-16200, 0, 0)], 'GMT-0430\000'),
'GMT-0530': ('GMT-0530', 0, 1, [], '', [(-19800, 0, 0)], 'GMT-0530\000'),
'GMT-0630': ('GMT-0630', 0, 1, [], '', [(-23400, 0, 0)], 'GMT-0630\000'),
'GMT-0730': ('GMT-0730', 0, 1, [], '', [(-27000, 0, 0)], 'GMT-0730\000'),
'GMT-0830': ('GMT-0830', 0, 1, [], '', [(-30600, 0, 0)], 'GMT-0830\000'),
'GMT-0930': ('GMT-0930', 0, 1, [], '', [(-34200, 0, 0)], 'GMT-0930\000'),
'GMT-1030': ('GMT-1030', 0, 1, [], '', [(-37800, 0, 0)], 'GMT-1030\000'),
'GMT-1130': ('GMT-1130', 0, 1, [], '', [(-41400, 0, 0)], 'GMT-1130\000'),
'GMT-1230': ('GMT-1230', 0, 1, [], '', [(-45000, 0, 0)], 'GMT-1230\000'),
}
# These are the timezones not in pytz.common_timezones
_old_zlst = [
'AST', 'AT', 'BST', 'BT', 'CCT',
'CET', 'CST', 'Cuba', 'EADT', 'EAST',
'EEST', 'EET', 'EST', 'Egypt', 'FST',
'FWT', 'GB-Eire', 'GMT+0100', 'GMT+0130', 'GMT+0200',
'GMT+0230', 'GMT+0300', 'GMT+0330', 'GMT+0400', 'GMT+0430',
'GMT+0500', 'GMT+0530', 'GMT+0600', 'GMT+0630', 'GMT+0700',
'GMT+0730', 'GMT+0800', 'GMT+0830', 'GMT+0900', 'GMT+0930',
'GMT+1', 'GMT+1000', 'GMT+1030', 'GMT+1100', 'GMT+1130',
'GMT+1200', 'GMT+1230', 'GMT+1300', 'GMT-0100', 'GMT-0130',
'GMT-0200', 'GMT-0300', 'GMT-0400', 'GMT-0500', 'GMT-0600',
'GMT-0630', 'GMT-0700', 'GMT-0730', 'GMT-0800', 'GMT-0830',
'GMT-0900', 'GMT-0930', 'GMT-1000', 'GMT-1030', 'GMT-1100',
'GMT-1130', 'GMT-1200', 'GMT-1230', 'GST', 'Greenwich',
'Hongkong', 'IDLE', 'IDLW', 'Iceland', 'Iran',
'Israel', 'JST', 'Jamaica', 'Japan', 'MEST',
'MET', 'MEWT', 'MST', 'NT', 'NZDT',
'NZST', 'NZT', 'PST', 'Poland', 'SST',
'SWT', 'Singapore', 'Turkey', 'UCT', 'UT',
'Universal', 'WADT', 'WAST', 'WAT', 'WET',
'ZP4', 'ZP5', 'ZP6',
]
_old_zmap = {
'aest': 'GMT+10', 'aedt': 'GMT+11',
'aus eastern standard time': 'GMT+10',
'sydney standard time': 'GMT+10',
'tasmania standard time': 'GMT+10',
'e. australia standard time': 'GMT+10',
'aus central standard time': 'GMT+0930',
'cen. australia standard time': 'GMT+0930',
'w. australia standard time': 'GMT+8',
'central europe standard time': 'GMT+1',
'eastern standard time': 'US/Eastern',
'us eastern standard time': 'US/Eastern',
'central standard time': 'US/Central',
'mountain standard time': 'US/Mountain',
'pacific standard time': 'US/Pacific',
'mst': 'US/Mountain', 'pst': 'US/Pacific',
'cst': 'US/Central', 'est': 'US/Eastern',
'gmt+0000': 'GMT+0', 'gmt+0': 'GMT+0',
'gmt+0100': 'GMT+1', 'gmt+0200': 'GMT+2', 'gmt+0300': 'GMT+3',
'gmt+0400': 'GMT+4', 'gmt+0500': 'GMT+5', 'gmt+0600': 'GMT+6',
'gmt+0700': 'GMT+7', 'gmt+0800': 'GMT+8', 'gmt+0900': 'GMT+9',
'gmt+1000': 'GMT+10', 'gmt+1100': 'GMT+11', 'gmt+1200': 'GMT+12',
'gmt+1300': 'GMT+13',
'gmt-0100': 'GMT-1', 'gmt-0200': 'GMT-2', 'gmt-0300': 'GMT-3',
'gmt-0400': 'GMT-4', 'gmt-0500': 'GMT-5', 'gmt-0600': 'GMT-6',
'gmt-0700': 'GMT-7', 'gmt-0800': 'GMT-8', 'gmt-0900': 'GMT-9',
'gmt-1000': 'GMT-10', 'gmt-1100': 'GMT-11', 'gmt-1200': 'GMT-12',
'gmt+1': 'GMT+1', 'gmt+2': 'GMT+2', 'gmt+3': 'GMT+3',
'gmt+4': 'GMT+4', 'gmt+5': 'GMT+5', 'gmt+6': 'GMT+6',
'gmt+7': 'GMT+7', 'gmt+8': 'GMT+8', 'gmt+9': 'GMT+9',
'gmt+10': 'GMT+10', 'gmt+11': 'GMT+11', 'gmt+12': 'GMT+12',
'gmt+13': 'GMT+13',
'gmt-1': 'GMT-1', 'gmt-2': 'GMT-2', 'gmt-3': 'GMT-3',
'gmt-4': 'GMT-4', 'gmt-5': 'GMT-5', 'gmt-6': 'GMT-6',
'gmt-7': 'GMT-7', 'gmt-8': 'GMT-8', 'gmt-9': 'GMT-9',
'gmt-10': 'GMT-10', 'gmt-11': 'GMT-11', 'gmt-12': 'GMT-12',
'gmt+130': 'GMT+0130', 'gmt+0130': 'GMT+0130',
'gmt+230': 'GMT+0230', 'gmt+0230': 'GMT+0230',
'gmt+330': 'GMT+0330', 'gmt+0330': 'GMT+0330',
'gmt+430': 'GMT+0430', 'gmt+0430': 'GMT+0430',
'gmt+530': 'GMT+0530', 'gmt+0530': 'GMT+0530',
'gmt+630': 'GMT+0630', 'gmt+0630': 'GMT+0630',
'gmt+730': 'GMT+0730', 'gmt+0730': 'GMT+0730',
'gmt+830': 'GMT+0830', 'gmt+0830': 'GMT+0830',
'gmt+930': 'GMT+0930', 'gmt+0930': 'GMT+0930',
'gmt+1030': 'GMT+1030',
'gmt+1130': 'GMT+1130',
'gmt+1230': 'GMT+1230',
'gmt-130': 'GMT-0130', 'gmt-0130': 'GMT-0130',
'gmt-230': 'GMT-0230', 'gmt-0230': 'GMT-0230',
'gmt-330': 'GMT-0330', 'gmt-0330': 'GMT-0330',
'gmt-430': 'GMT-0430', 'gmt-0430': 'GMT-0430',
'gmt-530': 'GMT-0530', 'gmt-0530': 'GMT-0530',
'gmt-630': 'GMT-0630', 'gmt-0630': 'GMT-0630',
'gmt-730': 'GMT-0730', 'gmt-0730': 'GMT-0730',
'gmt-830': 'GMT-0830', 'gmt-0830': 'GMT-0830',
'gmt-930': 'GMT-0930', 'gmt-0930': 'GMT-0930',
'gmt-1030': 'GMT-1030',
'gmt-1130': 'GMT-1130',
'gmt-1230': 'GMT-1230',
'ut': 'Universal',
'bst': 'GMT+1', 'mest': 'GMT+2', 'sst': 'GMT+2',
'fst': 'GMT+2', 'wadt': 'GMT+8', 'eadt': 'GMT+11', 'nzdt': 'GMT+13',
'wet': 'GMT', 'wat': 'GMT+1', 'at': 'GMT-2', 'ast': 'GMT-4',
'nt': 'GMT-11', 'idlw': 'GMT-12', 'cet': 'GMT+1', 'cest': 'GMT+2',
'met': 'GMT+1',
'mewt': 'GMT+1', 'swt': 'GMT+1', 'fwt': 'GMT+1', 'eet': 'GMT+2',
'eest': 'GMT+3',
'bt': 'GMT+3', 'zp4': 'GMT+4', 'zp5': 'GMT+5', 'zp6': 'GMT+6',
'wast': 'GMT+7', 'cct': 'GMT+8', 'jst': 'GMT+9', 'east': 'GMT+10',
'gst': 'GMT+10', 'nzt': 'GMT+12', 'nzst': 'GMT+12', 'idle': 'GMT+12',
'ret': 'GMT+4', 'ist': 'GMT+0530', 'edt': 'GMT-4',
}
# some timezone definitions of the "-0400" are not working
# when upgrading
for hour in range(0, 13):
hour = hour
fhour = str(hour)
if len(fhour) == 1:
fhour = '0' + fhour
_old_zmap['-%s00' % fhour] = 'GMT-%i' % hour
_old_zmap['+%s00' % fhour] = 'GMT+%i' % hour
def _p(zone):
return _numeric_timezones[zone]
def _static_timezone_factory(data):
zone = data[0]
cls = type(zone, (StaticTzInfo,), dict(
__reduce__=lambda _: (_p, (zone, )),
zone=zone,
_utcoffset=memorized_timedelta(data[5][0][0]),
_tzname=data[6][:-1])) # strip the trailing null
return cls()
_numeric_timezones = {key: _static_timezone_factory(data)
for key, data in _numeric_timezone_data.items()}
class Timezone:
"""
Timezone information returned by PytzCache.__getitem__
Adapts datetime.tzinfo object to DateTime._timezone interface
"""
def __init__(self, tzinfo):
self.tzinfo = tzinfo
def info(self, t=None):
if t is None:
dt = datetime.now(tz=pytz.utc)
else:
# can't use utcfromtimestamp past 2038
dt = EPOCH + timedelta(0, t)
# need to normalize tzinfo for the datetime to deal with
# daylight savings time.
normalized_dt = self.tzinfo.normalize(dt.astimezone(self.tzinfo))
normalized_tzinfo = normalized_dt.tzinfo
offset = normalized_tzinfo.utcoffset(normalized_dt)
secs = offset.days * 24 * 60 * 60 + offset.seconds
dst = normalized_tzinfo.dst(normalized_dt)
if dst == timedelta(0):
is_dst = 0
else:
is_dst = 1
return secs, is_dst, normalized_tzinfo.tzname(normalized_dt)
class PytzCache:
"""
Reimplementation of the DateTime._cache class that uses for timezone info
"""
_zlst = pytz.common_timezones + _old_zlst # used by DateTime.TimeZones
_zmap = {name.lower(): name for name in pytz.all_timezones}
_zmap.update(_old_zmap) # These must take priority
_zidx = _zmap.keys()
def __getitem__(self, key):
name = self._zmap.get(key.lower(), key) # fallback to key
try:
return Timezone(pytz.timezone(name))
except pytz.UnknownTimeZoneError:
try:
return Timezone(_numeric_timezones[name])
except KeyError:
raise DateTimeError('Unrecognized timezone: %s' % key)

View File

@ -0,0 +1,15 @@
##############################################################################
#
# Copyright (c) 2003 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
# This file is needed to make this a package.

View File

@ -0,0 +1,57 @@
1970-01-01 (1970, 1, 4)
1970-01-02 (1970, 1, 5)
1970-01-30 (1970, 5, 5)
1970-01-31 (1970, 5, 6)
1970-02-01 (1970, 5, 7)
1970-02-02 (1970, 6, 1)
1970-02-28 (1970, 9, 6)
1970-03-01 (1970, 9, 7)
1970-03-30 (1970, 14, 1)
1970-03-31 (1970, 14, 2)
1970-04-01 (1970, 14, 3)
1970-09-30 (1970, 40, 3)
1970-10-01 (1970, 40, 4)
1970-10-02 (1970, 40, 5)
1970-10-03 (1970, 40, 6)
1970-10-04 (1970, 40, 7)
1970-10-05 (1970, 41, 1)
1971-01-02 (1970, 53, 6)
1971-01-03 (1970, 53, 7)
1971-01-04 (1971, 1, 1)
1971-01-05 (1971, 1, 2)
1971-12-31 (1971, 52, 5)
1972-01-01 (1971, 52, 6)
1972-01-02 (1971, 52, 7)
1972-01-03 (1972, 1, 1)
1972-01-04 (1972, 1, 2)
1972-12-30 (1972, 52, 6)
1972-12-31 (1972, 52, 7)
1973-01-01 (1973, 1, 1)
1973-01-02 (1973, 1, 2)
1973-12-29 (1973, 52, 6)
1973-12-30 (1973, 52, 7)
1973-12-31 (1974, 1, 1)
1974-01-01 (1974, 1, 2)
1998-12-30 (1998, 53, 3)
1998-12-31 (1998, 53, 4)
1999-01-01 (1998, 53, 5)
1999-01-02 (1998, 53, 6)
1999-01-03 (1998, 53, 7)
1999-01-04 (1999, 1, 1)
1999-01-05 (1999, 1, 2)
1999-12-30 (1999, 52, 4)
1999-12-31 (1999, 52, 5)
2000-01-01 (1999, 52, 6)
2000-01-02 (1999, 52, 7)
2000-01-03 (2000, 1, 1)
2000-01-04 (2000, 1, 2)
2000-01-05 (2000, 1, 3)
2000-01-06 (2000, 1, 4)
2000-01-07 (2000, 1, 5)
2000-01-08 (2000, 1, 6)
2000-01-09 (2000, 1, 7)
2000-01-10 (2000, 2, 1)
2019-12-28 (2019, 52, 6)
2019-12-29 (2019, 52, 7)
2019-12-30 (2020, 1, 1)
2019-12-31 (2020, 1, 2)

View File

@ -0,0 +1,764 @@
##############################################################################
#
# Copyright (c) 2003 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
import math
import os
import pickle
import platform
import sys
import time
import unittest
from datetime import date
from datetime import datetime
from datetime import timedelta
from datetime import tzinfo
import pytz
from DateTime import DateTime
from DateTime.DateTime import _findLocalTimeZoneName
try:
__file__
except NameError: # pragma: no cover
f = sys.argv[0]
else:
f = __file__
IS_PYPY = getattr(platform, 'python_implementation', lambda: None)() == 'PyPy'
DATADIR = os.path.dirname(os.path.abspath(f))
del f
ZERO = timedelta(0)
class FixedOffset(tzinfo):
"""Fixed offset in minutes east from UTC."""
def __init__(self, offset, name):
self.__offset = timedelta(minutes=offset)
self.__name = name
def utcoffset(self, dt):
return self.__offset
def tzname(self, dt):
return self.__name
def dst(self, dt):
return ZERO
class DateTimeTests(unittest.TestCase):
def _compare(self, dt1, dt2):
'''Compares the internal representation of dt1 with
the representation in dt2. Allows sub-millisecond variations.
Primarily for testing.'''
self.assertEqual(round(dt1._t, 3), round(dt2._t, 3))
self.assertEqual(round(dt1._d, 9), round(dt2._d, 9))
self.assertEqual(round(dt1.time, 9), round(dt2.time, 9))
self.assertEqual(dt1.millis(), dt2.millis())
self.assertEqual(dt1._micros, dt2._micros)
def testBug1203(self):
# 01:59:60 occurred in old DateTime
dt = DateTime(7200, 'GMT')
self.assertTrue(str(dt).find('60') < 0, dt)
def testDSTInEffect(self):
# Checks GMT offset for a DST date in the US/Eastern time zone
dt = DateTime(2000, 5, 9, 15, 0, 0, 'US/Eastern')
self.assertEqual(dt.toZone('GMT').hour(), 19,
(dt, dt.toZone('GMT')))
def testDSTNotInEffect(self):
# Checks GMT offset for a non-DST date in the US/Eastern time zone
dt = DateTime(2000, 11, 9, 15, 0, 0, 'US/Eastern')
self.assertEqual(dt.toZone('GMT').hour(), 20,
(dt, dt.toZone('GMT')))
def testAddPrecision(self):
# Precision of serial additions
dt = DateTime()
self.assertEqual(str(dt + 0.10 + 3.14 + 6.76 - 10), str(dt),
dt)
# checks problem reported in
# https://github.com/zopefoundation/DateTime/issues/41
dt = DateTime(2038, 10, 7, 8, 52, 44.959840, "UTC")
self.assertEqual(str(dt + 0.10 + 3.14 + 6.76 - 10), str(dt),
dt)
def testConsistentSecondMicroRounding(self):
dt = DateTime(2038, 10, 7, 8, 52, 44.9598398, "UTC")
self.assertEqual(int(dt.second() * 1000000),
dt.micros() % 60000000)
def testConstructor3(self):
# Constructor from date/time string
dt = DateTime()
dt1s = '%d/%d/%d %d:%d:%f %s' % (
dt.year(),
dt.month(),
dt.day(),
dt.hour(),
dt.minute(),
dt.second(),
dt.timezone())
dt1 = DateTime(dt1s)
# Compare representations as it's the
# only way to compare the dates to the same accuracy
self.assertEqual(repr(dt), repr(dt1))
def testConstructor4(self):
# Constructor from time float
dt = DateTime()
dt1 = DateTime(float(dt))
self._compare(dt, dt1)
def testConstructor5(self):
# Constructor from time float and timezone
dt = DateTime()
dt1 = DateTime(float(dt), dt.timezone())
self.assertEqual(str(dt), str(dt1), (dt, dt1))
dt1 = DateTime(float(dt), str(dt.timezone()))
self.assertEqual(str(dt), str(dt1), (dt, dt1))
def testConstructor6(self):
# Constructor from year and julian date
# This test must normalize the time zone, or it *will* break when
# DST changes!
dt1 = DateTime(2000, 5.500000578705)
dt = DateTime('2000/1/5 12:00:00.050 pm %s' % dt1.localZone())
self._compare(dt, dt1)
def testConstructor7(self):
# Constructor from parts
dt = DateTime()
dt1 = DateTime(
dt.year(),
dt.month(),
dt.day(),
dt.hour(),
dt.minute(),
dt.second(),
dt.timezone())
# Compare representations as it's the
# only way to compare the dates to the same accuracy
self.assertEqual(repr(dt), repr(dt1))
def testDayOfWeek(self):
# Compare to the datetime.date value to make it locale independent
expected = date(2000, 6, 16).strftime('%A')
# strftime() used to always be passed a day of week of 0
dt = DateTime('2000/6/16')
s = dt.strftime('%A')
self.assertEqual(s, expected, (dt, s))
def testOldDate(self):
# Fails when an 1800 date is displayed with negative signs
dt = DateTime('1830/5/6 12:31:46.213 pm')
dt1 = dt.toZone('GMT+6')
self.assertTrue(str(dt1).find('-') < 0, (dt, dt1))
def testSubtraction(self):
# Reconstruction of a DateTime from its parts, with subtraction
# this also tests the accuracy of addition and reconstruction
dt = DateTime()
dt1 = dt - 3.141592653
dt2 = DateTime(
dt.year(),
dt.month(),
dt.day(),
dt.hour(),
dt.minute(),
dt.second())
dt3 = dt2 - 3.141592653
self.assertEqual(dt1, dt3, (dt, dt1, dt2, dt3))
def testTZ1add(self):
# Time zone manipulation: add to a date
dt = DateTime('1997/3/8 1:45am GMT-4')
dt1 = DateTime('1997/3/9 1:45pm GMT+8')
self.assertTrue((dt + 1.0).equalTo(dt1))
def testTZ1sub(self):
# Time zone manipulation: subtract from a date
dt = DateTime('1997/3/8 1:45am GMT-4')
dt1 = DateTime('1997/3/9 1:45pm GMT+8')
self.assertTrue((dt1 - 1.0).equalTo(dt))
def testTZ1diff(self):
# Time zone manipulation: diff two dates
dt = DateTime('1997/3/8 1:45am GMT-4')
dt1 = DateTime('1997/3/9 1:45pm GMT+8')
self.assertEqual(dt1 - dt, 1.0, (dt, dt1))
def test_compare_methods(self):
# Compare two dates using several methods
dt = DateTime('1997/1/1')
dt1 = DateTime('1997/2/2')
self.assertTrue(dt1.greaterThan(dt))
self.assertTrue(dt1.greaterThanEqualTo(dt))
self.assertTrue(dt.lessThan(dt1))
self.assertTrue(dt.lessThanEqualTo(dt1))
self.assertTrue(dt.notEqualTo(dt1))
self.assertFalse(dt.equalTo(dt1))
# Compare a date to float
dt = DateTime(1.0)
self.assertTrue(dt == DateTime(1.0)) # testing __eq__
self.assertFalse(dt != DateTime(1.0)) # testing __ne__
self.assertFalse(dt.greaterThan(1.0))
self.assertTrue(dt.greaterThanEqualTo(1.0))
self.assertFalse(dt.lessThan(1.0))
self.assertTrue(dt.lessThanEqualTo(1.0))
self.assertFalse(dt.notEqualTo(1.0))
self.assertTrue(dt.equalTo(1.0))
# Compare a date to int
dt = DateTime(1)
self.assertEqual(dt, DateTime(1.0))
self.assertTrue(dt == DateTime(1)) # testing __eq__
self.assertFalse(dt != DateTime(1)) # testing __ne__
self.assertFalse(dt.greaterThan(1))
self.assertTrue(dt.greaterThanEqualTo(1))
self.assertFalse(dt.lessThan(1))
self.assertTrue(dt.lessThanEqualTo(1))
self.assertFalse(dt.notEqualTo(1))
self.assertTrue(dt.equalTo(1))
# Compare a date to string; there is no implicit type conversion
# but behavior if consistent as when comparing, for example, an int
# and a string.
dt = DateTime("2023")
self.assertFalse(dt == "2023") # testing __eq__
self.assertTrue(dt != "2023") # testing __ne__
self.assertRaises(TypeError, dt.greaterThan, "2023")
self.assertRaises(TypeError, dt.greaterThanEqualTo, "2023")
self.assertRaises(TypeError, dt.lessThan, "2023")
self.assertRaises(TypeError, dt.lessThanEqualTo, "2023")
self.assertTrue(dt.notEqualTo("2023"))
self.assertFalse(dt.equalTo("2023"))
def test_compare_methods_none(self):
# Compare a date to None
for dt in (DateTime('1997/1/1'), DateTime(0)):
self.assertTrue(dt.greaterThan(None))
self.assertTrue(dt.greaterThanEqualTo(None))
self.assertFalse(dt.lessThan(None))
self.assertFalse(dt.lessThanEqualTo(None))
self.assertTrue(dt.notEqualTo(None))
self.assertFalse(dt.equalTo(None))
def test_pickle(self):
dt = DateTime()
data = pickle.dumps(dt, 1)
new = pickle.loads(data)
for key in DateTime.__slots__:
self.assertEqual(getattr(dt, key), getattr(new, key))
def test_pickle_with_tz(self):
dt = DateTime('2002/5/2 8:00am GMT+8')
data = pickle.dumps(dt, 1)
new = pickle.loads(data)
for key in DateTime.__slots__:
self.assertEqual(getattr(dt, key), getattr(new, key))
def test_pickle_asdatetime_with_tz(self):
dt = DateTime('2002/5/2 8:00am GMT+8')
data = pickle.dumps(dt.asdatetime(), 1)
new = DateTime(pickle.loads(data))
for key in DateTime.__slots__:
self.assertEqual(getattr(dt, key), getattr(new, key))
def test_pickle_with_numerical_tz(self):
for dt_str in ('2007/01/02 12:34:56.789 +0300',
'2007/01/02 12:34:56.789 +0430',
'2007/01/02 12:34:56.789 -1234'):
dt = DateTime(dt_str)
data = pickle.dumps(dt, 1)
new = pickle.loads(data)
for key in DateTime.__slots__:
self.assertEqual(getattr(dt, key), getattr(new, key))
def test_pickle_with_micros(self):
dt = DateTime('2002/5/2 8:00:14.123 GMT+8')
data = pickle.dumps(dt, 1)
new = pickle.loads(data)
for key in DateTime.__slots__:
self.assertEqual(getattr(dt, key), getattr(new, key))
def test_pickle_old(self):
dt = DateTime('2002/5/2 8:00am GMT+0')
data = (
'(cDateTime.DateTime\nDateTime\nq\x01Noq\x02}q\x03(U\x05'
'_amonq\x04U\x03Mayq\x05U\x05_adayq\x06U\x03Thuq\x07U\x05_pmonq'
'\x08h\x05U\x05_hourq\tK\x08U\x05_fmonq\nh\x05U\x05_pdayq\x0bU'
'\x04Thu.q\x0cU\x05_fdayq\rU\x08Thursdayq\x0eU\x03_pmq\x0fU\x02amq'
'\x10U\x02_tq\x11GA\xcehy\x00\x00\x00\x00U\x07_minuteq\x12K\x00U'
'\x07_microsq\x13L1020326400000000L\nU\x02_dq\x14G@\xe2\x12j\xaa'
'\xaa\xaa\xabU\x07_secondq\x15G\x00\x00\x00\x00\x00\x00\x00\x00U'
'\x03_tzq\x16U\x05GMT+0q\x17U\x06_monthq\x18K\x05U'
'\x0f_timezone_naiveq\x19I00\nU\x04_dayq\x1aK\x02U\x05_yearq'
'\x1bM\xd2\x07U\x08_nearsecq\x1cG\x00\x00\x00\x00\x00\x00\x00'
'\x00U\x07_pmhourq\x1dK\x08U\n_dayoffsetq\x1eK\x04U\x04timeq'
'\x1fG?\xd5UUUV\x00\x00ub.')
data = data.encode('latin-1')
new = pickle.loads(data)
for key in DateTime.__slots__:
self.assertEqual(getattr(dt, key), getattr(new, key))
def test_pickle_old_without_micros(self):
dt = DateTime('2002/5/2 8:00am GMT+0')
data = (
'(cDateTime.DateTime\nDateTime\nq\x01Noq\x02}q\x03(U\x05'
'_amonq\x04U\x03Mayq\x05U\x05_adayq\x06U\x03Thuq\x07U\x05_pmonq'
'\x08h\x05U\x05_hourq\tK\x08U\x05_fmonq\nh\x05U\x05_pdayq\x0bU'
'\x04Thu.q\x0cU\x05_fdayq\rU\x08Thursdayq\x0eU\x03_pmq\x0fU'
'\x02amq\x10U\x02_tq\x11GA\xcehy\x00\x00\x00\x00U\x07_minuteq'
'\x12K\x00U\x02_dq\x13G@\xe2\x12j\xaa\xaa\xaa\xabU\x07_secondq'
'\x14G\x00\x00\x00\x00\x00\x00\x00\x00U\x03_tzq\x15U\x05GMT+0q'
'\x16U\x06_monthq\x17K\x05U\x0f_timezone_naiveq\x18I00\nU'
'\x04_dayq\x19K\x02U\x05_yearq\x1aM\xd2\x07U\x08_nearsecq'
'\x1bG\x00\x00\x00\x00\x00\x00\x00\x00U\x07_pmhourq\x1cK\x08U'
'\n_dayoffsetq\x1dK\x04U\x04timeq\x1eG?\xd5UUUV\x00\x00ub.')
data = data.encode('latin-1')
new = pickle.loads(data)
for key in DateTime.__slots__:
self.assertEqual(getattr(dt, key), getattr(new, key))
def test_pickle_dates_after_2038(self):
dt = DateTime('2039/09/02 07:07:6.235027 GMT+1')
data = pickle.dumps(dt, 1)
new = pickle.loads(data)
for key in DateTime.__slots__:
self.assertEqual(getattr(dt, key), getattr(new, key))
def test_pickle_old_with_micros_as_float(self):
dt = DateTime('2002/5/2 8:00am GMT+0')
data = (
'ccopy_reg\n_reconstructor\nq\x00(cDateTime.DateTime\nDateTime'
'\nq\x01c__builtin__\nobject\nq\x02Ntq\x03Rq\x04(GA\xcehy\x00\x00'
'\x00\x00I00\nX\x05\x00\x00\x00GMT+0q\x05tq\x06b.')
data = data.encode('latin-1')
new = pickle.loads(data)
for key in DateTime.__slots__:
self.assertEqual(getattr(dt, key), getattr(new, key))
def testTZ2(self):
# Time zone manipulation test 2
dt = DateTime()
dt1 = dt.toZone('GMT')
s = dt.second()
s1 = dt1.second()
self.assertEqual(s, s1, (dt, dt1, s, s1))
def testTZDiffDaylight(self):
# Diff dates across daylight savings dates
dt = DateTime('2000/6/8 1:45am US/Eastern')
dt1 = DateTime('2000/12/8 12:45am US/Eastern')
self.assertEqual(dt1 - dt, 183, (dt, dt1, dt1 - dt))
def testY10KDate(self):
# Comparison of a Y10K date and a Y2K date
dt = DateTime('10213/09/21')
dt1 = DateTime(2000, 1, 1)
dsec = (dt.millis() - dt1.millis()) / 1000.0
ddays = math.floor((dsec / 86400.0) + 0.5)
self.assertEqual(ddays, 3000000, ddays)
def test_tzoffset(self):
# Test time-zone given as an offset
# GMT
dt = DateTime('Tue, 10 Sep 2001 09:41:03 GMT')
self.assertEqual(dt.tzoffset(), 0)
# Timezone by name, a timezone that hasn't got daylightsaving.
dt = DateTime('Tue, 2 Mar 2001 09:41:03 GMT+3')
self.assertEqual(dt.tzoffset(), 10800)
# Timezone by name, has daylightsaving but is not in effect.
dt = DateTime('Tue, 21 Jan 2001 09:41:03 PST')
self.assertEqual(dt.tzoffset(), -28800)
# Timezone by name, with daylightsaving in effect
dt = DateTime('Tue, 24 Aug 2001 09:41:03 PST')
self.assertEqual(dt.tzoffset(), -25200)
# A negative numerical timezone
dt = DateTime('Tue, 24 Jul 2001 09:41:03 -0400')
self.assertEqual(dt.tzoffset(), -14400)
# A positive numerical timzone
dt = DateTime('Tue, 6 Dec 1966 01:41:03 +0200')
self.assertEqual(dt.tzoffset(), 7200)
# A negative numerical timezone with minutes.
dt = DateTime('Tue, 24 Jul 2001 09:41:03 -0637')
self.assertEqual(dt.tzoffset(), -23820)
# A positive numerical timezone with minutes.
dt = DateTime('Tue, 24 Jul 2001 09:41:03 +0425')
self.assertEqual(dt.tzoffset(), 15900)
def testISO8601(self):
# ISO8601 reference dates
ref0 = DateTime('2002/5/2 8:00am GMT')
ref1 = DateTime('2002/5/2 8:00am US/Eastern')
ref2 = DateTime('2006/11/6 10:30 GMT')
ref3 = DateTime('2004/06/14 14:30:15 GMT-3')
ref4 = DateTime('2006/01/01 GMT')
# Basic tests
# Though this is timezone naive and according to specification should
# be interpreted in the local timezone, to preserve backwards
# compatibility with previously expected behaviour.
isoDt = DateTime('2002-05-02T08:00:00')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('2002-05-02T08:00:00Z')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('2002-05-02T08:00:00+00:00')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('2002-05-02T08:00:00-04:00')
self.assertTrue(ref1.equalTo(isoDt))
isoDt = DateTime('2002-05-02 08:00:00-04:00')
self.assertTrue(ref1.equalTo(isoDt))
# Bug 1386: the colon in the timezone offset is optional
isoDt = DateTime('2002-05-02T08:00:00-0400')
self.assertTrue(ref1.equalTo(isoDt))
# Bug 2191: date reduced formats
isoDt = DateTime('2006-01-01')
self.assertTrue(ref4.equalTo(isoDt))
isoDt = DateTime('200601-01')
self.assertTrue(ref4.equalTo(isoDt))
isoDt = DateTime('20060101')
self.assertTrue(ref4.equalTo(isoDt))
isoDt = DateTime('2006-01')
self.assertTrue(ref4.equalTo(isoDt))
isoDt = DateTime('200601')
self.assertTrue(ref4.equalTo(isoDt))
isoDt = DateTime('2006')
self.assertTrue(ref4.equalTo(isoDt))
# Bug 2191: date/time separators are also optional
isoDt = DateTime('20020502T08:00:00')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('2002-05-02T080000')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('20020502T080000')
self.assertTrue(ref0.equalTo(isoDt))
# Bug 2191: timezones with only one digit for hour
isoDt = DateTime('20020502T080000+0')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('20020502 080000-4')
self.assertTrue(ref1.equalTo(isoDt))
isoDt = DateTime('20020502T080000-400')
self.assertTrue(ref1.equalTo(isoDt))
isoDt = DateTime('20020502T080000-4:00')
self.assertTrue(ref1.equalTo(isoDt))
# Bug 2191: optional seconds/minutes
isoDt = DateTime('2002-05-02T0800')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('2002-05-02T08')
self.assertTrue(ref0.equalTo(isoDt))
# Bug 2191: week format
isoDt = DateTime('2002-W18-4T0800')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('2002-W184T0800')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('2002W18-4T0800')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('2002W184T08')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('2004-W25-1T14:30:15-03:00')
self.assertTrue(ref3.equalTo(isoDt))
isoDt = DateTime('2004-W25T14:30:15-03:00')
self.assertTrue(ref3.equalTo(isoDt))
# Bug 2191: day of year format
isoDt = DateTime('2002-122T0800')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('2002122T0800')
self.assertTrue(ref0.equalTo(isoDt))
# Bug 2191: hours/minutes fractions
isoDt = DateTime('2006-11-06T10.5')
self.assertTrue(ref2.equalTo(isoDt))
isoDt = DateTime('2006-11-06T10,5')
self.assertTrue(ref2.equalTo(isoDt))
isoDt = DateTime('20040614T1430.25-3')
self.assertTrue(ref3.equalTo(isoDt))
isoDt = DateTime('2004-06-14T1430,25-3')
self.assertTrue(ref3.equalTo(isoDt))
isoDt = DateTime('2004-06-14T14:30.25-3')
self.assertTrue(ref3.equalTo(isoDt))
isoDt = DateTime('20040614T14:30,25-3')
self.assertTrue(ref3.equalTo(isoDt))
# ISO8601 standard format
iso8601_string = '2002-05-02T08:00:00-04:00'
iso8601DT = DateTime(iso8601_string)
self.assertEqual(iso8601_string, iso8601DT.ISO8601())
# ISO format with no timezone
isoDt = DateTime('2006-01-01 00:00:00')
self.assertTrue(ref4.equalTo(isoDt))
def testJulianWeek(self):
# Check JulianDayWeek function
fn = os.path.join(DATADIR, 'julian_testdata.txt')
with open(fn) as fd:
lines = fd.readlines()
for line in lines:
d = DateTime(line[:10])
result_from_mx = tuple(map(int, line[12:-2].split(',')))
self.assertEqual(result_from_mx[1], d.week())
def testCopyConstructor(self):
d = DateTime('2004/04/04')
self.assertEqual(DateTime(d), d)
self.assertEqual(str(DateTime(d)), str(d))
d2 = DateTime('1999/04/12 01:00:00')
self.assertEqual(DateTime(d2), d2)
self.assertEqual(str(DateTime(d2)), str(d2))
def testCopyConstructorPreservesTimezone(self):
# test for https://bugs.launchpad.net/zope2/+bug/200007
# This always worked in the local timezone, so we need at least
# two tests with different zones to be sure at least one of them
# is not local.
d = DateTime('2004/04/04')
self.assertEqual(DateTime(d).timezone(), d.timezone())
d2 = DateTime('2008/04/25 12:00:00 EST')
self.assertEqual(DateTime(d2).timezone(), d2.timezone())
self.assertEqual(str(DateTime(d2)), str(d2))
d3 = DateTime('2008/04/25 12:00:00 PST')
self.assertEqual(DateTime(d3).timezone(), d3.timezone())
self.assertEqual(str(DateTime(d3)), str(d3))
def testRFC822(self):
# rfc822 conversion
dt = DateTime('2002-05-02T08:00:00+00:00')
self.assertEqual(dt.rfc822(), 'Thu, 02 May 2002 08:00:00 +0000')
dt = DateTime('2002-05-02T08:00:00+02:00')
self.assertEqual(dt.rfc822(), 'Thu, 02 May 2002 08:00:00 +0200')
dt = DateTime('2002-05-02T08:00:00-02:00')
self.assertEqual(dt.rfc822(), 'Thu, 02 May 2002 08:00:00 -0200')
# Checking that conversion from local time is working.
dt = DateTime()
dts = dt.rfc822().split(' ')
times = dts[4].split(':')
_isDST = time.localtime(time.time())[8]
if _isDST:
offset = time.altzone
else:
offset = time.timezone
self.assertEqual(dts[0], dt.aDay() + ',')
self.assertEqual(int(dts[1]), dt.day())
self.assertEqual(dts[2], dt.aMonth())
self.assertEqual(int(dts[3]), dt.year())
self.assertEqual(int(times[0]), dt.h_24())
self.assertEqual(int(times[1]), dt.minute())
self.assertEqual(int(times[2]), int(dt.second()))
self.assertEqual(dts[5], "%+03d%02d" % divmod((-offset / 60), 60))
def testInternationalDateformat(self):
for year in (1990, 2001, 2020):
for month in (1, 12):
for day in (1, 12, 28, 31):
try:
d_us = DateTime("%d/%d/%d" % (year, month, day))
except Exception:
continue
d_int = DateTime("%d.%d.%d" % (day, month, year),
datefmt="international")
self.assertEqual(d_us, d_int)
d_int = DateTime("%d/%d/%d" % (day, month, year),
datefmt="international")
self.assertEqual(d_us, d_int)
def test_intl_format_hyphen(self):
d_jan = DateTime('2011-01-11 GMT')
d_nov = DateTime('2011-11-01 GMT')
d_us = DateTime('11-01-2011 GMT')
d_int = DateTime('11-01-2011 GMT', datefmt="international")
self.assertNotEqual(d_us, d_int)
self.assertEqual(d_us, d_nov)
self.assertEqual(d_int, d_jan)
def test_calcTimezoneName(self):
from DateTime.interfaces import TimeError
timezone_dependent_epoch = 2177452800
try:
DateTime()._calcTimezoneName(timezone_dependent_epoch, 0)
except TimeError:
self.fail('Zope Collector issue #484 (negative time bug): '
'TimeError raised')
def testStrftimeTZhandling(self):
# strftime timezone testing
# This is a test for collector issue #1127
format = '%Y-%m-%d %H:%M %Z'
dt = DateTime('Wed, 19 Nov 2003 18:32:07 -0215')
dt_string = dt.strftime(format)
dt_local = dt.toZone(_findLocalTimeZoneName(0))
dt_localstring = dt_local.strftime(format)
self.assertEqual(dt_string, dt_localstring)
def testStrftimeFarDates(self):
# Checks strftime in dates <= 1900 or >= 2038
dt = DateTime('1900/01/30')
self.assertEqual(dt.strftime('%d/%m/%Y'), '30/01/1900')
dt = DateTime('2040/01/30')
self.assertEqual(dt.strftime('%d/%m/%Y'), '30/01/2040')
def testZoneInFarDates(self):
# Checks time zone in dates <= 1900 or >= 2038
dt1 = DateTime('2040/01/30 14:33 GMT+1')
dt2 = DateTime('2040/01/30 11:33 GMT-2')
self.assertEqual(dt1.strftime('%d/%m/%Y %H:%M'),
dt2.strftime('%d/%m/%Y %H:%M'))
@unittest.skipIf(
IS_PYPY,
"Using Non-Ascii characters for strftime doesn't work in PyPy"
"https://bitbucket.org/pypy/pypy/issues/2161/pypy3-strftime-does-not-accept-unicode" # noqa: E501 line too long
)
def testStrftimeStr(self):
dt = DateTime('2002-05-02T08:00:00+00:00')
uchar = b'\xc3\xa0'.decode('utf-8')
ok = dt.strftime('Le %d/%m/%Y a %Hh%M').replace('a', uchar)
ustr = b'Le %d/%m/%Y \xc3\xa0 %Hh%M'.decode('utf-8')
self.assertEqual(dt.strftime(ustr), ok)
def testTimezoneNaiveHandling(self):
# checks that we assign timezone naivity correctly
dt = DateTime('2007-10-04T08:00:00+00:00')
self.assertFalse(dt.timezoneNaive(),
'error with naivity handling in __parse_iso8601')
dt = DateTime('2007-10-04T08:00:00Z')
self.assertFalse(dt.timezoneNaive(),
'error with naivity handling in __parse_iso8601')
dt = DateTime('2007-10-04T08:00:00')
self.assertTrue(dt.timezoneNaive(),
'error with naivity handling in __parse_iso8601')
dt = DateTime('2007/10/04 15:12:33.487618 GMT+1')
self.assertFalse(dt.timezoneNaive(),
'error with naivity handling in _parse')
dt = DateTime('2007/10/04 15:12:33.487618')
self.assertTrue(dt.timezoneNaive(),
'error with naivity handling in _parse')
dt = DateTime()
self.assertFalse(dt.timezoneNaive(),
'error with naivity for current time')
s = '2007-10-04T08:00:00'
dt = DateTime(s)
self.assertEqual(s, dt.ISO8601())
s = '2007-10-04T08:00:00+00:00'
dt = DateTime(s)
self.assertEqual(s, dt.ISO8601())
def testConversions(self):
sdt0 = datetime.now() # this is a timezone naive datetime
dt0 = DateTime(sdt0)
self.assertTrue(dt0.timezoneNaive(), (sdt0, dt0))
sdt1 = datetime(2007, 10, 4, 18, 14, 42, 580, pytz.utc)
dt1 = DateTime(sdt1)
self.assertFalse(dt1.timezoneNaive(), (sdt1, dt1))
# convert back
sdt2 = dt0.asdatetime()
self.assertEqual(sdt0, sdt2)
sdt3 = dt1.utcdatetime() # this returns a timezone naive datetime
self.assertEqual(sdt1.hour, sdt3.hour)
dt4 = DateTime('2007-10-04T10:00:00+05:00')
sdt4 = datetime(2007, 10, 4, 5, 0)
self.assertEqual(dt4.utcdatetime(), sdt4)
self.assertEqual(dt4.asdatetime(), sdt4.replace(tzinfo=pytz.utc))
dt5 = DateTime('2007-10-23 10:00:00 US/Eastern')
tz = pytz.timezone('US/Eastern')
sdt5 = datetime(2007, 10, 23, 10, 0, tzinfo=tz)
dt6 = DateTime(sdt5)
self.assertEqual(dt5.asdatetime(), sdt5)
self.assertEqual(dt6.asdatetime(), sdt5)
self.assertEqual(dt5, dt6)
self.assertEqual(dt5.asdatetime().tzinfo, tz)
self.assertEqual(dt6.asdatetime().tzinfo, tz)
def testBasicTZ(self):
# psycopg2 supplies it's own tzinfo instances, with no `zone` attribute
tz = FixedOffset(60, 'GMT+1')
dt1 = datetime(2008, 8, 5, 12, 0, tzinfo=tz)
DT = DateTime(dt1)
dt2 = DT.asdatetime()
offset1 = dt1.tzinfo.utcoffset(dt1)
offset2 = dt2.tzinfo.utcoffset(dt2)
self.assertEqual(offset1, offset2)
def testEDTTimezone(self):
# should be able to parse EDT timezones: see lp:599856.
dt = DateTime("Mon, 28 Jun 2010 10:12:25 EDT")
self.assertEqual(dt.Day(), 'Monday')
self.assertEqual(dt.day(), 28)
self.assertEqual(dt.Month(), 'June')
self.assertEqual(dt.timezone(), 'GMT-4')
def testParseISO8601(self):
parsed = DateTime()._parse_iso8601('2010-10-10')
self.assertEqual(parsed, (2010, 10, 10, 0, 0, 0, 'GMT+0000'))
def test_interface(self):
from DateTime.interfaces import IDateTime
self.assertTrue(IDateTime.providedBy(DateTime()))
def test_security(self):
dt = DateTime()
self.assertEqual(dt.__roles__, None)
self.assertEqual(dt.__allow_access_to_unprotected_subobjects__, 1)
def test_format(self):
dt = DateTime(1968, 3, 10, 23, 45, 0, 'Europe/Vienna')
fmt = '%d.%m.%Y %H:%M'
result = dt.strftime(fmt)
unformatted_result = '1968/03/10 23:45:00 Europe/Vienna'
self.assertEqual(result, f'{dt:%d.%m.%Y %H:%M}')
self.assertEqual(unformatted_result, f'{dt}')
self.assertEqual(unformatted_result, f'{dt}')
self.assertEqual(result, f'{dt:{fmt}}')
self.assertEqual(unformatted_result, f'{dt:}')
self.assertEqual(unformatted_result, f'{dt}')
def test_suite():
import doctest
return unittest.TestSuite([
unittest.defaultTestLoader.loadTestsFromTestCase(DateTimeTests),
doctest.DocFileSuite('DateTime.txt', package='DateTime'),
doctest.DocFileSuite('pytz.txt', package='DateTime'),
])

View File

@ -0,0 +1,239 @@
# don't import any costly modules
import os
import sys
report_url = (
"https://github.com/pypa/setuptools/issues/new?template=distutils-deprecation.yml"
)
def warn_distutils_present():
if 'distutils' not in sys.modules:
return
import warnings
warnings.warn(
"Distutils was imported before Setuptools, but importing Setuptools "
"also replaces the `distutils` module in `sys.modules`. This may lead "
"to undesirable behaviors or errors. To avoid these issues, avoid "
"using distutils directly, ensure that setuptools is installed in the "
"traditional way (e.g. not an editable install), and/or make sure "
"that setuptools is always imported before distutils."
)
def clear_distutils():
if 'distutils' not in sys.modules:
return
import warnings
warnings.warn(
"Setuptools is replacing distutils. Support for replacing "
"an already imported distutils is deprecated. In the future, "
"this condition will fail. "
f"Register concerns at {report_url}"
)
mods = [
name
for name in sys.modules
if name == "distutils" or name.startswith("distutils.")
]
for name in mods:
del sys.modules[name]
def enabled():
"""
Allow selection of distutils by environment variable.
"""
which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'local')
if which == 'stdlib':
import warnings
warnings.warn(
"Reliance on distutils from stdlib is deprecated. Users "
"must rely on setuptools to provide the distutils module. "
"Avoid importing distutils or import setuptools first, "
"and avoid setting SETUPTOOLS_USE_DISTUTILS=stdlib. "
f"Register concerns at {report_url}"
)
return which == 'local'
def ensure_local_distutils():
import importlib
clear_distutils()
# With the DistutilsMetaFinder in place,
# perform an import to cause distutils to be
# loaded from setuptools._distutils. Ref #2906.
with shim():
importlib.import_module('distutils')
# check that submodules load as expected
core = importlib.import_module('distutils.core')
assert '_distutils' in core.__file__, core.__file__
assert 'setuptools._distutils.log' not in sys.modules
def do_override():
"""
Ensure that the local copy of distutils is preferred over stdlib.
See https://github.com/pypa/setuptools/issues/417#issuecomment-392298401
for more motivation.
"""
if enabled():
warn_distutils_present()
ensure_local_distutils()
class _TrivialRe:
def __init__(self, *patterns) -> None:
self._patterns = patterns
def match(self, string):
return all(pat in string for pat in self._patterns)
class DistutilsMetaFinder:
def find_spec(self, fullname, path, target=None):
# optimization: only consider top level modules and those
# found in the CPython test suite.
if path is not None and not fullname.startswith('test.'):
return None
method_name = 'spec_for_{fullname}'.format(**locals())
method = getattr(self, method_name, lambda: None)
return method()
def spec_for_distutils(self):
if self.is_cpython():
return None
import importlib
import importlib.abc
import importlib.util
try:
mod = importlib.import_module('setuptools._distutils')
except Exception:
# There are a couple of cases where setuptools._distutils
# may not be present:
# - An older Setuptools without a local distutils is
# taking precedence. Ref #2957.
# - Path manipulation during sitecustomize removes
# setuptools from the path but only after the hook
# has been loaded. Ref #2980.
# In either case, fall back to stdlib behavior.
return None
class DistutilsLoader(importlib.abc.Loader):
def create_module(self, spec):
mod.__name__ = 'distutils'
return mod
def exec_module(self, module):
pass
return importlib.util.spec_from_loader(
'distutils', DistutilsLoader(), origin=mod.__file__
)
@staticmethod
def is_cpython():
"""
Suppress supplying distutils for CPython (build and tests).
Ref #2965 and #3007.
"""
return os.path.isfile('pybuilddir.txt')
def spec_for_pip(self):
"""
Ensure stdlib distutils when running under pip.
See pypa/pip#8761 for rationale.
"""
if sys.version_info >= (3, 12) or self.pip_imported_during_build():
return
clear_distutils()
self.spec_for_distutils = lambda: None
@classmethod
def pip_imported_during_build(cls):
"""
Detect if pip is being imported in a build script. Ref #2355.
"""
import traceback
return any(
cls.frame_file_is_setup(frame) for frame, line in traceback.walk_stack(None)
)
@staticmethod
def frame_file_is_setup(frame):
"""
Return True if the indicated frame suggests a setup.py file.
"""
# some frames may not have __file__ (#2940)
return frame.f_globals.get('__file__', '').endswith('setup.py')
def spec_for_sensitive_tests(self):
"""
Ensure stdlib distutils when running select tests under CPython.
python/cpython#91169
"""
clear_distutils()
self.spec_for_distutils = lambda: None
sensitive_tests = (
[
'test.test_distutils',
'test.test_peg_generator',
'test.test_importlib',
]
if sys.version_info < (3, 10)
else [
'test.test_distutils',
]
)
for name in DistutilsMetaFinder.sensitive_tests:
setattr(
DistutilsMetaFinder,
f'spec_for_{name}',
DistutilsMetaFinder.spec_for_sensitive_tests,
)
DISTUTILS_FINDER = DistutilsMetaFinder()
def add_shim():
DISTUTILS_FINDER in sys.meta_path or insert_shim()
class shim:
def __enter__(self) -> None:
insert_shim()
def __exit__(self, exc: object, value: object, tb: object) -> None:
_remove_shim()
def insert_shim():
sys.meta_path.insert(0, DISTUTILS_FINDER)
def _remove_shim():
try:
sys.meta_path.remove(DISTUTILS_FINDER)
except ValueError:
pass
if sys.version_info < (3, 12):
# DistutilsMetaFinder can only be disabled in Python < 3.12 (PEP 632)
remove_shim = _remove_shim

View File

@ -0,0 +1 @@
__import__('_distutils_hack').do_override()

View File

@ -0,0 +1,20 @@
This package contains a modified version of ca-bundle.crt:
ca-bundle.crt -- Bundle of CA Root Certificates
This is a bundle of X.509 certificates of public Certificate Authorities
(CA). These were automatically extracted from Mozilla's root certificates
file (certdata.txt). This file can be found in the mozilla source tree:
https://hg.mozilla.org/mozilla-central/file/tip/security/nss/lib/ckfw/builtins/certdata.txt
It contains the certificates in PEM format and therefore
can be directly used with curl / libcurl / php_curl, or with
an Apache+mod_ssl webserver for SSL client authentication.
Just configure this file as the SSLCACertificateFile.#
***** BEGIN LICENSE BLOCK *****
This Source Code Form is subject to the terms of the Mozilla Public License,
v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain
one at http://mozilla.org/MPL/2.0/.
***** END LICENSE BLOCK *****
@(#) $RCSfile: certdata.txt,v $ $Revision: 1.80 $ $Date: 2011/11/03 15:11:58 $

View File

@ -0,0 +1,77 @@
Metadata-Version: 2.2
Name: certifi
Version: 2025.1.31
Summary: Python package for providing Mozilla's CA Bundle.
Home-page: https://github.com/certifi/python-certifi
Author: Kenneth Reitz
Author-email: me@kennethreitz.com
License: MPL-2.0
Project-URL: Source, https://github.com/certifi/python-certifi
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)
Classifier: Natural Language :: English
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Python: >=3.6
License-File: LICENSE
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: home-page
Dynamic: license
Dynamic: project-url
Dynamic: requires-python
Dynamic: summary
Certifi: Python SSL Certificates
================================
Certifi provides Mozilla's carefully curated collection of Root Certificates for
validating the trustworthiness of SSL certificates while verifying the identity
of TLS hosts. It has been extracted from the `Requests`_ project.
Installation
------------
``certifi`` is available on PyPI. Simply install it with ``pip``::
$ pip install certifi
Usage
-----
To reference the installed certificate authority (CA) bundle, you can use the
built-in function::
>>> import certifi
>>> certifi.where()
'/usr/local/lib/python3.7/site-packages/certifi/cacert.pem'
Or from the command line::
$ python -m certifi
/usr/local/lib/python3.7/site-packages/certifi/cacert.pem
Enjoy!
.. _`Requests`: https://requests.readthedocs.io/en/master/
Addition/Removal of Certificates
--------------------------------
Certifi does not support any addition/removal or other modification of the
CA trust store content. This project is intended to provide a reliable and
highly portable root of trust to python deployments. Look to upstream projects
for methods to use alternate trust.

View File

@ -0,0 +1,14 @@
certifi-2025.1.31.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
certifi-2025.1.31.dist-info/LICENSE,sha256=6TcW2mucDVpKHfYP5pWzcPBpVgPSH2-D8FPkLPwQyvc,989
certifi-2025.1.31.dist-info/METADATA,sha256=t5kcT5aGu0dQ6_psUNZYTqnC0uCRnponewm3uYjeHbg,2451
certifi-2025.1.31.dist-info/RECORD,,
certifi-2025.1.31.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
certifi-2025.1.31.dist-info/top_level.txt,sha256=KMu4vUCfsjLrkPbSNdgdekS-pVJzBAJFO__nI8NF6-U,8
certifi/__init__.py,sha256=neIaAf7BM36ygmQCmy-ZsSyjnvjWghFeu13wwEAnjj0,94
certifi/__main__.py,sha256=xBBoj905TUWBLRGANOcf7oi6e-3dMP4cEoG9OyMs11g,243
certifi/__pycache__/__init__.cpython-312.pyc,,
certifi/__pycache__/__main__.cpython-312.pyc,,
certifi/__pycache__/core.cpython-312.pyc,,
certifi/cacert.pem,sha256=xVsh-Qf3-G1IrdCTVS-1ZRdJ_1-GBQjMu0I9bB-9gMc,297255
certifi/core.py,sha256=qRDDFyXVJwTB_EmoGppaXU_R9qCZvhl-EzxPMuV3nTA,4426
certifi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0

View File

@ -0,0 +1,5 @@
Wheel-Version: 1.0
Generator: setuptools (75.8.0)
Root-Is-Purelib: true
Tag: py3-none-any

View File

@ -0,0 +1,4 @@
from .core import contents, where
__all__ = ["contents", "where"]
__version__ = "2025.01.31"

View File

@ -0,0 +1,12 @@
import argparse
from certifi import contents, where
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--contents", action="store_true")
args = parser.parse_args()
if args.contents:
print(contents())
else:
print(where())

File diff suppressed because it is too large Load Diff

Some files were not shown because too many files have changed in this diff Show More