File

libs/d3/src/lib/helper/d3-time-format-locale.service.ts

Description

This service formats the time labels for the time axis in d3. Internationalisation will be managed by moment.

Index

Properties
Methods

Constructor

constructor(timezoneSrvc: TimezoneService)
Parameters :
Name Type Optional
timezoneSrvc TimezoneService No

Methods

Public formatTime
formatTime(time: number)
Parameters :
Name Type Optional
time number No
Returns : string
Private roundDay
roundDay(time: number)
Parameters :
Name Type Optional
time number No
Returns : any
Private roundHour
roundHour(time: number)
Parameters :
Name Type Optional
time number No
Returns : any
Private roundMinute
roundMinute(time: number)
Parameters :
Name Type Optional
time number No
Returns : any
Private roundMonth
roundMonth(time: number)
Parameters :
Name Type Optional
time number No
Returns : any
Private roundSecond
roundSecond(time: number)
Parameters :
Name Type Optional
time number No
Returns : any
Private roundWeek
roundWeek(time: number)
Parameters :
Name Type Optional
time number No
Returns : any
Private roundYear
roundYear(time: number)
Parameters :
Name Type Optional
time number No
Returns : any

Properties

Protected formatDay
Type : string
Default value : 'MMM D'
Protected formatHour
Type : string
Default value : 'HH:mm'
Protected formatMillisecond
Type : string
Default value : '.SSS'
Protected formatMinute
Type : string
Default value : 'HH:mm'
Protected formatMonth
Type : string
Default value : 'MMMM'
Protected formatSecond
Type : string
Default value : ':ss'
Protected formatWeek
Type : string
Default value : 'MMM D'
Protected formatYear
Type : string
Default value : 'YYYY'
import { Injectable } from '@angular/core';
import { TimezoneService } from '@helgoland/core';
import moment from 'moment';

/**
 * This service formats the time labels for the time axis in d3. Internationalisation will be managed by moment.
 */
@Injectable({
  providedIn: 'root'
})
export class D3TimeFormatLocaleService {

  protected formatMillisecond = '.SSS';
  protected formatSecond = ':ss';
  protected formatMinute = 'HH:mm';
  protected formatHour = 'HH:mm';
  protected formatDay = 'MMM D';
  protected formatWeek = 'MMM D';
  protected formatMonth = 'MMMM';
  protected formatYear = 'YYYY';

  constructor(
    protected timezoneSrvc: TimezoneService
  ) { }

  public formatTime(time: number): string {
    const curr = this.timezoneSrvc.createTzDate(time);

    const format = this.roundSecond(time) < curr ? this.formatMillisecond
      : this.roundMinute(time) < curr ? this.formatSecond
        : this.roundHour(time) < curr ? this.formatMinute
          : this.roundDay(time) < curr ? this.formatHour
            : this.roundMonth(time) < curr ? (this.roundWeek(time) < curr ? this.formatDay : this.formatWeek)
              : this.roundYear(time) < curr ? this.formatMonth
                : this.formatYear;

    return this.timezoneSrvc.formatTzDate(moment(time), format);
  }

  private roundMinute(time: number) {
    return this.timezoneSrvc.createTzDate(time).startOf('minute');
  }

  private roundSecond(time: number) {
    return this.timezoneSrvc.createTzDate(time).startOf('second');
  }

  private roundYear(time: number) {
    return this.timezoneSrvc.createTzDate(time).startOf('year');
  }

  private roundMonth(time: number) {
    return this.timezoneSrvc.createTzDate(time).startOf('month');
  }

  private roundWeek(time: number) {
    return this.timezoneSrvc.createTzDate(time).startOf('week');
  }

  private roundHour(time: number) {
    return this.timezoneSrvc.createTzDate(time).startOf('hour');
  }

  private roundDay(time: number) {
    return this.timezoneSrvc.createTzDate(time).startOf('day');
  }

}

result-matching ""

    No results matching ""