Skip to content

Commit

Permalink
SAK-42991 remove more timezone display manipulation in Samigo (sakaip…
Browse files Browse the repository at this point in the history
…roject#7923)

(cherry picked from commit ad25f3f)
  • Loading branch information
ottenhoff authored and ern committed Mar 11, 2020
1 parent afc8426 commit 5c725b6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public boolean clearLocalTimeZone(String userId) {

@Override
public String timeFormat(Date time, Locale locale, int df) {
if (time == null || locale == null) return "";
log.debug("timeFormat: {}, {}, {}", time.toString(), locale.toString(), df);

DateFormat dsf = DateFormat.getTimeInstance(df, locale);
Expand All @@ -119,6 +120,7 @@ public String timeFormat(Date time, Locale locale, int df) {

@Override
public String dateFormat(Date date, Locale locale, int df) {
if (date == null || locale == null) return "";
log.debug("dateFormat: {}, {}, {}", date.toString(), locale.toString(), df);

DateFormat dsf = DateFormat.getDateInstance(df, locale);
Expand All @@ -128,6 +130,7 @@ public String dateFormat(Date date, Locale locale, int df) {

@Override
public String dateTimeFormat(Date date, Locale locale, int df) {
if (date == null || locale == null) return "";
log.debug("dateTimeFormat: {}, {}, {}", date.toString(), locale.toString(), df);

DateFormat dsf = DateFormat.getDateTimeInstance(df, df, locale);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.sakaiproject.tool.assessment.ui.bean.author;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
Expand Down Expand Up @@ -55,6 +56,7 @@
import org.sakaiproject.site.api.Group;
import org.sakaiproject.site.api.Site;
import org.sakaiproject.site.cover.SiteService;
import org.sakaiproject.time.api.UserTimeService;
import org.sakaiproject.tool.api.SessionManager;
import org.sakaiproject.tool.api.ToolManager;
import org.sakaiproject.tool.api.ToolSession;
Expand Down Expand Up @@ -245,6 +247,8 @@ public class AssessmentSettingsBean implements Serializable {
private ToolManager toolManager;
@Resource(name = "org.sakaiproject.util.api.FormattedText")
private FormattedText formattedText;
@Resource(name = "org.sakaiproject.time.api.UserTimeService")
private UserTimeService userTimeService;

/*
* Creates a new AssessmentBean object.
Expand Down Expand Up @@ -1143,11 +1147,15 @@ public void setIpAddresses(String ipAddresses) {
* @param date Date object
* @return date String "MM-dd-yyyy hh:mm:ss a"
*/
private String getDisplayFormatFromDate(Date date, boolean manipulateTimezoneForClient) {
private String getDisplayFormatFromDate(Date date) {
if (date == null) return StringUtils.EMPTY;

if (displayFormat == null) {
setDisplayFormat(ContextUtil.getLocalizedString("org.sakaiproject.tool.assessment.bundle.GeneralMessages","output_data_picker_w_sec"));
}

try {
return tu.getDisplayDateTime(displayFormat, date, manipulateTimezoneForClient);
return tu.getDisplayDateTime(displayFormat, date);
}
catch (Exception ex) {
// we will leave it as an empty string
Expand All @@ -1161,7 +1169,7 @@ public String getStartDateInClientTimezoneString() {
return this.originalStartDateString;
}
else {
return getDisplayFormatFromDate(startDate, true);
return userTimeService.dateTimeFormat(startDate, new ResourceLoader().getLocale(), DateFormat.MEDIUM);
}
}

Expand All @@ -1170,7 +1178,7 @@ public String getStartDateString() {
return this.originalStartDateString;
}
else {
return getDisplayFormatFromDate(startDate, false);
return getDisplayFormatFromDate(startDate);
}
}

Expand Down Expand Up @@ -1201,7 +1209,7 @@ public String getDueDateInClientTimezoneString() {
return this.originalDueDateString;
}
else {
return getDisplayFormatFromDate(dueDate, true);
return userTimeService.dateTimeFormat(dueDate, new ResourceLoader().getLocale(), DateFormat.MEDIUM);
}
}

Expand All @@ -1210,7 +1218,7 @@ public String getDueDateString() {
return this.originalDueDateString;
}
else {
return getDisplayFormatFromDate(dueDate, false);
return getDisplayFormatFromDate(dueDate);
}
}

Expand Down Expand Up @@ -1242,7 +1250,7 @@ public String getRetractDateString()
return this.originalRetractDateString;
}
else {
return getDisplayFormatFromDate(retractDate, false);
return getDisplayFormatFromDate(retractDate);
}
}

Expand Down Expand Up @@ -1276,7 +1284,7 @@ public String getFeedbackDateInClientTimezoneString() {
return this.originalFeedbackDateString;
}
else {
return getDisplayFormatFromDate(feedbackDate, true);
return userTimeService.dateTimeFormat(feedbackDate, new ResourceLoader().getLocale(), DateFormat.MEDIUM);
}
}

Expand All @@ -1285,7 +1293,7 @@ public String getFeedbackDateString() {
return this.originalFeedbackDateString;
}
else {
return getDisplayFormatFromDate(feedbackDate, false);
return getDisplayFormatFromDate(feedbackDate);
}
}

Expand Down Expand Up @@ -1315,7 +1323,7 @@ public String getFeedbackEndDateInClientTimezoneString() {
return this.originalFeedbackEndDateString;
}
else {
return getDisplayFormatFromDate(feedbackEndDate, true);
return userTimeService.dateTimeFormat(feedbackEndDate, new ResourceLoader().getLocale(), DateFormat.MEDIUM);
}
}

Expand All @@ -1324,7 +1332,7 @@ public String getFeedbackEndDateString() {
return this.originalFeedbackEndDateString;
}
else {
return getDisplayFormatFromDate(feedbackEndDate, false);
return getDisplayFormatFromDate(feedbackEndDate);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package org.sakaiproject.tool.assessment.ui.bean.author;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
Expand Down Expand Up @@ -62,6 +63,7 @@
import org.sakaiproject.site.api.Group;
import org.sakaiproject.site.api.Site;
import org.sakaiproject.site.cover.SiteService;
import org.sakaiproject.time.api.UserTimeService;
import org.sakaiproject.tool.assessment.api.SamigoApiFactory;
import org.sakaiproject.tool.assessment.data.dao.assessment.ExtendedTime;
import org.sakaiproject.tool.assessment.data.dao.authz.AuthorizationData;
Expand Down Expand Up @@ -245,6 +247,8 @@ public class PublishedAssessmentSettingsBean implements Serializable {
private ToolManager toolManager;
@Resource(name = "org.sakaiproject.util.api.FormattedText")
private FormattedText formattedText;
@Resource(name = "org.sakaiproject.time.api.UserTimeService")
private UserTimeService userTimeService;

/*
* Creates a new AssessmentBean object.
Expand Down Expand Up @@ -1113,11 +1117,11 @@ public void setIpAddresses(String ipAddresses) {
* @param date Date object
* @return date String "MM-dd-yyyy hh:mm:ss a"
*/
private String getDisplayFormatFromDate(Date date, boolean manipulateTimezoneForClient) {
private String getDisplayFormatFromDate(Date date) {
if (date == null) return StringUtils.EMPTY;

try {
return tu.getDisplayDateTime(displayFormat, date, manipulateTimezoneForClient);
return tu.getDisplayDateTime(displayFormat, date);
}
catch (Exception ex) {
// we will leave it as an empty string
Expand All @@ -1131,7 +1135,7 @@ public String getStartDateInClientTimezoneString() {
return this.originalStartDateString;
}
else {
return getDisplayFormatFromDate(startDate, true);
return userTimeService.dateTimeFormat(startDate, new ResourceLoader().getLocale(), DateFormat.MEDIUM);
}
}

Expand All @@ -1140,7 +1144,7 @@ public String getStartDateString() {
return this.originalStartDateString;
}
else {
return getDisplayFormatFromDate(startDate, false);
return getDisplayFormatFromDate(startDate);
}
}

Expand Down Expand Up @@ -1171,7 +1175,7 @@ public String getDueDateInClientTimezoneString() {
return this.originalDueDateString;
}
else {
return getDisplayFormatFromDate(dueDate, true);
return userTimeService.dateTimeFormat(dueDate, new ResourceLoader().getLocale(), DateFormat.MEDIUM);
}
}

Expand All @@ -1180,7 +1184,7 @@ public String getDueDateString() {
return this.originalDueDateString;
}
else {
return getDisplayFormatFromDate(dueDate, false);
return getDisplayFormatFromDate(dueDate);
}
}

Expand Down Expand Up @@ -1212,7 +1216,7 @@ public String getRetractDateString()
return this.originalRetractDateString;
}
else {
return getDisplayFormatFromDate(retractDate, false);
return getDisplayFormatFromDate(retractDate);
}
}

Expand Down Expand Up @@ -1243,7 +1247,7 @@ public String getFeedbackDateInClientTimezoneString() {
return this.originalFeedbackDateString;
}
else {
return getDisplayFormatFromDate(feedbackDate, true);
return userTimeService.dateTimeFormat(feedbackDate, new ResourceLoader().getLocale(), DateFormat.MEDIUM);
}
}

Expand All @@ -1252,7 +1256,7 @@ public String getFeedbackDateString() {
return this.originalFeedbackDateString;
}
else {
return getDisplayFormatFromDate(feedbackDate, false);
return getDisplayFormatFromDate(feedbackDate);
}
}

Expand Down Expand Up @@ -1282,7 +1286,7 @@ public String getFeedbackEndDateInClientTimezoneString() {
return this.originalFeedbackEndDateString;
}
else {
return getDisplayFormatFromDate(feedbackEndDate, true);
return userTimeService.dateTimeFormat(feedbackEndDate, new ResourceLoader().getLocale(), DateFormat.MEDIUM);
}
}

Expand All @@ -1291,7 +1295,7 @@ public String getFeedbackEndDateString() {
return this.originalFeedbackEndDateString;
}
else {
return getDisplayFormatFromDate(feedbackEndDate, false);
return getDisplayFormatFromDate(feedbackEndDate);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,14 +345,12 @@ private void processFile(String fileName, String uploadFile, boolean isRespondus
List list = assessmentService.getBasicInfoOfAllActiveAssessments(
AssessmentFacadeQueries.TITLE,true);
TimeUtil tu = new TimeUtil();
String display_dateFormat= ContextUtil.getLocalizedString("org.sakaiproject.tool.assessment.bundle.GeneralMessages","output_data_picker_w_sec");
SimpleDateFormat displayFormat = new SimpleDateFormat(display_dateFormat, new ResourceLoader().getLocale());
Iterator iter = list.iterator();
while (iter.hasNext()) {
AssessmentFacade assessmentFacade= (AssessmentFacade) iter.next();
assessmentFacade.setTitle(ComponentManager.get(FormattedText.class).convertFormattedTextToPlaintext(assessmentFacade.getTitle()));
try {
String lastModifiedDateDisplay = tu.getDisplayDateTime(displayFormat, assessmentFacade.getLastModifiedDate(), false);
String lastModifiedDateDisplay = tu.getDateTimeWithTimezoneConversion(assessmentFacade.getLastModifiedDate());
assessmentFacade.setLastModifiedDateForDisplay(lastModifiedDateDisplay);
} catch (Exception ex) {
log.warn("Unable to format date: " + ex.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

package org.sakaiproject.tool.assessment.ui.listener.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
Expand All @@ -47,8 +46,6 @@
@Slf4j
public class TimeUtil
{
private static final String ISO_8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZZ";
private static DateTimeFormatter dtf = DateTimeFormat.forPattern(ISO_8601_DATE_FORMAT);

private TimeZone m_client_timezone= null;
private TimeZone m_server_timezone= null;
Expand All @@ -58,48 +55,19 @@ public TimeUtil() {
m_server_timezone= TimeZone.getDefault();
}


/**
* Convert a Date representation of date and time in the server TimeZone
* to String representation of date and time in client TimeZone
* used for display.
* tz1 is the client timezone, tz2 is the server timezone
*/

private String convertFromServerDateToTimeZone2String(SimpleDateFormat ndf, Date tz2Date, TimeZone tz1) {
Calendar cal1= new GregorianCalendar(tz1);
ndf.setCalendar(cal1);
String clientStr= ndf.format(tz2Date);

return clientStr;
}

/*
* @deprecated use UserTimeService instead
* This will return a formatted date/time with or without adjustment for client time zone.
* This will return a formatted date/time without adjustment for client time zone.
* If instructor is located in Michigan and teaches on Sakai based in Chicago,
* the date should stay stable in the server timezone when using date/time picker. Previous
* behavior meant the date would be constantly manipulated by client timezone because of the
* convertFromServerDateToTimeZone2String manipulation below.
*/
public String getDisplayDateTime(SimpleDateFormat ndf, Date serverDate, boolean manipulateTimezoneForClient) {
public String getDisplayDateTime(SimpleDateFormat ndf, Date serverDate) {
//we can't format a null date
if (serverDate == null) {
return "";
}

try {
if (manipulateTimezoneForClient && m_client_timezone !=null && m_server_timezone!=null && !m_client_timezone.hasSameRules(m_server_timezone)) {
String sdf = ndf.toPattern();
// If we are going to manipulate the timezone for client browser, let's be clear and show user the timezone.
if (StringUtils.containsNone(sdf, "zZ")) {
ndf = new SimpleDateFormat(sdf + " z");
}
return convertFromServerDateToTimeZone2String (ndf, serverDate, m_client_timezone);
}
else {
return ndf.format(serverDate);
}
return ndf.format(serverDate);
}
catch (RuntimeException e){
log.warn("can not format the Date to a string", e);
Expand Down

0 comments on commit 5c725b6

Please sign in to comment.