Coverage for C: \ Users \ peaco \ OneDrive \ Documents \ GitHub \ mt_metadata \ mt_metadata \ common \ time_period.py: 100%
29 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-10 00:11 -0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-10 00:11 -0800
1# =====================================================
2# Imports
3# =====================================================
4from typing import Annotated
6import numpy as np
7import pandas as pd
8from pydantic import Field, field_validator, ValidationInfo
10from mt_metadata.base import MetadataBase
11from mt_metadata.common.mttime import MDate, MTime
14# =====================================================
15class TimePeriod(MetadataBase):
16 """
17 Time span of a period of time.
18 """
20 end: Annotated[
21 str | float | int | np.datetime64 | pd.Timestamp | MTime,
22 Field(
23 default_factory=lambda: MTime(time_stamp="1980-01-01T00:00:00+00:00"),
24 description="End date and time of collection in UTC.",
25 alias=None,
26 json_schema_extra={
27 "units": None,
28 "required": True,
29 "examples": "2020-02-04T16:23:45.453670+00:00",
30 "type": "string",
31 },
32 ),
33 ]
35 start: Annotated[
36 str | float | int | np.datetime64 | pd.Timestamp | MTime,
37 Field(
38 default_factory=lambda: MTime(),
39 description="Start date and time of collection in UTC.",
40 alias=None,
41 json_schema_extra={
42 "units": None,
43 "required": True,
44 "examples": "2020-02-01T09:23:45.453670+00:00",
45 "type": "string",
46 },
47 ),
48 ]
50 @field_validator("start", "end", mode="before")
51 @classmethod
52 def validate_time(cls, value, info: ValidationInfo) -> MTime:
53 """
54 Validate that the value is a valid time.
55 """
56 return MTime(time_stamp=value)
58 def start_is_default(self) -> bool:
59 """
60 Check if the start time is the default time.
61 """
62 return MTime(time_stamp=self.start).is_default()
64 def end_is_default(self) -> bool:
65 """
66 Check if the end time is the default time.
67 """
68 return MTime(time_stamp=self.end).is_default()
71class TimePeriodDate(MetadataBase):
72 """
73 Time span of a period of time.
74 """
76 end_date: Annotated[
77 str | float | int | np.datetime64 | pd.Timestamp | MTime | MDate,
78 Field(
79 default_factory=lambda: MDate(time_stamp="1980-01-01"),
80 description="End date and time of collection in UTC.",
81 alias=None,
82 json_schema_extra={
83 "units": None,
84 "required": True,
85 "examples": "2020-02-04",
86 "type": "string",
87 },
88 ),
89 ]
91 start_date: Annotated[
92 str | float | int | np.datetime64 | pd.Timestamp | MTime | MDate,
93 Field(
94 default_factory=lambda: MDate(time_stamp="1980-01-01"),
95 description="Start date and time of collection in UTC.",
96 alias=None,
97 json_schema_extra={
98 "units": None,
99 "required": True,
100 "examples": "2020-02-01",
101 "type": "string",
102 },
103 ),
104 ]
106 @field_validator("start_date", "end_date", mode="before")
107 @classmethod
108 def validate_time(cls, value, info: ValidationInfo) -> MTime:
109 """
110 Validate that the value is a valid time.
111 """
112 mt_date = MDate(time_stamp=value)
113 return mt_date.isodate()
115 def start_is_default(self) -> bool:
116 """
117 Check if the start time is the default time.
118 """
119 return MDate(time_stamp=self.start_date).is_default()
121 def end_is_default(self) -> bool:
122 """
123 Check if the end time is the default time.
124 """
125 return MDate(time_stamp=self.end_date).is_default()