Skip to main content
Home
for DLMS smart meters

Main navigation

  • Home
  • Products
  • About us
  • Open Source
  • Community
  • Forum
  • Downloads
User account menu
  • Log in

Breadcrumb

  1. Home
  2. Error On Parsing Jalali Date (Persian)

Error On Parsing Jalali Date (Persian)

By ali_bahal, 21 May, 2023
Forums
Gurux.DLMS

hi, in recent update of gurux , It can't Parse Persian date Correctly . in previous versions ,it was working greatly.

it has error on C# version and GxDlmsDirector.

Packet examples :

TX: 7E A0 1C 00 02 76 71 03 54 A8 19 E6 E6 00 C0 01 C1 00 08 00 00 01 00 00 FF 02 00 60 1A 7E
13:49:30
RX: 7E A0 21 03 00 02 76 71 74 82 BD E6 E7 00 C4 01 C1 00 09 0C 05 7A 02 1F 07 0D 31 17 00 FF 2E 00 62 F2 7E

now it gives Error :
Year, Month and day parameters describe an un-representable DateTime.

at System.DateTime.DateToTicks(Int32 year, Int32 month, Int32 day)
at System.DateTime..ctor(Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, Int32 millisecond)
at Gurux.DLMS.Internal.GXCommon.GetDateTime(GXDLMSSettings settings, GXByteBuffer buff, GXDataInfo info)
at Gurux.DLMS.Internal.GXCommon.GetData(GXDLMSSettings settings, GXByteBuffer data, GXDataInfo info)
at Gurux.DLMS.GXDLMSClient.ChangeType(GXByteBuffer value, DataType type, Boolean useUtc)

ali_bahal

2 years ago

I think the problem raises…

I think the problem raises for month with 31 days . for example in above packets, in Persian 2nd month have 31 days and raises error. but other dates works greatly.

do you have any Idea, what we can do or is there anything that you can change in code ?

ali_bahal

2 years ago

I you can add Calendar to…

If you can add Calendar to GXDLMSSettings class and use below constructor of DateTime :

public DateTime(int year, int month, int day, int hour, int minute, int second, Calendar calendar)

that has calendar in in Conversion, I think all the problems will be solved

Profile picture for user Kurumi

Kurumi

2 years ago

Hi, No changes have been…

Hi,

No changes have been made to the date and time for a long time. Application is using the default calendar when the date-time is read from the meter.

The date is Yekshanbeh: 31. Ordibehesht 1402. Is that correct?
What is your time zone?

BR,
Mikko

ali_bahal

2 years ago

Yes, it.'s correct date…

Yes, it.'s correct date.
Timezome is : +3:30 Tehran

Is there any way to change calendar (set) for this?

Profile picture for user Kurumi

Kurumi

2 years ago

Hi, Calendar settings are…

Hi,

Calendar settings are defined in Windows and correct settings are given as a parameter. You can try to change settings from the Windows regional settings.

Are you using Windows 10 or 11? The same environment is installed and I believe that this can be fixed if it can be repeated.

BR,

Mikko

ali_bahal

2 years ago

No we are using windows…

No we are using windows server 2012, and I need to set gurux calendar from code. Setting current thread culture also don't slove the problem.
Any ideas will be appreciated.

ali_bahal

2 years ago

hi, Is it possible for you…

hi,
Is it possible for you to use DateTime overloads with calendar input in GxCommon ?
I mean use :
new DateTime(year, month, day, hours, minutes, seconds, milliseconds,Thread.CurrentThread.CurrentCulture.Calendar)

instead of :
new DateTime(year, month, day, hours, minutes, seconds, milliseconds)

or :
new DateTime(year, month, day, hours, minutes, seconds, milliseconds, Thread.CurrentThread.CurrentCulture.Calendar, DateTimeKind.Local)

instead of :
new DateTime(year, month, day, hours, minutes, seconds, milliseconds, DateTimeKind.Local)

Profile picture for user Kurumi

Kurumi

2 years ago

Hi, This doesn't help when…

Hi,

This doesn't help when you are reading data from the meter because it's not possible to set the calendar for the GXDLMSClient.

If you run dotnet --version what is the output?

BR,
Mikko

ali_bahal

2 years ago

there is no need to set…

there is no need to set calendar for the GXDLMSClient. we can use custume Culture class that has calendar as needed, and set for thread culture. but because you use this overloads of "new DateTime" that does not have Calendar in there inputs,then .net does not use thread calendar for them.

dotnet --version
7.0.203

ali_bahal

2 years ago

In changed the code as said…

In changed the code as I said above, and the code worked greatly

Image

ali_bahal

2 years ago

hi, I'm sorry, did you find…

hi,
I'm sorry, did you find any solution for this problem ? or have any idea ?

ali_bahal

1 year 12 months ago

Hi, I would like to know if…

Hi, I would like to know if you plan on implementing the changes I suggested in GxCommon. I would appreciate it if you could let me know.

I believe that changing the method as I proposed will not have any side effects on your code, since there is no calendar setting in GXDLMSClient. Therefore, your code uses the system's default calendar. Changing the code as follows:

new DateTime(year, month, day, hours, minutes, seconds, milliseconds, Thread.CurrentThread.CurrentCulture.Calendar)

instead of:

new DateTime(year, month, day, hours, minutes, seconds, milliseconds)

in GxCommon should be fine. If you have any comments on this, I would appreciate your feedback.

If this change is applied to the source version, I would be able to use your later updates more easily. Thank you in advance!

Profile picture for user Kurumi

Kurumi

1 year 12 months ago

Hi, Sorry to say, but it's…

Hi,

Sorry to say, but it's not s simple. This needs a lot of testing before it can be released.
Different time zones and localizations might cause really nasty problems.

It seems to work and it should be on the next release, but I can't say anything for sure before the tests are over.

BR,

Mikko

ali_bahal

1 year 11 months ago

Thank you. I would…

Thank you.
I would appreciate it if you could let me know the result.

Profile picture for user Kurumi

Kurumi

1 year 11 months ago

Hi, This can be added for…

Hi,

This can be added for the next release. It's released during the next week.

BR,
Mikko

ali_bahal

1 year 11 months ago

Hi, thank you in advance.

Hi, thank you in advance.

Profile picture for user Kurumi

Kurumi

1 year 11 months ago

Hi, This is improved for the…

Hi,

This is improved for the GXDLMSDirector version 9.0.2306.0201.

BR,
Mikko

ali_bahal

1 year 11 months ago

Hi, sorry for the delayed…

Hi, sorry for the delayed response. I was updating my code to use the new version; but I encountered another problem; as mentioned in Microsoft documents:

"The Year property returns the year of the current instance in the Gregorian calendar. It does not return the year using the default calendar of the current culture. To retrieve the year using a particular calendar, you can call that calendar's GetYear method."

However, because you used the following code in the "SetDateTime" method within the "GxCommon" class:

if ((dt.Skip & DateTimeSkips.Year) == 0)
{
buff.SetUInt16((ushort)tm.Year);
}

The time setting does not work correctly because always send year in Gregorian calendar. To correct this problem, we should use the following methods in "SetDateTime" :

Thread.CurrentThread.CurrentCulture.Calendar.GetYear()
Thread.CurrentThread.CurrentCulture.Calendar.GetMonth()
Thread.CurrentThread.CurrentCulture.Calendar.GetDayOfMonth()

I would appreciate it if you could also update this method accordingly.

Profile picture for user Kurumi

Kurumi

1 year 11 months ago

Hi, I'm afraid that this can…

Hi,

I'm afraid that this can't be the solution. The year, month, or day might be something else than the current thread values when reading historical data.

BR,
Mikko

ali_bahal

1 year 11 months ago

I don't think it will cause…

I don't think it will cause problems because:

1. We always set the thread culture to the appropriate culture, so historical data will also be used with this culture and calendar.
2. I believe this method is used in writing objects to Meter.
3. calling Thread.CurrentThread.CurrentCulture.Calendar.GetYear(dt) and using dt.Year always return the same value for Gregorian calendar.

and finally if you have any other solution for this, I'll appreciate if you apply on code.

Profile picture for user Kurumi

Kurumi

1 year 11 months ago

Hi, This is a problem…

Hi,

This is a problem because when you read historical data (profile generic) from the meter SetDateTime is used to set the date and time from the meter bytes.

I don't have a design for this at the moment. I need to think about how to solve this.

BR,
Mikko

ali_bahal

1 year 11 months ago

Hello! Thank you for your…

Hello! Thank you for your message. I appreciate you taking the time to reach out. I am eagerly looking forward to hearing back from you and am available to assist you with any further inquiries or concerns. Please don't hesitate to let me know how I can be of help.

ali_bahal

1 year 11 months ago

Hi, have you thought of any…

Hi, have you thought of any solutions?

Profile picture for user Kurumi

Kurumi

1 year 11 months ago

Hi, I don't have a solution…

Hi,

I don't have a solution for this yet. The main problem is that there is no way to tell if Gregorian or Persian calendar is used. It will take time to find a solution to this.

BR,
Mikko

ali_bahal

1 year 11 months ago

hi, I have question: why in …

hi, I have question:
why in "GetDateTime" method of "GxCommon" you don't use "Thread.CurrentThread.CurrentCulture.Calendar" if deviation is not defined ?

in some meters they don't use deviation and this cause incorrect date parsing. I mean in below code Else part:

//0x8000 == -32768
//deviation = -1 if skipped.
if (deviation != -1 && deviation != -32768 && year != 1 && (dt.Skip & DateTimeSkips.Year) == 0)
{
dt.Value = new DateTimeOffset(new DateTime(year, month, day, hours, minutes, seconds, milliseconds, Thread.CurrentThread.CurrentCulture.Calendar),
new TimeSpan(0, -deviation, 0));
}
else //Use current time if deviation is not defined.
{
dt.Skip |= DateTimeSkips.Deviation;
DateTime tmp = new DateTime(year, month, day, hours, minutes, seconds, milliseconds,DateTimeKind.Local);
dt.Value = new DateTimeOffset(tmp);
}

  • Log in or register to post comments
  • Create new account
  • Reset your password

Hire Us!

Latest Releases

  • Tue, 05/20/2025 - 08:40
    gurux.dlms.c 9.0.2505.2001
  • Mon, 05/19/2025 - 10:19
    gurux.net.java 1.0.30
  • Mon, 05/19/2025 - 10:00
    gurux.dlms.c 9.0.2505.1901
  • Thu, 05/15/2025 - 16:26
    gurux.dlms.c 9.0.2505.1501
  • Wed, 05/14/2025 - 08:30
    gurux.dlms.c 9.0.2505.1401

New forum topics

  • HDLC Adress resolver configuration
  • Reading all "Data" object type parameter together
  • com_read getting failed for billing profile and instantaneous profile
  • Simulator connection closed
  • Regarding the dlms android project
More
RSS feed
Privacy FAQ GXDN Issues Contact
Follow Gurux on Twitter Follow Gurux on Linkedin