Skip to content

[discussion] date server side time zone information #1686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.adobe.cq.forms.core.components.internal.models.v1.form;

import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Map;

Expand Down Expand Up @@ -99,4 +100,17 @@ public Object[] getDefault() {
return defaultValue != null ? defaultValue.clone() : null;
}

/**
* Returns the server timezone identifier for client-side consumption.
*
* @return server timezone identifier (e.g., "America/New_York", "Asia/Kolkata")
* @since com.adobe.cq.forms.core.components.models.form 5.12.0
*/
@Override
public String getServerTimezone() {
ZoneId serverZone = ZoneId.systemDefault();

return serverZone.getId();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,23 @@

import org.osgi.annotation.versioning.ConsumerType;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
* Interface for {@code Password} Sling Model used for the {@code /apps/core/fd/components/form/password/v1/password} component.
* Interface for {@code DateTime} Sling Model used for the {@code /apps/core/fd/components/form/datetime/v1/datetime} component.
*
* @since com.adobe.cq.forms.core.components.models.form 5.12.0
*/
@ConsumerType
public interface DateTime extends Field, DateTimeConstraint {

/**
* Returns the server timezone identifier for client-side consumption.
*
* @return server timezone identifier (e.g., "America/New_York", "Asia/Kolkata")
* @since com.adobe.cq.forms.core.components.models.form 5.12.0
*/
@JsonIgnore
String getServerTimezone();

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import io.wcm.testing.mock.aem.junit5.AemContextExtension;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;

@ExtendWith(AemContextExtension.class)
public class DateTimeImplTest {
Expand Down Expand Up @@ -71,7 +73,15 @@ void testGetLabel() {
void testGetName() {
DateTime datetime = Utils.getComponentUnderTest(PATH_DATETIME_CUSTOMIZED, DateTime.class, context);
assertEquals("abc", datetime.getName());
}

@Test
void testGetServerTimezone() {
DateTime dateTime = Utils.getComponentUnderTest(PATH_DATETIME_CUSTOMIZED, DateTime.class, context);
String timezone = dateTime.getServerTimezone();

assertNotNull(timezone);
assertFalse(timezone.isEmpty());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,31 @@ The following properties are written to JCR for this Form Date & Time component
11. './maximumMessage' - defines the maximum date and time message that will come when the selected date and time is after the maximum date and time
12. './default' - defines the default date and time that will be selected when the form is loaded

## Server Timezone Information

The component exposes server timezone information via data attributes for client-side consumption:

### Data Attributes Available
- `data-server-timezone-id` - Server timezone identifier (e.g., "America/New_York")

### Getting Current Date in Server Timezone

```javascript
// Get server timezone info from the datetime component
const datetimeComponent = document.querySelector('.cmp-adaptiveform-datetime');
const serverTimezoneId = datetimeComponent.dataset.serverTimezoneId;

// Get current date in server timezone
function getCurrentDateInServerTimezone() {
const now = new Date();
const serverDate = new Date(now.toLocaleString("en-US", {timeZone: serverTimezoneId}));
return serverDate.toISOString().slice(0, 16); // Format: YYYY-MM-DDTHH:mm
}

// Set current date as default value
const currentServerDate = getCurrentDateInServerTimezone();
```

## Client Libraries
The component provides a `core.forms.components.datetime.v1.runtime` client library category that contains the Javascript runtime for the component.
It should be added to a relevant site client library using the `embed` property.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
data-cmp-readonly="${datetime.readOnly ? 'true' : 'false'}"
id="${datetime.id}"
data-cmp-adaptiveformcontainer-path="${formstructparser.formContainerPath}"
data-sly-test.widgetId="${datetime.id}-widget">
data-sly-test.widgetId="${datetime.id}-widget"
data-server-timezone-id="${datetime.serverTimezone}">
<div class="cmp-adaptiveform-datetime__label-container">
<div data-sly-call="${label.label @componentId=widgetId, labelValue=datetime.label.value, labelVisible=datetime.label.visible, bemBlock='cmp-adaptiveform-datetime'}" data-sly-unwrap></div>
<div data-sly-call="${questionMark.questionMark @componentId=datetime.id, longDescription=datetime.description, bemBlock='cmp-adaptiveform-datetime'}" data-sly-unwrap></div>
Expand Down
Loading