Merge pull request #28986 from rpasaporte/master
Fix date parsing and formatting in convertTimeToLocalTimezone function
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-catalog-unprocessed-entities': patch
|
||||
---
|
||||
|
||||
Fixed the `convertTimeToLocalTimezone` function in the FailedEntities component to correctly parse ISO 8601 date strings and set the timezone to the current local timezone.
|
||||
@@ -93,18 +93,15 @@ const RenderErrorContext = ({
|
||||
* Converts input datetime which lacks timezone info into user's local time so that they can
|
||||
* easily understand the times.
|
||||
*/
|
||||
const convertTimeToLocalTimezone = (strDateTime: string | Date) => {
|
||||
const dateTime = DateTime.fromFormat(
|
||||
strDateTime.toLocaleString(),
|
||||
'yyyy-MM-dd hh:mm:ss',
|
||||
{
|
||||
zone: 'UTC',
|
||||
},
|
||||
);
|
||||
export const convertTimeToLocalTimezone = (dateTime: string | Date) => {
|
||||
const isoDateTime =
|
||||
typeof dateTime === 'string' ? dateTime : dateTime.toISOString();
|
||||
|
||||
const dateTimeLocalTz = dateTime.setZone(DateTime.local().zoneName);
|
||||
const strDateTime = DateTime.fromISO(isoDateTime, {
|
||||
zone: DateTime.local().zoneName,
|
||||
});
|
||||
|
||||
return dateTimeLocalTz.toFormat('yyyy-MM-dd hh:mm:ss ZZZZ');
|
||||
return strDateTime.toFormat('yyyy-MM-dd hh:mm:ss ZZZZ');
|
||||
};
|
||||
|
||||
export const FailedEntities = () => {
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import { convertTimeToLocalTimezone } from './components/FailedEntities';
|
||||
import { catalogUnprocessedEntitiesPlugin } from './plugin';
|
||||
|
||||
describe('catalog-unprocessed-entities', () => {
|
||||
@@ -20,3 +21,29 @@ describe('catalog-unprocessed-entities', () => {
|
||||
expect(catalogUnprocessedEntitiesPlugin).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('components/FailedEntities/convertTimeToLocalTimezone', () => {
|
||||
it('should correctly a UTC ISO string to local time', () => {
|
||||
const utcTime = '2024-09-03T08:15:08.088Z';
|
||||
const localTime = convertTimeToLocalTimezone(utcTime);
|
||||
expect(localTime).toBe('2024-09-03 08:15:08 UTC');
|
||||
});
|
||||
|
||||
it('should correctly convert a UTC Date object to local time', () => {
|
||||
const utcTime = new Date('2024-09-03T08:15:08.088Z');
|
||||
const localTime = convertTimeToLocalTimezone(utcTime);
|
||||
expect(localTime).toBe('2024-09-03 08:15:08 UTC');
|
||||
});
|
||||
|
||||
it('should return "Invalid Date" for an invalid date string', () => {
|
||||
const invalidTime = 'invalid-date-string';
|
||||
const localTime = convertTimeToLocalTimezone(invalidTime);
|
||||
expect(localTime).toBe('Invalid DateTime');
|
||||
});
|
||||
|
||||
it('should handle empty string input', () => {
|
||||
const emptyString = '';
|
||||
const localTime = convertTimeToLocalTimezone(emptyString);
|
||||
expect(localTime).toBe('Invalid DateTime');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user