Skip to content

Commit

Permalink
feat: add options
Browse files Browse the repository at this point in the history
  • Loading branch information
César Alberca committed Dec 2, 2021
1 parent 7a7f1f7 commit cddebdb
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions packages/utils/src/datetime/datetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { Duration } from './duration'
import { StringUnitLength } from './string-unit-length'
import { InfoOptions } from './info-options'

export interface DatetimeOptions {
isLocal: boolean
}

export class Datetime {
static now(): Datetime {
return new Datetime(LuxonDatetime.local().setZone('utc'))
Expand Down Expand Up @@ -63,8 +67,13 @@ export class Datetime {
return new Datetime(LuxonDatetime.fromISO(isoString))
}

static fromJsDate(date: Date) {
return new Datetime(LuxonDatetime.fromJSDate(date))
/**
* Create from a JavaScript {Date} object
* @param date
* @param options
*/
static fromJsDate(date: Date, options?: DatetimeOptions) {
return new Datetime(LuxonDatetime.fromJSDate(date), options)
}

/**
Expand All @@ -85,8 +94,8 @@ export class Datetime {
return Info.weekdays(length, options)
}

constructor(private readonly _value: LuxonDatetime, isLocal: boolean = false) {
if (isLocal) {
constructor(private readonly _value: LuxonDatetime, options: DatetimeOptions = { isLocal: false }) {
if (options.isLocal) {
this._value = _value
} else {
this._value = this._value.setZone('utc')
Expand Down Expand Up @@ -168,15 +177,15 @@ export class Datetime {
}

toLocal(): Datetime {
return new Datetime(this._value.toLocal(), true)
return new Datetime(this._value.toLocal(), { isLocal: true })
}

toMillis(): number {
return this._value.toMillis()
}

asLocal() {
return new Datetime(this._value.setZone('local', { keepLocalTime: true }), true)
return new Datetime(this._value.setZone('local', { keepLocalTime: true }), { isLocal: true })
}

isValid() {
Expand Down

0 comments on commit cddebdb

Please sign in to comment.