datewithzone.ts 819 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { dateInTimeZone, timeToUntilString } from './dateutil'
  2. export class DateWithZone {
  3. public date: Date
  4. public tzid?: string | null
  5. constructor(date: Date, tzid?: string | null) {
  6. if (isNaN(date.getTime())) {
  7. throw new RangeError('Invalid date passed to DateWithZone')
  8. }
  9. this.date = date
  10. this.tzid = tzid
  11. }
  12. private get isUTC() {
  13. return !this.tzid || this.tzid.toUpperCase() === 'UTC'
  14. }
  15. public toString() {
  16. const datestr = timeToUntilString(this.date.getTime(), this.isUTC)
  17. if (!this.isUTC) {
  18. return `;TZID=${this.tzid}:${datestr}`
  19. }
  20. return `:${datestr}`
  21. }
  22. public getTime() {
  23. return this.date.getTime()
  24. }
  25. public rezonedDate() {
  26. if (this.isUTC) {
  27. return this.date
  28. }
  29. return dateInTimeZone(this.date, this.tzid)
  30. }
  31. }