How to set accurate time for Xamarin.Android AlarmManager - datetime

I am setting local notifications to fire at specific time in Xamarin.Android application. It all works but the set time seems to be little off, set time comes from user input (with minute precision). When setting notifications further in the future (days later) the notification fired at the next minute than it should have.
Below is my current code to calculate time. calendarEvent.StartTime is a DateTime property.
TimeSpan span = calendarEvent.StartTime - DateTime.Now;
manager.Set(AlarmType.ElapsedRealtime,(long)(SystemClock.ElapsedRealtime() + span.TotalMilliseconds),pendingIntent);
I would like to know how to accurately calculate the time so that notifications would fire at the start of the minute they are supposed to. In the current code they fire in the middle of the minute or later.

If you really need precision try to use the SetExact() method of the alarm manager class.
TimeSpan span = calendarEvent.StartTime - DateTime.Now;
manager.SetExact(AlarmType.ElapsedRealtime,(long)(SystemClock.ElapsedRealtime() + span.TotalMilliseconds),pendingIntent);
And I don't know if it is relevant, but if you are using events for a calendar maybe you should use RTC, since AlarmType.RTC is based on the clock, whereas AlarmType.ElapsedRealTime is based on the time passed since the device was turned on.

Related

Get Firestore Server Timestamp from the client side

I'm building an app that tracks time. It calculates the time by differentiating the seconds like so:
serverTimestamp: FieldValue // {seconds: number, milliseconds: number}.
getSeconds() {
const createdTime = new Date(this.serverTimestamp * 1000).getTime()
const currentTime = new Date().getTime();
return Math.abs((currentTime - createdTime) / 1000);
}
The problem is that Date's values equal to the current client's device clock, while firestore's timestamp equal to the firestore server, which may cause inequivalence between these two dates.
The leads me with two alternatives:
1. Save Date instead of serverTimestamp().
This way, the difference between these two dates will be accurate. Although, they won't represent the real date. Plus, if the user will change his clock on his machine, then the seconds would be changed too.
2. Create an HTTPS function that retrieves the current server timestamp.
This way, the difference between these two dates will be accurate either. Although, it feels like I'm going too far only to get the current timestamp of the server.
I am probably going to stick with alternative number two. But I was hoping if there's a better solution to achieve my goal.
PS - I'm using #angular/fire. So, solutions from this package would be welcomed.
You can also write the current timestamp with a serverTimestamp, then read the time back out of the location it was just written. Then you can make changes or calculations with that value.

Fullcalendar Start Hour Setting by the Time of Current Hour

I am trying to use Fullcalendar in AngularJS.
I somehow implemented the calendar and it works (saves data to the SQL).
However, if I click on the day in the calendar, the modal pops up and the start date shows 00:00:00 in time aspect.
My questions is how can you set the time for the hour of current time?
If it is 9AM currently, then, how can the time in the start initialize the time as 09:00:00 ?
This is what I have for the coding.
select: function(start, end) {
$('#ModalAdd #start').val(moment(start).format('YYYY-MM-DD HH:mm:ss'));
$('#ModalAdd #end').val(moment(end).format('YYYY-MM-DD HH:mm:ss'));
$('#ModalAdd').modal('show');
}
I have a feeling that it would be nice to somehow modify the code below and place it within the above code, but I am stuck on where to put it.
var time = new Time();
var h = date.getHour();
I don't know it the Time() even works (it was Date() from other source).
Please can anyone help me on initializing the hour in the Fullcalendar based on the current hour? I am looking for any advice or even a hint to solve this matter.
Thank you in advance!
You can use momentJS to add the current (local) system time to the selected day:
select: function(start, end) {
var today = moment();
start.set({ hours: today.hours(), minute: today.minutes() });
$('#ModalAdd #start').val(start.format('YYYY-MM-DD HH:mm:ss'));
$('#ModalAdd #end').val(end.format('YYYY-MM-DD HH:mm:ss'));
$('#ModalAdd').modal('show');
}
See https://momentjs.com/docs/#/get-set/set/
Also it's worth mentioning that start and end are already moments, so you don't need to wrap them in the moment constructor again as you were doing before.
Another thing to consider if you do this, is whether your calendar has other views available, in particular the agenda-style views, on which selections can be made which would trigger the modal? If so, then you need to ensure that the time-manipulation code above only runs when the view is "month", because the agenda view will, by default, already use the time that the user actually chose on the calendar.

What is the best way to get the date and time in server side?

I'm developing an asp.net mvc3 project. I have a trouble in this problem that I encounter. I will give a scenario so that it will understand well.
Scenario:
I have 2 PC (PC1(server) and PC2(client)). For example both two PC has different date and time let say for PC1 is +8GMT Date 8/10/2016 and for PC2 +8GMT Date 8/9/2016. I am using the PC2 the client and i'm using a code for getting the time is DateTime.Now(); in my controller and the time is display in label in one of my views. I tried to adjust the Date and Time of the PC2 the label for displaying t he time also change. What I want is even I change the Date and Time in PC2 it won't affect/change the displayed Date and Time in my label it will stick on what the Date and Time in the PC1.
This scenario is i'm using/testing the publish project
Any suggestions are welcome.
I'm not sure if I understand the question, but you might consider looking into the DateTime.ToUniversalTime method, which converts the value of the DateTime object to UTC.
This way, you may be able to work with a standard time from which you can convert to any time zone you might want to use to display in your application, regardless of server location.
DateTime serverTime = DateTime.Now;
DateTime utcTime = serverTime.ToUniversalTime;
string timeZoneId = "some time zone id";
TimeZoneInfo myTime = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
DateTime label = TimeZoneInfo.ConvertTimeFromUtc(utcTime, myTime);
I am not very clear on your description. It seems as if you want your label to always show the time on the server (PC1) when the web page is loaded on the client by calling the site on the server, maybe like https://pc1. What you're doing should accomplish that: the time displayed in your label will be the system time from the server. Changing the time on the client will not affect it.
If you want your client (PC2) to show its local time, you will need to use code that runs on the client, i.e. JavaScript in most cases.
Working with dates in Javascript can be a little different from other languages/expectations, so I suggest reading the docs at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date.
Get started with var currentDate = new Date();.

Scheduling a Job with a fixed termination date in asp.NET using chroniton.NETCore

I'm working on a WebAPI that schedules Events and appointments.In order to do that, I am using the chroniton.NetCore package. I am able to set the schedule using a cron expression or other functions, but is there any way to set a stop date or termination date for the schedule?
var schedule = new EveryXTimeSchedule(TimeSpan.FromSeconds(5));
var scheduledJob = singularity.ScheduleParameterizedJob(
schedule, job, "Hello World", true); //starts immediately
I wish to stop this job on the next day at midnight without using Task.Delay(since the api is stateless)
Thank you.
Current version of chroniton.NETCore does not support such behaviour out of the box. You can implement your own logic based on fact, that you have information about time range.
ScheduleParameterizedJob() returns an IScheduledJob which can be used to cancel the job if needed. You can create additional RunOnceSchedule to close your first job on the next day at midnight using
Singularity.Instance.StopScheduledJob(IScheduledJob job);
you can inherit from EveryXTimeSchedule and override NextScheduledTime method to return Chroniton.Constants.Never if 'next scheduled time' is >= the next day at midnight;
Best way to create a Midnight DateTime in C# :
DateTime nextDayAtMidnight = DateTime.Today.AddDays(1)

BidSystem, online auction, problem with timing

guys!
I'm developing an online auction with time limit.
The ending time period is only for one opened auction.
After logging into the site I show the time left for the open auction. The time is calculated in this way:
EndDateTime = Date and Time of end of auction;
DateTime.Now() = current Date and Time
timeLeft= (EndDateTime - DateTime.Now()).Seconds().
In javascript, I update the time left by:
timeLeft=timeLeft-1
The problem is that when I login from different browsers at the same time the browsers show a different count down.
Help me, please!
I guess there will always be differences of a few seconds because of the server processing time and the time needed to download the page.
The best way would be to actually send the end time to the browser and calculate the time remaining in javascript. That way the times should be the same (on the same machine of course).
Roman,
I had a little look at eBay (they know a thing or two about this stuff :)) and noticed that once the item is inside the last 90 seconds, a GET request gets fired every 2 seconds to update the variables in the javascript via a json response. you can look at this inside firebug/fiddler to see what it does.
here is an example of the json it pulls down:
{
"ViewItemLiteResponse":{
"Item":[
{
"IsRefreshPage":false,
"ViewerItemRelation":"NONE",
"EndDate":{
"Time":"12:38:48 BST",
"Date":"01 Oct, 2010"
},
"LastModifiedDate":1285932821000,
"CurrentPrice":{
"CleanAmount":"23.00",
"Amount":23,
"MoneyStandard":"£23.00",
"CurrencyCode":"GBP"
},
"IsEnded":false,
"AccessedDate":1285933031000,
"BidCount":4,
"MinimumToBid":{
"CleanAmount":"24.00",
"Amount":24,
"MoneyStandard":"£24.00",
"CurrencyCode":"GBP"
},
"TimeLeft":{
"SecondsLeft":37,
"MinutesLeft":1,
"HoursLeft":0,
"DaysLeft":0
},
"Id":160485015499,
"IsFinalized":false,
"ViewerItemRelationId":0,
"IsAutoRefreshEnabled":true
}
]
}
}
You could do something similar inside your code.
[edit] - on further looking at the eBay code, altho it only runs the intensive GET requests in the last 90 seconds, the same json as above is added when the page is initially loaded as well. Then, at 3 mins or so, the GET request is run every 10 seconds. therefore i assume the same javascript is run against that structure whether it be >90 seconds or not.
This may be a problem with javascript loading at different speeds,
or the setInterval will trigger at slightly different times depending on the loop
i would look into those two

Resources