-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatetimetools.py
71 lines (57 loc) · 2.42 KB
/
datetimetools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Copyright (c) 2024 Medical Imaging and Data Resource Center (MIDRC).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
import pandas as pd
from PySide6.QtCore import QDate, QDateTime, QTime, QTimeZone
def convert_date_to_milliseconds(date):
"""
Converts a date to milliseconds since epoch.
"""
if date is None:
return None
return QDateTime(date, QTime(), QTimeZone.utc()).toMSecsSinceEpoch()
def pandas_date_to_qdate(pandas_date):
"""
Convert a pandas Timestamp or datetime object to a PySide2 QDate object.
Parameters:
pandas_date (pd.Timestamp or datetime64): Pandas Timestamp or datetime object.
Returns:
QDate: PySide2 QDate object representing the same date.
"""
if isinstance(pandas_date, pd.Timestamp):
return QDate(pandas_date.year, pandas_date.month, pandas_date.day)
# else:
if isinstance(pandas_date, np.datetime64):
return numpy_datetime64_to_qdate(pandas_date)
# else:
raise ValueError("Input must be a Pandas Timestamp or datetime object")
def numpy_datetime64_to_qdate(numpy_datetime):
"""
Convert a NumPy datetime64 object to a PySide2 QDate object.
Parameters:
numpy_datetime (numpy.datetime64): NumPy datetime64 object.
Returns:
QDate: PySide2 QDate object representing the same date.
"""
# Extract year, month, and day from numpy datetime64
# year = np.datetime64(numpy_datetime, 'Y').astype(int)
# month = np.datetime64(numpy_datetime, 'M').astype(int)
# day = np.datetime64(numpy_datetime, 'D').astype(int)
# Convert numpy datetime64 to Python datetime
python_datetime = np.datetime64(numpy_datetime).astype('M8[D]').astype('O')
# Extract year, month, and day from Python datetime
year = python_datetime.year
month = python_datetime.month
day = python_datetime.day
return QDate(year, month, day)