Inconsistent datetime across server - c#

I have a MVC function like this below, which will return the serialized JSON object back to the client:
public ActionResult ReportSalesHeaderListRest(ReportSalesRestViewModel viewModel, int? shop)
{
List<ReportGeneralDetailDto> rptSalesHeaderList = (
from txSalesHeader in completedTxSalesHeaderList.OrderBy(x => x.TxCode)
select new ReportGeneralDetailDto
{
ItemDate = txSalesHeader.CashierDatetime,
ItemCode = txSalesHeader.TxCode,
ItemValue = txSalesHeader.AmountTotal
})
.ToList();
viewModel.RptGeneralDetailList = rptSalesHeaderList;
return Json(viewModel, JsonRequestBehavior.AllowGet);
}
The problem is, JSON() function added some hours offset to my "ItemDate" attribute, when I run this code on an Asia server, 8 hours will be added to it, so "05/04/2014 08:15:19 (dd/mm/yyyy)" will become "Date(1396685719823)", which is "Sat Apr 05 2014 16:15:19 GMT+0800" instead of the time I want.
Does anyone come across this problem and is there any way ask JSON() function not to add the offset to my datetime values? Thanks!

I think that this is an issue with servers being in different time zones. You can use the DateTime.ToLocalTime() and DateTime.ToUniversalTime() to make the necessary conversion.

Related

eBay Trading API requires parameters for GetOrders method required, but to process, some need to be valueless. Null, not accepted

I'm using the eBay Trading API, Visual Studio, C#, with the eBay NuGet Package installed, which creates eBay objects and methods for me.
The definition of the GetOrders(...) method is:~
public OrderTypeCollection GetOrders(StringCollection OrderIDList, DateTime CreateTimeFrom, DateTime CreateTimeTo, TradingRoleCodeType OrderRole, OrderStatusCodeType OrderStatus, ListingTypeCodeType ListingType, PaginationType Pagination, DateTime ModTimeFrom, DateTime ModTimeTo, int NumberOfDays, bool IncludeFinalValueFee, SortOrderCodeType SortingOrder);
Almost all of these parameters are required, null is not accepted. Some will accept blank variables. So I have this code to call the method:
var roletype = TradingRoleCodeType.Seller;
var orderstatuscode = OrderStatusCodeType.All;
var pagination = new PaginationType();
pagination.EntriesPerPage = itemsPerPage;
pagination.PageNumber = pageNumber;
int noOfDays = new int();
DateTime blankDate = new DateTime(); // Not blank, but 01/01/0001 12:00:00 AM
orders = apicall.GetOrders(null, fromDate, toDate, roletype, orderstatuscode, new ListingTypeCodeType(), pagination, blankDate, blankDate, noOfDays, false, SortOrderCodeType.Ascending);
This code will run, but returns the error "Do not provide CreateTimeFrom/To as well as ModTimeFrom/To, only provide one or the other"
If I write my own calls to the API, I can just leave out the unrequired parameters, but by using the methods built by the eBay Nuget package, I'm forced to submit parameters. To me, it appears impossible to use this method, because it requires two sets of dates to submit, but if submitted with two sets of dates, it is rejected by the server.
I'm fairly new to C#, so there's possibly something simple I'm missing. There are 3 overloads for the method, but none of the accept the required set of parameters.
I needed to pick one of the simpler GetOrders overflows, with less parameters. Then feed in optional parameters into the parent object. For example:
var orderstatuscode = OrderStatusCodeType.All;
var pagination = new PaginationType();
pagination.EntriesPerPage = itemsPerPage;
pagination.PageNumber = pageNumber;
apicall.Pagination = pagination;
orders.AddRange(apicall.GetOrders(fromDate, toDate, roletype , orderstatuscode));
-- Edit --
After doing more work with the code, I've found an even more flexible way of getting this to support a custom selection of parameters.
while (hasOrders)
{
Console.WriteLine("Getting page {0}.... ", pagination.PageNumber);
apicall.Pagination = pagination;
apicall.ModTimeFrom = fromDate;
apicall.ModTimeTo = toDate;
apicall.OrderRole = TradingRoleCodeType.Seller;
apicall.OrderStatus = OrderStatusCodeType.All; ;
apicall.Execute();
orders.AddRange(apicall.ApiResponse.OrderArray);
//orders.AddRange(apicall.GetOrders(fromDate,toDate, roletype, orderstatuscode)); // No overflow will support modified date.
hasOrders = apicall.HasMoreOrders;
pagination.PageNumber++;
}

DateTimeOffset: TZ Offset is reset to +00:00 when fetching data from LiteDb collection

When inserting, the offset is OK, however when retrieving the document, it's resetting to +00:00
Property:
public DateTimeOffset CreatedOn { get; set; }
Insert:
user.CreatedOn = DateTimeOffset.Now; // 01/20/2021 6:05:21 PM +05:30
col.Insert(user);
col.EnsureIndex(x => x.Username);
Find:
using (var db = _liteDbConnection.Create() as LiteDatabase)
{
var col = db.GetCollection<AuthUser>(USERS_COLLECTION);
return col.FindOne(x => x.UserId == userId);
}
user.CreatedOn becomes
01/20/2021 6:05:21 PM +00:00
Am I doing something wrong?
From the documentation
Following the BSON specification, DateTime values are stored only up to the miliseconds. All DateTime values are converted to UTC on storage and converted back to local time on retrieval.
It doesn't look like there's actual DateTimeOffset support. (Personally I think it's a terrible idea to convert to local time on retrieval, but that's a slightly different matter.) Additionally, it looks like it's not really converting to local time properly, given the incorrect offset of 0.
I would suggest avoiding using DateTimeOffset with LiteDb until it's really supported (maintaining the offset).
This happens because LiteDB stores a DateTimeOffset using value.UtcDateTime. This is unfortunate, but it can't be changed due to compatibility. However, you can override this behavior by creating a custom serializer:
BsonMapper.Global.RegisterType<DateTimeOffset>
(
serialize: obj =>
{
var doc = new BsonDocument();
doc["DateTime"] = obj.DateTime.Ticks;
doc["Offset"] = obj.Offset.Ticks;
return doc;
},
deserialize: doc => new DateTimeOffset(doc["DateTime"].AsInt64, new TimeSpan(doc["Offset"].AsInt64))
);

Is there a way to have a common time users cannot modify even if they change time in settings

I'm working an an app that's Puts and Gets data to Firebase, based on Date, and Time my problem now is that users can modify it in settings and either get future data or old data I've tried using the dateTime.now however that can be modified I've also tried datetime.utcnow but it can also be modified. So my question is is there away I can work this out so users cannot modify time or even if they do the app time isn't affected.?
Sample of code stored to Firebase.
await CrossCloudFirestore.Current
.Instance
.Collection(DataPaths.NewsFeed)
.Document(guid)
.SetAsync(new {
ID = ticks + id,
ProfileImage = prefrences.profileImage,
UserName = usernames,
Title = title,
Date = DateTime.Now.ToString("dd MMM yyyy"),
Time = DateTime.UtcNow.ToString("HH:mm:ss"),
Details = details,
OrderTime = orderTime
});
Another Sample of Something I was Attempting.
//All 3 Needs sorting/fixing utc vs local/GMT time
//DateTime date = DateTime.UtcNow;
//var act = TimeZoneInfo.ConvertTime(date, TimeZoneInfo.Local);
//Application.Current.MainPage.DisplayAlert("", act.ToString(), "OK");
The solution is, that you should get the current time from an API rather than from the device.
The way to do it, is to set Date & Time to a value gotten from an API.
So I personally like this http://worldclockapi.com/api/json/est/now; copy this into your code where you would like to set your date & time, and you should be all fine.
You can edit the API URL to match your time zone; lets say you want Central Standard time, then this would be your API URL http://worldclockapi.com/api/json/cst/now.
Feel free to search for other APIs using Google.
Now, how do you set your variable to an API result value? ~ Like this:
var url = "http://worldclockapi.com/api/json/cst/now";
var client = new HttpClient();
var currentDateTime = client.Get(url);
await CrossCloudFirestore.Current
.Instance
.Collection(DataPaths.NewsFeed)
.Document(guid)
.SetAsync(new {
ID = ticks + id,
ProfileImage = prefrences.profileImage,
UserName = usernames,
Title = title,
Date = currentDateTime.ToString("dd MMM yyyy"),
Time = currentDateTime.ToString("HH:mm:ss"),
Details = details,
OrderTime = orderTime
});

Parse LUIS builtin.datetime.date

I have built a Luis model, and I have encountered issues parsing the builtin.datetime.date for an entry like "this weeked" returns 2016-W20-WE (or something similar) which doesnt parse successfuly using DateTime.Parse.
I came across this post which seems to use a class called LUISParse, but I can't find any reference to this on Google.
I have the same problem and see that in their sdk doc they use Chronic on the actual entity string rather than trying to interpret the date format. See Here:
So I did something like this:
Chronic.Parser parser = new Chronic.Parser();
EntityRecommendation date = new EntityRecommendation();
luisResponse.TryFindEntity("builtin.datetime.date", out date);
var dateResult = parser.Parse(date.Entity);
It works since the date.Entity only contains the date related string. Ex: I pass in "Yes I am planning to go next week" to Luis and the entity contains "next week".
Rather than use the raw value, I've found I had to dig into the internal elements and use the "value", "start" and "end" properties. Those properties at least follow date formats. This works for durations such as Weekend or Next 3 Days as well as Today and specific date requests.
if (entity.Type == "builtin.datetimeV2.daterange")
{
var resolutionValues = (IList<object>)entity.Resolution["values"];
foreach (var value in resolutionValues)
{
_start = Convert.ToDateTime(((IDictionary<string, object>)value)["start"]);
_end = Convert.ToDateTime(((IDictionary<string, object>)value)["end"]);
}
}
else if (entity.Type == "builtin.datetimeV2.date")
{
var resolutionValues = (IList<object>)entity.Resolution["values"];
foreach (var value in resolutionValues)
{
_when = Convert.ToDateTime(((IDictionary<string, object>)value)["value"]);
}
}

Unity C# Parse Query Problems

I'm new to Parse and Facebook SDK's and when looking through the documentation and other posts I still can't get the query working. When using a query to get the data back from the server it isn't allowing me to put the information into variables. For example, when the code is called, all that appears in the console is: "Parse.ParseObject".
var query = ParseObject.GetQuery("DataRetrieval")
.WhereEqualTo("RetrievalID", retrievalID);
query.FindAsync().ContinueWith(t =>
{
IEnumerable<ParseObject> createdAt = t.Result;
foreach ( var obj in createdAt)
{
Debug.Log(obj);
DateTime createdAtTime = obj.Get<DateTime>("createdAt");
Debug.Log(createdAtTime);
DateTime? updatedAt = dataRetrieval.UpdatedAt;
Debug.Log(updatedAt);
}
});
I've tried multiple different ways to get this to work. I'm also looking to retrieve data which I have stored, but I'm assuming that it will be done in the same/similar way to this once it's working.

Categories