Skip to content

Timestamp

Christian Prinz edited this page Oct 17, 2022 · 1 revision

Class: Timestamp

a special version of a PlainDateTime which offers expire calculations

Hierarchy

Table of contents

Constructors

Properties

Accessors

Methods

Constructors

constructor

Protected new Timestamp(props)

Parameters

Name Type
props Required<PlainDateTimeProps>

Inherited from

PlainDateTime.constructor

Defined in

valueObjects/composed/PlainDateTime.ts:79

Properties

_value

Protected Readonly _value: void

the actual value of the ValueObject

Inherited from

PlainDateTime._value

Defined in

valueObjects/ValueObject.ts:4


date

Readonly date: number

Inherited from

PlainDateTime.date

Defined in

valueObjects/composed/PlainDateTime.ts:33


hours

Readonly hours: number

Inherited from

PlainDateTime.hours

Defined in

valueObjects/composed/PlainDateTime.ts:36


milliseconds

Readonly milliseconds: number

Inherited from

PlainDateTime.milliseconds

Defined in

valueObjects/composed/PlainDateTime.ts:39


minutes

Readonly minutes: number

Inherited from

PlainDateTime.minutes

Defined in

valueObjects/composed/PlainDateTime.ts:37


month

Readonly month: number

Inherited from

PlainDateTime.month

Defined in

valueObjects/composed/PlainDateTime.ts:32


seconds

Readonly seconds: number

Inherited from

PlainDateTime.seconds

Defined in

valueObjects/composed/PlainDateTime.ts:38


weekday

Readonly weekday: number

Inherited from

PlainDateTime.weekday

Defined in

valueObjects/composed/PlainDateTime.ts:34


year

Readonly year: number

Inherited from

PlainDateTime.year

Defined in

valueObjects/composed/PlainDateTime.ts:31

Accessors

dayMinutes

get dayMinutes(): number

returns the total amount of minutes that have passed on this day

Returns

number

Inherited from

PlainDateTime.dayMinutes

Defined in

valueObjects/composed/PlainDateTime.ts:42


value

get value(): T

the actual value of this ValueObject

Returns

T

Inherited from

PlainDateTime.value

Defined in

valueObjects/ValueObject.ts:13

Methods

compare

compare(other, density?): 0 | 1 | -1

compares this date with a given other PlainDateTime or 'now' to compare it with the current dateTime.

  • returns -1 if this dateTime is earlier than the other one
  • returns 0 if the dates are equal
  • returns 1 if this dateTime is later than the other one
Examples
'2020-05-25'.compare('2020-09-18') => -1
'2020-05-25'.compare('2020-01-01') => 1
'2020-05-25'.compare('2020-05-06', 'YM') => 0

Parameters

Name Type Default value Description
other "now" | PlainDateTime undefined the PlainDateTime to compare to / 'now' to use the current dateTime
density PlainDateTimeDensity 'YMDHMSs' the density the comparison has to have

Returns

0 | 1 | -1

-1 | 0 | 1 indicating which dateTime is more recent

Inherited from

PlainDateTime.compare

Defined in

valueObjects/composed/PlainDateTime.ts:348


createOffset

createOffset(offset, options?): Timestamp

creates a new PlainDateTime derived from the existing dateTime, with a given offset

Parameters

Name Type
offset Partial<PlainDateTimeProps>
options? CreationOptions

Returns

Timestamp

Overrides

PlainDateTime.createOffset

Defined in

valueObjects/composed/Timestamp.ts:59


createSet

createSet(newData, options?): Timestamp

creates a new PlainDateTime derived from the existing dateTime, where the given newData partial replaces the old data.

Parameters

Name Type
newData Partial<PlainDateTimeProps>
options? CreationOptions

Returns

Timestamp

Overrides

PlainDateTime.createSet

Defined in

valueObjects/composed/Timestamp.ts:55


createTimezoneOffset

createTimezoneOffset(timezone, hasSummertime?, options?): Timestamp

creates a new PlainDateTime derived from the existing dateTime, with a given timezone offset. The difference from createOffset() is that you only create the hour offset and automatically have the daylight saving time offset added.

Parameters

Name Type Default value
timezone number undefined
hasSummertime boolean true
options? CreationOptions undefined

Returns

Timestamp

Overrides

PlainDateTime.createTimezoneOffset

Defined in

valueObjects/composed/Timestamp.ts:63


distance

distance(toOther, density?): number

compares this date with a given other PlainDateTime or 'now' to compare it with the current dateTime and returns the distance between both.

  • distance = other - this
    • positive result = other dateTime was later
    • negative result = other dateTime was earlier
  • units depending on density:
    • "Y" / "YM" / "YMD" = days
    • "YMDH" = hours
    • "YMDHM" = minutes
    • "YMDHMS" = seconds
    • "YMDHMSs" = milliseconds (default)

Parameters

Name Type Default value Description
toOther "now" | PlainDateTime undefined -
density PlainDateTimeDensity 'YMDHMSs' the density the comparison has to have

Returns

number

the distance in days

Inherited from

PlainDateTime.distance

Defined in

valueObjects/composed/PlainDateTime.ts:398


equals

equals(obj, density?): boolean

compares if the given value is either an equal ValueObject, or an equal value which would create an equal ValueObject

Parameters

Name Type Default value Description
obj PlainDateTimeable | PlainDateTime undefined to compare of equality
density PlainDateTimeDensity 'YMDHMSs' -

Returns

boolean

Inherited from

PlainDateTime.equals

Defined in

valueObjects/composed/PlainDateTime.ts:310


expiresIn

expiresIn(density?): number

returns the (negative) distance from the timestamp to "now"

Parameters

Name Type
density? PlainDateTimeDensity

Returns

number

Defined in

valueObjects/composed/Timestamp.ts:22


getTime

getTime(): number

returns the time in ms since 1970 similar to Date.getTime()

Returns

number

Inherited from

PlainDateTime.getTime

Defined in

valueObjects/composed/PlainDateTime.ts:47


isExpired

isExpired(range?): boolean

Parameters

Name Type Description
range? Partial<PlainDateTime> to add to the existing Timestamp

Returns

boolean

if the timestamp (incl. range) is expired

Defined in

valueObjects/composed/Timestamp.ts:17


isSummerTime

isSummerTime(): boolean

Returns

boolean

Inherited from

PlainDateTime.isSummerTime

Defined in

valueObjects/composed/PlainDateTime.ts:72


isToday

isToday(): boolean

returns true if this date matches with the date of today (without time)

Returns

boolean

Inherited from

PlainDateTime.isToday

Defined in

valueObjects/composed/PlainDateTime.ts:62


toDate

toDate(density?, timeZoned?): Date

Parameters

Name Type Default value
density PlainDateTimeDensity 'YMDHMSs'
timeZoned boolean false

Returns

Date

Inherited from

PlainDateTime.toDate

Defined in

valueObjects/composed/PlainDateTime.ts:442


toJSON

toJSON(): Object

this function is invoked by JSON.stringify() and converts the inner "_propertyKey" to "propertyKey"

Returns

Object

the value

Name Type
date number
hours number
milliseconds number
minutes number
month number
seconds number
weekday number
year number

Inherited from

PlainDateTime.toJSON

Defined in

valueObjects/composed/PlainDateTime.ts:429


toString

toString(): string

Returns

string

Inherited from

PlainDateTime.toString

Defined in

valueObjects/composed/PlainDateTime.ts:419


create

Static create(value, options?): Timestamp

PlainDateTimeable

can be either PlainDateTimeProps:

{
 year: number;
 month?: number;
 date?: number;
 hours?: number;
 minutes?: number;
 seconds?: number;
 milliseconds?: number;
}

or YMDHMSs_Array:

[
  year: number,
  month?: number,
  date?: number,
  hours?: number,
  minutes?: number,
  seconds?: number,
  milliseconds?: number
]

or a string representation:

  • "DD.MM.YYYY"
  • "YYYY-MM-DD"
  • "DD.MM.YYYYTHH:MM:SS.sss+hh:mm"
  • JS dateString (RFC2822 || ISO8601)

Parameters

Name Type Description
value PlainDateTimeable to create the ValueObject of
options? CreationOptions constraints the value has to fulfill

Returns

Timestamp

the created ValueObject

Overrides

PlainDateTime.create

Defined in

valueObjects/composed/Timestamp.ts:40


fromList

Static fromList(values, options?): Timestamp[]

Parameters

Name Type Description
values undefined | PlainDateTimeable[] an array of primitives to map to an array of ValueObjects
options? CreationOptions & ListCreationOptions constraints the values / list has to fulfill

Returns

Timestamp[]

the array of ValueObjects

Overrides

PlainDateTime.fromList

Defined in

valueObjects/composed/Timestamp.ts:44


in

Static in(offset, options?): Timestamp

creates a timestamp with an offset to "now" e.g. 1h in the future:

const soon = Timestamp.in({hours: 1});
soon.expiresIn('YMDH');  // 1
soon.isExpired();        // false

Parameters

Name Type
offset Partial<PlainDateTimeProps>
options? CreationOptions

Returns

Timestamp

Defined in

valueObjects/composed/Timestamp.ts:34


listEquals

Static listEquals<ValueType>(a, b): boolean

compares 2 Lists of ValueObjects / values on equality

Type parameters

Name
ValueType

Parameters

Name Type Description
a ValueObject<ValueType>[] the list of ValueObjects to compare with
b ValueObject<ValueType>[] | ValueType[] a list of ValueObjects / values for comparison

Returns

boolean

true if the lists are equal

Inherited from

PlainDateTime.listEquals

Defined in

valueObjects/ValueObject.ts:29


now

Static now(options?): Timestamp

creates a PlainDateTime from the current dateTime. Similar to new Date()

Parameters

Name Type
options? CreationOptions & PlainDateTimeNowOptions

Returns

Timestamp

Overrides

PlainDateTime.now

Defined in

valueObjects/composed/Timestamp.ts:51


prefix

Static Protected prefix(options, addition?): string

constructs a prefix for possible error messages based on the ValueObjects name and an additional string:

  • example for NonEmptyString with name='Person':
"Person > NonEmptyString"

Parameters

Name Type Default value Description
options undefined | CreationOptions undefined the CreationOptions where the optional name: string is taken from
addition string '' An additional name which will be added as "name.addition"

Returns

string

the name of the ValueObject

Inherited from

PlainDateTime.prefix

Defined in

valueObjects/ValueObject.ts:77


validate

Static validate(value, options?): Required<PlainDateTimeProps>

Throws

various errors if not correct

Parameters

Name Type Description
value PlainDateTimeable to be validated as a valid dateTime with the corresponding constraints (options)
options? Omit<CreationOptions, "offset"> constraints the value has to fulfill

Returns

Required<PlainDateTimeProps>

the value if the validation was successful

Inherited from

PlainDateTime.validate

Defined in

valueObjects/composed/PlainDateTime.ts:234


validateInterval

Static Protected validateInterval(value, options): void

Parameters

Name Type
value string | number
options IntervalCreationOptions

Returns

void

Inherited from

PlainDateTime.validateInterval

Defined in

valueObjects/ValueObject.ts:86


validateList

Static Protected validateList<Primitive>(list, options?): list is Primitive[]

Type parameters

Name
Primitive

Parameters

Name Type
list undefined | Primitive[]
options? CreationOptions & ListCreationOptions

Returns

list is Primitive[]

Inherited from

PlainDateTime.validateList

Defined in

valueObjects/ValueObject.ts:101

DDD basics - v0.0.1

Clone this wiki locally