Kaynağa Gözat

Event::formatRange

Adam Shaw 7 yıl önce
ebeveyn
işleme
30e5d14ed9

+ 18 - 0
src/api/EventApi.ts

@@ -4,6 +4,7 @@ import { EventMutation } from '../structs/event-mutation'
 import { DateInput } from '../datelib/env'
 import { diffDates, computeAlignedDayRange } from '../util/misc'
 import { subtractDurations, DurationInput, createDuration } from '../datelib/duration'
+import { createFormatter, FormatterInput } from '../datelib/formatting'
 
 export default class EventApi {
 
@@ -163,6 +164,23 @@ export default class EventApi {
     this.mutate({ standardProps })
   }
 
+  formatRange(formatInput: FormatterInput) {
+    let dateEnv = this.calendar.dateEnv
+    let { instance } = this
+    let formatter = createFormatter(formatInput)
+
+    if (this.def.hasEnd) {
+      return dateEnv.formatRange(instance.range.start, instance.range.end, formatter, {
+        forcedStartTzo: instance.forcedStartTzo,
+        forcedEndTzo: instance.forcedEndTzo
+      })
+    } else {
+      return dateEnv.format(instance.range.start, formatter, {
+        forcedTzo: instance.forcedStartTzo
+      })
+    }
+  }
+
   private mutate(mutation: EventMutation) {
     let { instance } = this
 

+ 2 - 2
src/datelib/env.ts

@@ -334,7 +334,7 @@ export class DateEnv {
   }
 
   // TODO: choke on timeZoneName: long
-  format(marker: DateMarker, formatter: DateFormatter, dateOptions: any = {}) {
+  format(marker: DateMarker, formatter: DateFormatter, dateOptions: { forcedTzo?: number } = {}) {
     return formatter.format(
       {
         marker: marker,
@@ -346,7 +346,7 @@ export class DateEnv {
     )
   }
 
-  formatRange(start: DateMarker, end: DateMarker, formatter: DateFormatter, dateOptions: any = {}) {
+  formatRange(start: DateMarker, end: DateMarker, formatter: DateFormatter, dateOptions: { forcedStartTzo?: number, forcedEndTzo?: number, isEndExclusive?: boolean } = {}) {
 
     if (dateOptions.isEndExclusive) {
       end = addMs(end, -1)

+ 45 - 0
tests/automated/event-data/Event.formatRange.js

@@ -0,0 +1,45 @@
+describe('Event::formatRange', function() {
+  pushOptions({
+    timeZone: 'America/New_York', // for forced timezone offsets
+    locale: 'en'
+  })
+
+  const FORMAT_SETTINGS = {
+    month: 'long',
+    day: 'numeric',
+    year: 'numeric',
+    timeZoneName: 'short',
+    separator: ' to '
+  }
+
+  describe('when event has an end', function() {
+    pushOptions({
+      events: [
+        { start: '2018-09-04T12:00:00-05:00', end: '2018-09-05T12:00:00-05:00' }
+      ]
+    })
+
+    it('formats start and end', function() {
+      initCalendar()
+      let event = currentCalendar.getEvents()[0]
+      let str = event.formatRange(FORMAT_SETTINGS)
+      expect(str).toBe('September 4 to 5, 2018, GMT+5')
+    })
+  })
+
+  describe('when event has NO end', function() {
+    pushOptions({
+      events: [
+        { start: '2018-09-04T12:00:00-05:00' }
+      ]
+    })
+
+    it('formats start', function() {
+      initCalendar()
+      let event = currentCalendar.getEvents()[0]
+      let str = event.formatRange(FORMAT_SETTINGS)
+      expect(str).toBe('September 4, 2018, GMT+5')
+    })
+  })
+
+})