MongoDB convert timezone for Loganalyzer - mongodb

I have installed rsyslog-8.26.0, loganalyzer-4.1.5 and MongoDB 3.4.4 on CentOS7.
MongoDB stores all dates and times in UTC and I can see data on Loganalyzer also UTC.
any idea to change this for local timezone?
either change MongoDB default timezone or convert timezone on Loganalyzer when output display?

You can use mongo schema method like below or set default timezone at server side
tableschema.pre('save', function (next) {
//You can store timezone according to input and use that for conversion
var timezone=this.timezone;
this.date=date // converted date here using timezone
next();
}
Best is convert timezone on Loganalyzer when output display , it will support multiple timezone according to client machine

I found timezone line is commented on php.ini
;date.timezone =
php info says
Default timezone UTC
php.ini timezone changed like below
date.timezone = "Australia/Melbourne"
and restart HTTPD
now I can see all logs with my time zone

Related

Why does PG timestamp give incorrect result?

Why
Select timestamptz '2022-03-19T00:00Z' as test1
give result : 2022-03-19 03:00:00+03 ??
You're indicating Postgresql a timestamptz at UTC time.
There, it's returning a strictly equivalent time, except it is displayed in a different time zone (UTC-03).
Why does Postgresql not return the exact same thing you indicated ?
When parsing a timestamp with time zone, Postgresql internally stores it in UTC time, using the provided TZ info (here 'Z, understood as Z00, which means UTC time) to determine the offset to apply to convert it (here, you're giving a perfectly fine UTC timestamp).
When displaying data however, Postgresql relies on the internal TimeZone info defined in the postgresql.conf file to choose the time zone to use.
In your case, the local time zone info might be set to the place you live (or where your server lies), which is in UTC-03. Just type show time zone to be sure of that.
If you want to retrieve the data at timezone UTC, you have two or three options:
the simplest one is to precise the timezone info you want in the select statement:
select timestamptz '2022-03-19T00:00Z' at time zone 'UTC' as test1;
which gives you a timestamp without timezone though;
or you can set the time zone info for your local session:
set timezone='UTC';
or if you want to set the time zone info to UTC permanently, the timezone field of the postgresql.conf file has to be changed.
This file is for instance in /opt/homebrew/var/postgres if you're running a local instance of postgresql on Mac (like me), installed from brew. On Linux, I believe it might be in /etc/postgresql.
You need to restart your postgresql instance after the change:
brew services restart postgresql on Mac for example.

Any way to save Date type as local time in MongoDB?

It seems like all the date type in Mongo DB are saved as UTC time. Is there anyway to configure from the MongoDB server side that I can change the default date type to display as local time? For example, to display/save as CTC time on the DB? Thanks,
MongoDB stores datetime as the number of milliseconds since epoch. No timezone information is stored with the data.
The value returned by the database would look something like 1614847506286.
How that is displayed is completely up to the application.

Grafana adds the timezone difference to dates

I recently started using Grafana to show data I have in my PostgreSQL Database.
Now I've reached a point where when selecting data and using a certain timestamp field as the "time" field in Grafana, the data that's shown is the dates + timezone difference.
For example:
My data has the timestamp "2020-08-24 12:05:30" and my timezone is UTC+3, but Grafana shows it as "2020-08-24 15:05:30".
Is there any way to simply display the data as it exists in my DB without adding that timezone difference?
Go to the dashboard settings and make sure the timezone is set to UTC or whatever timezone is used by the data.
Changing the timezone wasn't my solution, as it would also shift "now".
I simply added an interval to my query:
TO_TIMESTAMP(DateTime, 'YY-MM-DD HH24-MI-SS') - interval '2 hour' as time
to shift it 2 hours
The way I think it is intended to be used is:
Storing of dateTime values in the database should be in UTC only. (E.g. send a string representation of datetime.datetime.utcnow() to PostreSQL).
If you then select your timezone in Grafana's settings, things should work as expected.

How to save date properly?

I'm trying to save date (using C# official driver):
val = DateTime.Parse(value).Date; //Here date is {11/11/2011 12:00:00 AM}
var update = Update.Set("Date", val);
...
When I select Date from the database, the value is {11/10/2011 10:00:00 PM}
How to save only the date I want?
c# driver by default (without extra settings) saving local dates as utc date into database (date - time zone offset) but reading back without any action (so, utc date).
Because of this when you loading datetime from database you receive diff in 2 hours (your timezone offset). There are two approaches how to say to mongodb c# driver convert utc dates to local timezone dates during deserialization:
1.through the attributes for particular date field:
[BsonDateTimeOptions(Kind = DateTimeKind.Local)]
public DateTime SomeDateProperty {get;set;}
2.through global settings for all datetime fields (default is UtcInstance):
DateTimeSerializationOptions.Defaults = DateTimeSerializationOptions.LocalInstance;
Once you will do #1 or #2 you will see local date.
Update:
#2 is obsolete in latest driver version so use code below instead:
BsonSerializer.RegisterSerializer(typeof(DateTime),
new DateTimeSerializer(DateTimeSerializationOptions.LocalInstance));
Update:
#2 has changed again:
BsonSerializer.RegisterSerializer(typeof(DateTime), DateTimeSerializer.LocalInstance);
You're running into a timezone issue. Your date object is probably in a timezone other than UTC (2 hours ahead by the looks of it) or your default timezone is set to something other than UTC. The driver will convert the date to the appropriate timezone before storing it in the database.
Normally you wouldn't notice this since the reverse (retrieving the UTC date from the database) should convert it back to the original timezone. There might be an issue with the driver you're using or C# might require a bit more code to get it right.
Storing dates as strings is usually not a good idea since it disables range queries on dates.
Mongo stores everything in UTC, in case your date time is UTC this will help
val = DateTime.SpecifyKind(val , DateTimeKind.Utc);
var update = Update.Set("Date", val);
2.2.4.26 has changed again:
BsonSerializer.RegisterSerializer(typeof(DateTime), DateTimeSerializer.LocalInstance);
In my case [BsonDateTimeOptions(Kind = DateTimeKind.Local)] doesn't worked.
What i did is below
DateTime _someDateProperty ;
public DateTime SomeDateProperty
{
get { return _someDateProperty ; }
set
{
_someDateProperty = new DateTime(value.Ticks, DateTimeKind.Local);
}
}
Mongodb Date value stored in "UTC DateTime" format which comprises of both date & time. so you just cannot store date alone in the field. Instead you can get the date part alone in your application code after retrieving from mongo.
like in c#
datevalue.ToString("MM/dd/yyyy");
or
datevalue.ToShortDateString()

Is the GWT TimeZone offset backwards?

I'm using com.google.gwt.i18n.client.timezone to try and display a date (as at the server), but GWT automatically adds the current timezone to the date when formatting it, meaning The wrong date is shown in different timezones.
To combat this, I'm sending the server's timezone offset to the client and using that when formatting.
I live in Australia and the current timezone is +11 GMT/UTC, but the default timezone being displayed when I format the date is -11 GMT.
The offset from the server is +11 hours (as it should be), but when I try and format the date with this offset, I get the wrong date, and so I need to use the negative offset instead.
Why is the default timezone wrong?
When you are getting a date (particularly if you're parsing a date) make sure you specify the timezone. GWT's DateTimeFormat.parse only supports "RFC format" timezones, something like -0800 for Pacific time. If your server is sending dates in strings to the client, make sure it includes the timezone in this format.
Then when you convert the date to a string to present it to the user, make sure you use the overload of DateTimeFormat.format that specifies a TimeZone and pass the timezone that you want the date to be presented in (the timezone of the server, in your case.)
By default dates are presented in the timezone that the user's system is set to. Setting the default timezone in GWT (so you can ignore timezones and do everything in the server's timezone) is an open issue (3489) at the time I write this.