Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
510 changes: 255 additions & 255 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"bluebird": "^3.4.7",
"change-case": "^3.0.2",
"md5-hex": "^2.0.0",
"moment": "^2.22.0",
"moment": "^2.22.2",
"pluralize": "^4.0.0",
"qs": "^6.5.2",
"readable-stream": "^3.0.6",
Expand Down
2 changes: 1 addition & 1 deletion src/Documents/Commands/GetRevisionsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class GetRevisionsCommand extends RavenCommand<IRavenArrayResult> {
}

if (this._before) {
uri += "&before=" + DateUtil.stringify(this._before);
uri += "&before=" + DateUtil.default.stringify(this._before);
}

if (!TypeUtil.isNullOrUndefined(this._start)) {
Expand Down
31 changes: 30 additions & 1 deletion src/Documents/Conventions/DocumentConventions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { ReadBalanceBehavior } from "../../Http/ReadBalanceBehavior";
import { throwError } from "../../Exceptions";
import { CONSTANTS } from "../../Constants";
import { TypeUtil } from "../../Utility/TypeUtil";
import { DateUtil } from "../../Utility/DateUtil";
import { DateUtil, DateUtilOpts } from "../../Utility/DateUtil";
import { CasingConvention, ObjectUtil, ObjectChangeCaseOptions } from "../../Utility/ObjectUtil";
import { JsonSerializer } from "../../Mapping/Json/Serializer";

Expand Down Expand Up @@ -71,6 +71,7 @@ export class DocumentConventions {
private _remoteEntityFieldNameConvention: CasingConvention;

private _objectMapper: TypesAwareObjectMapper;
private _dateUtil: DateUtil;

private _useCompression;

Expand Down Expand Up @@ -116,16 +117,24 @@ export class DocumentConventions {
});

this._useCompression = null;

this._dateUtilOpts = {};
this._dateUtil = new DateUtil(this._dateUtilOpts);
}

public get objectMapper(): TypesAwareObjectMapper {
return this._objectMapper;
}

public set objectMapper(value: TypesAwareObjectMapper) {
this._assertNotFrozen();
this._objectMapper = value;
}

public get dateUtil(): DateUtil {
return this._dateUtil;
}

public get readBalanceBehavior(): ReadBalanceBehavior {
return this._readBalanceBehavior;
}
Expand Down Expand Up @@ -196,6 +205,26 @@ export class DocumentConventions {
this._useCompression = value;
}

private _dateUtilOpts: DateUtilOpts;

public get storeDatesInUtc() {
return this._dateUtilOpts.useUtcDates;
}

public set storeDatesInUtc(value) {
this._assertNotFrozen();
this._dateUtilOpts.useUtcDates = value;
}

public get storeDatesWithTimezoneInfo() {
return this._dateUtilOpts.withTimezone;
}

public set storeDatesWithTimezoneInfo(value) {
this._assertNotFrozen();
this._dateUtilOpts.withTimezone = true;
}

/**
* If set to 'true' then it will throw an exception when any query is performed (in session)
* without explicit page size set.
Expand Down
2 changes: 1 addition & 1 deletion src/Documents/Identity/Commands/NextHiloCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class NextHiloCommand extends RavenCommand<HiLoResult> {

public createRequest(node: ServerNode): HttpRequestParameters {
const lastRangeAt: string = this._lastRangeAt
? DateUtil.stringify(this._lastRangeAt)
? DateUtil.default.stringify(this._lastRangeAt)
: "";

const queryString = qs.stringify({
Expand Down
2 changes: 1 addition & 1 deletion src/Documents/Session/AbstractDocumentQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ export abstract class AbstractDocumentQuery<T extends object, TSelf extends Abst

private _stringifyParameter(value: any) {
if (TypeUtil.isDate(value)) {
return DateUtil.stringify(value);
return DateUtil.default.stringify(value);
}

if (TypeUtil.isString(value)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Documents/Session/InMemoryDocumentSessionOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export abstract class InMemoryDocumentSessionOperations

const documentInfo = this._getDocumentInfo(instance);
const lastModified = documentInfo.metadata[CONSTANTS.Documents.Metadata.LAST_MODIFIED];
return DateUtil.parse(lastModified);
return DateUtil.default.parse(lastModified);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Mapping/ObjectMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export class TypesAwareObjectMapper implements ITypesAwareObjectMapper {
}

if (fieldTypeName === "date") {
fieldContext.setValue(DateUtil.parse(fieldVal));
fieldContext.setValue(this._conventions.dateUtil.parse(fieldVal));
return;
}

Expand Down Expand Up @@ -386,7 +386,7 @@ export class TypesAwareObjectMapper implements ITypesAwareObjectMapper {
[objPathPrefix]: "date"
});

return DateUtil.stringify(obj as Date);
return this._conventions.dateUtil.stringify(obj as Date);
}

if (TypeUtil.isSet(obj)) {
Expand Down
48 changes: 36 additions & 12 deletions src/Utility/DateUtil.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import * as moment from "moment";
import { throwError } from "../Exceptions";

export interface DateUtilOpts {
withTimezone?: boolean;
useUtcDates?: boolean;
}

export class DateUtil {

public static DEFAULT_DATE_FORMAT = "YYYY-MM-DDTHH:mm:ss.SSS0000";
public static DEFAULT_DATE_TZ_FORMAT = "YYYY-MM-DDTHH:mm:ss.SSS0000Z";

public static default: DateUtil = new DateUtil({});

public constructor(private _opts: DateUtilOpts) {}

public static timestamp(): number {
return moment().unix();
Expand All @@ -16,26 +27,39 @@ export class DateUtil {
return moment([1, 1, 1]).toDate();
}

public static parse(dateString: string): Date {
public parse(dateString: string): Date {
if (!dateString) {
return null;
}

const stripped = dateString.substring(0, dateString.length - 1);
const format = this.DEFAULT_DATE_FORMAT;

if (!dateString.endsWith("Z")) {
const parsed = moment(dateString, format);
let parsed;
if (this._opts.useUtcDates || this._opts.withTimezone) {
parsed = moment.parseZone(dateString, DateUtil.DEFAULT_DATE_TZ_FORMAT);
} else {
parsed = moment(dateString, DateUtil.DEFAULT_DATE_FORMAT);
}

if (parsed.isValid()) {
return parsed.toDate();
}
if (!parsed.isValid()) {
throwError("InvalidArgumentException", `Could not parse date string '${dateString}'.`);
}

return moment(stripped, format).toDate();
return parsed.toDate();
}

public static stringify(date: Date): string {
return moment(date).format(this.DEFAULT_DATE_FORMAT);
public stringify(date: Date): string {
const m = moment(date);
if (this._opts.useUtcDates) {
m.utc();
}

const format = this._opts.withTimezone
? DateUtil.DEFAULT_DATE_TZ_FORMAT
: DateUtil.DEFAULT_DATE_FORMAT;
const result = m.format(format);
if (this._opts.useUtcDates && !this._opts.withTimezone) {
return result + "Z";
}

return result;
}
}
12 changes: 9 additions & 3 deletions test/Issues/RDBC_233.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import { PatchRequest } from "../../src/Documents/Operations/PatchRequest";
describe("RDBC-233", function () {

let mapper: TypesAwareObjectMapper;
let conventions;

beforeEach(() => {
conventions = DocumentConventions.defaultConventions;
mapper = new TypesAwareObjectMapper({
documentConventions: DocumentConventions.defaultConventions
documentConventions: conventions
});
});

Expand All @@ -45,8 +47,12 @@ describe("RDBC-233", function () {
assert.ok(result);
assert.strictEqual(result.date, null);
assert.ok(result.anotherOne.start instanceof Date);
assert.strictEqual(result.anotherOne.start.toString(), DateUtil.parse(obj.anotherOne.start).toString());
assert.strictEqual(result.anotherOne.end.toString(), DateUtil.parse(obj.anotherOne.end).toString());
assert.strictEqual(
result.anotherOne.start.toString(),
DocumentConventions.defaultConventions.dateUtil.parse(obj.anotherOne.start).toString());
assert.strictEqual(
result.anotherOne.end.toString(),
DocumentConventions.defaultConventions.dateUtil.parse(obj.anotherOne.end).toString());
});

describe("entire flow - store, patch, load", async () => {
Expand Down
Loading