Persisting time with timezone offset in mongodb with nestjs - mongodb

I have a nestjs application which has a very date heavy schema.
According to my understanding date is stored in mongo without timezone. My API accepts time in ISO format with timezone offset.
Inserting following object {"date": "2009-06-30T18:30:00+11:00"}
will result in following document in the mongo database {date: ISODate('2009-06-30T07:30:00.000Z'), _id: "..."}
So the timezone offset is lost.
Is there an elegant way to keep the timezone offset and deliver the ISO string with the same offset on an GET request? Maybe make use of the class-transformer and store the offset in a separate property? If yes, how?
Here are the involved classes. (There is a dedicated ItemDto for GET requests which is not shown here.)
Dto:
export class CreateItemDto {
// Some other props are here
/**
* Date of this Information.
* #example "1900-01-01T05:00:00.000+05:00"
*/
#IsNotEmpty()
#IsDate()
#Type(() => Date)
date: Date;
}
Schema:
export class ItemSchema {
// Some other props are here
#Prop({ type: Date, required: true })
date!: Date;
}

In many cases the client application will convert and display the time in current local time zone, no matter in which time zone the timestamp was inserted.
If this is not sufficient for you then you have to store the time zone information in a separate field along with the actual timestamp.

Related

How to add a date to a prisma typescript form?

I am trying to figure out how to post a date to a prisma database.
I have a prisma.schema which has a createdAt field as follows:
createdAt DateTime #default(now()) #db.Timestamptz(6)
I made a model with a date field in it as follows:
#Field()
createdAt: Date;
And a create.input.ts with a similar field:
#IsNotEmpty()
#Field()
createdAt: Date;
then, in the form, I'm trying to add the createdAt date as the date the form is submitted, as follows:
return form.handler(() => createIssueGroup({ variables: { data: { ...data, createdAt: Date.now() } } })),
However, I get an error that says type number is not assignable to type string. I don't think I'm using a string in any of the date fields.
How can I post a date to prisma?
From your schema definition,
createdAt DateTime #default(now()) #db.Timestamptz(6)
The date will be automatically generated due to the now() method you specified in #default(). You don't need to pass a date to the database as that will be handled for you by Prisma. See the docs for more information on using now() and defining a default value.

Save Date.now() to timestamp column but get date/time field value out of range

My project (NestJS with TypeScript) is using TypeOrm on PostgreSQL database.
My table has a column (in migration file):
new TableColumn({
name: 'managed_at',
type: 'timestamp',
isNullable: true,
}),
Associated field in entity class:
#Column({ type: 'timestamp', nullable: true })
managedAt: Date | null;
I would like the column managed_at holds value of date and time.
If I save a data to the table with:
import { Repository } from 'typeorm';
...
// repo is the Repository of typeorm
repo.update(
{ managedAt: Date.now() }
);
I get error:
QueryFailedError: date/time field value out of range: "1651495656811"
How to solve this problem that using Date.now() and hold value of data and time?
import { Repository } from 'typeorm';
...
// repo is the Repository of typeorm
repo.update(
{ managedAt: new Date() }
);
Change Date.now() -> new Date().
You need to save Date type data to column in timestamp type.
Btw, you can add this in your entity class.
It will update column before update data.
#BeforeUpdate()
updateManagedAt(): void {
this.managedAt = new Date();
}
The static Date.now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC, as per documentation here Date.now().
Whereas valid input for the time stamp types consists of the concatenation of a date and a time, followed by an optional time zone (if you are using timestamptz type instead of timestamp type), followed by an optional AD or BC. (Alternatively, AD/BC can appear before the time zone, but this is not the preferred ordering.
You can read more about Date/Time types in pgSQL here.
In your case, you can do it like this
repo.update({ managedAt: (new Date()).toISOString() });
You'd use toISOString for sharing a date with another machine or process.

How I insert a time-date-stamp in MongoDB with a Golang Sruct?

I have the following struct:
type TypeIncidence struct {
Number int bson:"number" json:"number"
Description string bson:"description" json:"description"
Date_time_stamp string bson:"dateTimeStamp" json:"date_time_stamp"
}
and I want insert a document in a collection:
type TypeIncidence struct {
Number int `bson:"number" json:"number"`
Description string `bson:"description" json:"description"`
Date_time_stamp **string?**
}
var incidence TypeIncidence
incidence.Number = 1
Description =" Text"
Date_time_stamp = **string?**
What data type would I have to use in a Golang structure to store date_time_struct a string?
If I want to store with the following format 'YYYY-MM-DD hh:mm:ss', what module and/or function should I use in golang? (in local machine or server converting zone time)
Thanks in advance
You can use time.Time:
CreatedAt time.Time `json:"created_at" bson:"created_at"`
However, I would recommend that you store Epoch Unix timestamp (the number of seconds since Jan 1st 1970) because it is universal:
CreatedAt int64 `json:"created_at" bson:"created_at"`
I have tried in the past to store time.Time in MongoDB through Golang but then I had trouble when I parsed the same information into a datetime object in Python. If you would like to be compatible across languages and technologies, storing the Epoch Unix timestamp would be a great option.

Save a birthDate via qraphql in postgres

I would like to save a birthdate from an (react) input (type = 'date'), send it via GraphQL to node backend and persist it in postgres in a date format.
Input in HTML: 09.07.2000
In GraphQL resolver: 2000-07-09T00:00:00.000Z
Date format in Postgres (original output in console): 09.07.2000
Well, that's what i expected. But now, if i request the same field:
Date format in Postgres (original output in console): 09.07.2000
Graphql response: 08.07.2000
HTML Input: 08.07.2000
If I change the scalar in graphQl schema to String, the following string returns: Fri Jul 09 2000 00:00:00 GMT+0200 (CEST)
Code
schema
scalar Date
type Child {
...
birthDate: Date
...
}
resolvers
const { GraphQLDate } = require('graphql-iso-date')
...
Date: GraphQLDate,
Problem
It looks like there is a problem in converting the date format from different timezones. If there is no timezone the scalar resolver guess a timezone. But this is a birthdate, it hast to be the same date on every timezone. How can I fix this? Do I have to use a string instead of date in prostgres?
Thanks for any support 🙏 I'm really lost
UPDATE
It looks like knex.js is the problem.
A normal SQL query responds the expected date. But a query with knex.js response a datetime.

Change date storage format in MongoDB

In an input json file i receive dates in this format:
{ "dt_received" : "2016-01-22T12:35:52.123+05" }
When loaded into MongoDB, those dates are stored this way:
dt_received: "2016-01-22T07:35:52.123Z"
The issue is that i need the timezone to calculate my indicator.
In constraint, i can't create new columns such as "dt_received_timezone".
So i'm looking for changing the date storage format into MongoDB in order to make the timezone appear (or at least not disapear)
Is it a way to to this? Or any solution ?
If you receive data from various time zones and want to keep the time zone offset, you will have to save it into the database like this:
var now = new Date();
db.data.save( { date: now, offset: now.getTimezoneOffset() } );
You can then reconstruct the original time like this
var record = db.data.findOne();
var localNow = new Date( record.date.getTime() - ( record.offset * 60000 ) );
See the documentation for further details