Skip to content
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

[CSV Exporter] Add Date Format Options with Timezone Handling #352

Open
Juiced66 opened this issue Jul 5, 2024 · 0 comments
Open

[CSV Exporter] Add Date Format Options with Timezone Handling #352

Juiced66 opened this issue Jul 5, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request feature-request

Comments

@Juiced66
Copy link
Contributor

Juiced66 commented Jul 5, 2024

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.
@Juiced66 Juiced66 added enhancement New feature or request feature-request labels Jul 5, 2024
@Juiced66 Juiced66 self-assigned this Jul 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature-request
Projects
None yet
Development

No branches or pull requests

1 participant