I am using Xamarin Android to issue notification after 10 seconds using alarm manager but whatever time I set the notification is fired just after 5 seconds. I've tried reading the other posts from which I understood that I had to set milliseconds from after 1970, but didn't work
Attempt 1
Intent alarmIntent = new Intent(Application.Context, typeof(AlarmReceiver));
alarmIntent.PutExtra("message", "This is my test message!");
alarmIntent.PutExtra("title", "This is my test title!");
PendingIntent pendingIntent = PendingIntent.GetBroadcast(Application.Context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
AlarmManager alarmManager = (AlarmManager)Application.Context.GetSystemService(Context.AlarmService);
//alarmManager.Set(AlarmType.Rtc, SystemClock.ElapsedRealtime() + 30 * 1000, pendingIntent);
Java.Util.Calendar calendar = Java.Util.Calendar.Instance;
calendar.Set(CalendarField.HourOfDay, 21);
calendar.Set(CalendarField.Minute, 41);
alarmManager.Set(AlarmType.RtcWakeup, Calendar.GetInstance(Java.Util.TimeZone.Default).TimeInMillis+10000, pendingIntent);
Attempt 2
DateTime dtBasis = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); // Android times are quoted as milliseconds since start of 1970
alarmManager.Set(AlarmType.RtcWakeup, (long)DateTime.Now.AddMinutes(1).ToUniversalTime().Subtract(dtBasis).TotalMilliseconds, pendingIntent);
The standard method of getting the milliseconds since the epoch (01/01/1970) within Android is to use the System.currentTimeMillis() method. Therefore you would likely want to try setting the second parameter for alarmManager.set() to be System.currentTimeMillis() + 10000.
The C# equivalent would be to create a DateTime object representing the epoch
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
And then subtract it from the DateTime.UtcNow object to get a TimeSpan object from it
TimeSpan tsEpoch = DateTime.UtcNow.Subtract(epoch);
Finally, to retrieve the time in milliseconds, cast the timespan's TotalMilliseconds property to a long.
long milliSinceEpoch = (long) tsEpoch.TotalMilliseconds;
With that you simply add 10000 to that value and that should give you the result you are looking for.
This also helps in a great way
Java.Util.Calendar calendar = Java.Util.Calendar.Instance;
calendar.Set(CalendarField.HourOfDay, 20);
calendar.Set(CalendarField.Minute, 57);
alarmManager.Set(AlarmType.RtcWakeup, calendar.TimeInMillis,pendingIntent);
Related
I'm having a hard time understanding why this is happening or even if this should be happening. If I calculate the TimeSpan between two DateTime objects (same date, different times) and compare it to the same calculation using two TimeOnly objects I get different results.
var start = new DateTime(1, 1, 1, 14, 0, 0);
var end = new DateTime(1, 1, 1, 10, 0, 0);
Console.WriteLine(end - start); // Prints -4 hours
However...
var start = new TimeOnly(14, 0, 0);
var end = new TimeOnly(10, 0, 0);
Console.WriteLine(end - start); // Prints 20 hours???
Isn't the span between starting at 2pm and ending at 10am always a span of -4 hours? Interestingly enough if I take the second one and do Console.WriteLine(end.ToTimeSpan() - start.ToTimeSpan()); I end up with -4 hours.
This feels like an error on TimeOnly's part but I don't know. Here is a fiddle I did comparing results between NodaTime, System.DateTime, converting System.TimeOnly to TimeSpan, and System.TimeOnly.
I have class that open Pcap fileand have this 2 members: Seconds and Microseconds (both int).
How can i create from this 2 fields DateTime ?
This is what i have try:
DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, packet.Seconds);
And got this error:
Hour, Minute, and Second parameters describe an un-representable
DateTime.
Code:
var dateTime = new DateTime(1970, 1, 1).AddSeconds(packet.Seconds).AddTicks(packet.Microseconds * 10);
The problem is that the seconds parameter in that constructor must be between 0 and 59 inclusively. What you really want is 1970-1-1 plus the number of seconds.
var dateTime = new DateTime(1970, 1, 1).AddSeconds(packet.Seconds).AddSeconds(packet.Microseconds / 1000000.0);
I am having some trouble converting a date found in the file that stores browser history to a normal DateTime.
The file is located at: C:\Users[username]\AppData\Roaming\Mozilla\Firefox\Profiles[profilename]\places.sqlite
The table in question is: [moz_places]
The column is: [last_visit_date]
I've tried using the unix epoch and webkit format(like chrome uses), but neither are giving me the results I expect.
Here is my Unix conversion(not working):
public static DateTime FromUnixTime(long unixTime)
{
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return epoch.AddSeconds(unixTime);
}
This is my webkit conversion code: (Also not working for these dates, it works with chromes webkit dates)
public static DateTime ConvertWebkitTimeToDateTime(long ticks)
{
//Set up a date at the traditional starting point for unix time.
DateTime normalDate = new DateTime(1970, 1, 1, 0, 0, 0, 0);
//Subtract the amount of seconds from 1601 to 1970.
long convertedTime = (ticks - 11644473600000000);
//Devide by 1000000 to convert the remaining time to seconds.
convertedTime = convertedTime / 1000000;
//Add the seconds we calculated above.
normalDate = normalDate.AddSeconds(convertedTime);
//Finally we have the date.
return normalDate;
}
So what's the deal with these dates Firefox is storing? Here are a couple sample dates that should all be around today's date or yesterday's.(about 10/17/2013)
1373306583389000
1373306587125000
1373306700392000
Any help or documentation links would be awesome, thanks.
Unix timestamps are measured in seconds.
These values are larger by a factor of one million, i.e., they are using microseconds:
> select datetime(1373306583389000 / 1000000, 'unixepoch');
2013-07-08 18:03:03
Solution in C#:
public static DateTime FromUnixTime(long unixTime)
{
unixTime = unixTime / 1000000;
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return epoch.AddSeconds(unixTime);
}
CL. was correct in that this is simply the millisecond value from the Unix epoch.
Solution in SQL:
Here is a resource that explains his solution in greater detail:
https://support.mozilla.org/en-US/questions/835204
How do I get the current UTC time in ulong number of minutes since 01.01.2001 00:00 in c#? I know it involves DateTime.UtcNow property but how do I get the offset in minutes?
You can use:
ulong totalMinutes = (ulong) (DateTime.UtcNow - new DateTime(2001,1,1,0,0,0,0, DateTimeKind.Utc)).TotalMinutes;
You can use the DateTime.UtcNow function combined to a TimeSpan :
DateTime reference = new DateTime(2001, 01, 01, 0, 0, 0, DateTimeKind.Utc);
TimeSpan duration = new TimeSpan(DateTime.UtcNow.Ticks - reference.Ticks);
ulong minutesCount = Convert.ToUInt64(duration.TotalMinutes);
In my app I have a drop-down box of strings that shows possible hours in 12-hour time for the user to select. The possible values are:
9am
10am
11am
12pm
1pm
2pm
3pm
4pm
5pm
What code will convert one of these strings to a 24 hour integer? For example, 10am should be converted to 10 and 4pm should be converted to 16.
You can use DateTime.Parse(...) to get a DateTime value, and then reference the .Hour property for a result;
int h = DateTime.Parse("10am").Hour; // == 10
int h2 = DateTime.Parse("10pm").Hour; // == 22
DateTime.Parse is pretty liberal in what it allows, but obviously makes some assumptions internally. For example, in the above code, DateTime.Parse("10am") returns 10am on the current date in the current timezone (I think...). So, be aware of the context in which you use the API.
If you have a dropdown, why not set the values to be the integer values you desire:
<asp:DropDownList runat="server" ID="hours">
<asp:ListItem Value="9">9am</asp:ListItem>
<asp:ListItem Value="10">10am</asp:ListItem>
<!-- etc. -->
<asp:ListItem Value="17">5pm</asp:ListItem>
</asp:DropDownList>
Considering the times are continuous, you can simplify the logic:
var firstHourStr = box.Items[0].ToString();
var firstHour = int.Parse(firstHourStr.Replace("am", "").Replace("pm", ""));
if (firstHourStr.Contains("pm"))
{
firstHour += 12;
}
var selectedHour = firstHour + box.SelectedIndex;
If the hours are static, and you know the first hour, you could have a const and simplify the process by much with var selectedHour = FIRST_HOUR + box.SelectedIndex.
Also, I assumed valid formats as shown in the question.
Final note: You'll need to handle the 12pm case which causes problems, due to the nature of the hour 12 being a single second after "am".
You could use DateTime.Parse, but that would not play nicely with internationalization.
int hour = DateTime.Parse(stringValue).Hour;
Instead, just use DateTime objects in the ComboBox and format them using FormatString:
// In Constructor:
cbHours.Items.Add(new DateTime(2000, 1, 1, 8, 0, 0));
cbHours.Items.Add(new DateTime(2000, 1, 1, 10, 0, 0));
cbHours.Items.Add(new DateTime(2000, 1, 1, 13, 0, 0));
cbHours.FormatString = "h tt";
// In event handler
if (cbHours.SelectedIndex >= 0)
{
int hour = ((DateTime)cbHours.SelectedItem).Hour
// do things with the hour
}