-
Notifications
You must be signed in to change notification settings - Fork 8
Open
Labels
Description
Description:
Incorporate various date formats (UTC, timestamp, localeString) and timezone handling when exporting data to CSV using native JavaScript and the Intl API.
Tasks:
- Implement formatDate function to handle various date formats and timezones using native JavaScript.
- Update sendExport method to apply date and timezone formatting.
- Allow dateFormat and timezone parameters in exportMeasures.
Example Code:
function formatDate(date: string | number, formatType: string, locale: string): string {
let dateObj: Date;
if (typeof date === 'string') {
dateObj = new Date(date);
} else {
dateObj = new Date(date);
}
switch (formatType) {
case 'UTC':
return dateObj.toISOString();
case 'timestamp':
return dateObj.getTime().toString();
case 'localeString':
return dateObj.toLocaleString(locale);
case 'custom':
return new Intl.DateTimeFormat(locale, {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
timeZoneName: 'short'
}).format(dateObj);
default:
throw new Error(`Unsupported date format type: ${formatType}`);
}
}
// formatter in stream processing
private async processHits(
hits: KDocument<MeasureContent>[],
columns: Column[],
stream: PassThrough,
target: "asset" | "device"
) {
const rows = hits.map(hit =>
stringify([columns.map(({ header: measureName, isMeasure, path, formatter }) => {
let value = _.get(hit, path, null);
if (isMeasure && target === "asset" && hit._source.asset?.measureName !== measureName) {
return null;
}
if (formatter) {
value = formatter(value);
}
return value;
})])
);
stream.write(rows.join(''));
}
Pros:
- Enhances flexibility in date and timezone formatting.
- Provides accurate date representations across different time zones using native JavaScript.
- Opens the road for other formatter to be specified
Cons:
- Increases complexity in date handling logic.
- Requires careful testing for different timezones and formats.