-
Notifications
You must be signed in to change notification settings - Fork 0
/
uigetdatetime.m
164 lines (115 loc) · 4.7 KB
/
uigetdatetime.m
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
function newTime = uigetdatetime(varargin)
% function newTime = uigetdatetime(varargin)
%
% uigetdatetime launch a calendar widget to select a date/time
% newTime = uigetdatetime() launches a calendar widget with the current
% date and time as the initially selected date/time at 0 hrs offset from
% UTC
%
% newTime = uigetdatetime('displayTime',t) launches a calendar widget
% with `t` as the initially selected date/time at 0 hrs offset from UTC
% where `t` is a matlab serial date (datenum), datestring (datestr), or
% datetime object
%
% newTime = uigetdatetime('displayTime',t,'displayTimeZone',tz) launches
% a calendar width with `t` as the initially selected date/time with UTC
% offset associated with string `tz` where tz is a java timezone string.
% to see a list of allowed timezones:
% `tzList = cell(java.util.TimeZone.getAvailableIDs)`
%
% copyleft 2018 Peter Cook (peter dot cook AT colorado dot edu)
% with help from yair altman (altmany at gmail dot com)
%parse inputs
p = inputParser;
defaultDisplayTime = now();
defaultTimeZone = 'UTC';
tzList = cell(java.util.TimeZone.getAvailableIDs);
addParameter(p,'displayTime', defaultDisplayTime,...
@(x) assert(isnumeric(x) || isa(x,'char') || isa(x,'datetime')));
addParameter(p,'displayTimeZone', defaultTimeZone, @(x) ismember(x,tzList))
% addOptional(p,'displayTimeZone', defaultTimeZone)
parse(p,varargin{:})
displayTime = p.Results.displayTime;
displayTimeZone = p.Results.displayTimeZone;
%/parse inputs
%convert input date to datevec
if isnumeric(displayTime)
currentDatetime = datevec(datetime(displayTime,'convertFrom','datenum',...
'TimeZone',displayTimeZone));
elseif isa(displayTime,'char')
try
currentDatetime = datevec(datetime(displayTime,...
'convertFrom','datestr','TimeZone',displayTimeZone));
catch
currentDatetime = datevec(datetime(now(),'convertFrom','datenum',...
'TimeZone',displayTimeZone));
end
elseif isa(displayTime,'datetime')
currentDatetime = datevec(displayTime);
else
currentDatetime = datevec(datetime(now(),'convertFrom','datenum',...
'TimeZone',displayTimeZone));
end
%convert input timezone string to TimeZone object
try
timeZone = java.util.TimeZone.getTimeZone(displayTimeZone);
catch
displayTimeZone = 'UTC';
timeZone = java.util.TimeZone.getTimeZone(displayTimeZone);
end
%init Calendar object
jCal = java.util.Calendar.getInstance();
jCal.set(java.util.Calendar.YEAR, currentDatetime(1))
jCal.set(java.util.Calendar.MONTH, currentDatetime(2)-1)
jCal.set(java.util.Calendar.DAY_OF_MONTH, currentDatetime(3))
jCal.set(java.util.Calendar.HOUR_OF_DAY, currentDatetime(4))
jCal.set(java.util.Calendar.MINUTE, currentDatetime(5))
jCal.set(java.util.Calendar.SECOND, currentDatetime(6))
% jCal.setTimeZone(timeZone)
jCal.setTimeZone(java.util.TimeZone.getDefault())
%init figure for DateChooserPanel
hCalendar = figure();
figPos = get(0,'ScreenSize');
hCalendar.Position = [figPos(1)+figPos(3)/2-272/2,...
figPos(2)+figPos(4)/2-219/2,272,219];
hCalendar.MenuBar = 'none';
hCalendar.Name = 'Choose Date and Time Range';
hCalendar.NumberTitle = 'off';
% init DateChooserPanel object
jPanel = com.jidesoft.combobox.DateChooserPanel;
jPanel.setTimeDisplayed(true)
% jPanel.setTimeZone(timeZone)
jPanel.setTimeZone(java.util.TimeZone.getDefault())
jPanel.setSelectedCalendar(jCal)
%get java guts of DateChooserPanel Object
[hPanel,~] = javacomponent(jPanel,[0,0,272,219],hCalendar);
set(hPanel,'ShowTodayButton',false)
set(hPanel,'ShowNoneButton',false)
set(hPanel,'ItemStateChangedCallback',...
@(hPanel,evnt)dateTimeCallbackFcn(hPanel,evnt,hCalendar))
%cribbed from inputdlg.m
if ishghandle(hCalendar)
% Go into uiwait if the figure handle is still valid.
% This is mostly the case during regular use.
c = matlab.ui.internal.dialog.DialogUtils.disableAllWindowsSafely();
uiwait(hCalendar);
delete(c);
end
%\cribbed from inputdlg.m
function dateTimeCallbackFcn(~,~,hCalendar)
hModel = hPanel.getSelectionModel;
%get Date object
jSelectedDate = hModel.getSelectedDate();
%if a date is selected assign the value of newTime
if ~isempty(jSelectedDate)
newTime = datetime(jSelectedDate.getTime/1000,'convertFrom','posixtime','TimeZone',char(java.util.TimeZone.getDefault.getID));
end
%delete figure/container
delete(hCalendar)
end
%if the figure was deleted instead of the "OK" button pressed, return the
%input datetime
if ~exist('newTime','var')
newTime = datetime(currentDatetime);
end
end