Skip to content

Instantly share code, notes, and snippets.

@hwclass
Last active January 22, 2019 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hwclass/cb0edc0a5897455055567ec1e94337a1 to your computer and use it in GitHub Desktop.
Save hwclass/cb0edc0a5897455055567ec1e94337a1 to your computer and use it in GitHub Desktop.
import moment from 'moment'
class Timestamp {
private timestamp: string;
private readonly format = 'YYY-MM-DD HH:mm:ss'
private constructor(timestamp: string) {
this.timestamp = timestamp
}
private isValid(timestamp: string): Boolean {
return moment(timestamp, this.format, true).isValid()
}
public toString(): string {
return moment(this.timestamp).format(this.format)
}
public fromString(timestamp: string): Timestamp {
if (timestamp === '' && !this.isValid(timestamp)) {
throw Error('Invalid timestamp')
}
return new Timestamp(moment(timestamp).format(this.format))
}
public equals(that: Timestamp): Boolean {
return that.timestamp === this.timestamp
}
public isBefore(that: Timestamp): Boolean {
return moment(this.timestamp).isBefore(that.timestamp)
}
public isAfter(that: Timestamp): Boolean {
return moment(this.timestamp).isAfter(that.timestamp)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment