TimePicker doesn't allow "24:00"? - android-widget

I need to create a "from… to" time selection and created 2 TimePicker components for that. Because the database behind it already exists since years and uses a 24 hour format, in my code, I'm setting
tpOpenFrom.setIs24HourView(true);
tpOpenTo.setIs24HourView(true);
My problem now is that as it seems, "24:00" can't be selected as a "to:" value, as after "23"(:00), the component switches "back" to "00"(:00), making it impossible to find out whether the user forgot to make an actual selection, like if "09:00" has been entered as "from" time and "00:00" as "to" time. Or if a selection of "00:00" to "00:00" has been made: has the user forgotten to enter anything or did he intend to define "opened around the clock"?
In 24 hour mode, the TimePicker should really allow 24:00 to be entered. The 24-hour clock system in ISO 8601 defines midnight as a special case, than can be referred to as both "00:00" and "24:00", so TimePicker should allow the hours to be set to 24 and automatically block the minutes to 00.
Does anyone have an idea how to achieve that utilizing the TimePicker?

I had the same problems with a bootstrap timepicker but the is not difficult to implement. I am using the timepicker of Joris de Wit (http://jdewit.github.io/bootstrap-timepicker/).
search for "24" and replace all occurences with "25", then search for "23" in the bootstrap-timepicker.js and replace them with "24".
if you want to allow times like 24:01, .., 24:59, you're done. If you only want to accept 24:00 but not 24 with minutes, go to line 133 where the getTime function is.
Put the following code in front of the existing return (but don't delete it):
if (this.hour == 24 && this.minute != 0) {
this.setTime('00:' + this.minute);
return '00:' + this.minute;
}
This will convert any 24:XX time to 00:XX.

Related

Getting timezone abbreviations in Swift

I am trying to retrieve the timezone abbreviations of the local time zone by using the following code.
private func getLocalTimezone() -> String {
guard let localTimezone = TimeZone.current.abbreviation() else {
return ""
}
return localTimezone
}
But when I am in Indian time zone I am always getting GMT+5:30 where I need it as IST. Its coming correctly when I am in CST or PST. Can anyone please suggest a way to reach to the solution for this issue.
Thanks a lot in advance
This is because time zone abbreviations are locale sensitive. IST only means India Standard Time (Asia/Kolkata) in India. In other parts of the world, it could mean Israel Standard Time, or Irish Standard/Summer Time. Here's a site that shows you the list of abbreviations. You can see for yourself how ambiguous they can be.
This is why abbreviation() takes into account the region of your phone, i.e. this setting:
abbreviation() will give you "IST" if your device's region is India. If your phone is somewhere else, it shows "GMT+5:30" because that is the safest, most unambiguous option.
If you want it to output IST no matter where your device is, you need to hard code this by creating a dictionary of time zone identifiers to abbreviations that you want. There is a built in abbreviationDictionary that goes the other way - abbreviations to identifiers. You can search it, and it will work for IST (Asia/Kolkata), but might not work for whatever other time zone that you are interested in.
let abbreviation = TimeZone.abbreviationDictionary
.first(where: { $1 == TimeZone.current.identifier })?.key
and I'm not sure whether the contents of this dictionary will stay the same in future versions of iOS. Use it at your own risk.

How do I "add" time to firebase timestamp in Swift?

This question is best stated in an example:
It is currently 9:00am. User wants to do activity at 4:00pm the following day. They use UIDatePicker to select 4:00pm the next day, and then hit a button. I know firebase does times in milliseconds from 1970, so what I want to do is "add" the number of milliseconds from 9:00am to 4:00pm the following day to the ServerValue.timestamp(), like so:
activitiesRef.child(newActivity.id).setValue([
"id": newActivity.id,
"name": newActivity.name,
"isActive": newActivity.isActive,
"locString": newActivity.locationString,
"locLat": newActivity.locLat,
"locLong": newActivity.locLong,
"privacySetting": newActivity.privacySetting,
"targetTime": ServerValue.timestamp()]) // + some added value of time
//"targetTime": [".sv": "timestamp"]])
The reason for this is because I will be displaying a countdown timer elsewhere in the app until it reaches the targetTime. If I can push to firebase the targetTime, then the countdown timer will be a simple comparison of the current time on the user's phone to the targetTime itself.
The error I keep getting when trying to add a double value to the ServerValue.timestamp() is "Contextual type 'Any' cannot be used with dictionary literal"
If it is not possible to do so, what other options do I have? Thank you.
ServerValue.timestamp() is not a number that you can use to perform date arithmetic. It's a special placeholder value that the server side interprets with its sense of time.
The best you can do is write the timestamp, read it back out as a number, then perform math on it.

Strange date picker behaviour Xcode, swift 3

My current project is a timer which uses a date picker to set the amount of time the user wants before the timer goes off (say 1 minute, 6 hours and two minutes etc.). The problem lies in the amount of time that the date picker believes it has been set for. Below is the code which I am using to set the time.
#IBOutlet weak var datePicker: UIDatePicker!
var timeAmount:Double = 0
#IBAction func startButton() {
timeAmount = Double(datePicker.countDownDuration)
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: timeAmount, repeats: false)
}
Here it can be seen that the function startButton, sets the value of timeAmount to be the amount of time that the date picker is set for. This timeAmount value is then used in a local notification as the time.
The issue is that the datePicker.countDownDuration is never correct. If it is set for one minute, then timeAmount may return a value of 99 seconds, 62 seconds, 67 seconds etc. It is just completely random.
Maybe I do not entirely understand how the .countDownDuration feature works, however from everything I have read, this should return a value of 60 seconds.
Any thoughts and suggestions on the matter will be very much appreciated, thank you.
By default, the UIDatePicker will take the current second value for the calculation.
timeAmount = (number of minutes selected in picker * 60) + current time second value.
For example:
If the current time is 13:40:25 (1 PM 40 minutes 25 seconds) and you have selected one minute in the date picker, the value of timeAmount in this case is 85.
timeAmount = (1*60) + 25
This will solve your problem.
Go to your Storyboard and select UIDatapicker. Navigate to Date option in Attributes inspector.
Click the Dropdown and change it to Custom from Current Date.
You will see a new text field with the current time as the custom date.
Just change the second field to 00.
Run the App now. Now it will take 0 as the value for the second and you will able to see seconds value correctly based on the time you are choosing in the date picker.
Hope this will solve your problem.

email reminders time driven google spread

Can anyone help.
I have a simple google spreadsheet that enables me to book delegates onto a variety of courses at different venues and at different times and dates.
I used the same spreadsheet last year, kindly set up by Bob Rashkin! It worked perfectly and sent emails when delegates booked onto the course and also sent email reminders automatically when the course was 9 days or so from starting.
I have duplicated the spreadsheet for this term but the sheet refuses to send the reminder emails, I really could do with a little help on this?
A secondary matter is that when the script sends a booking confirmation email, it sometimes sends two of them which creates a lot of phone calls asking if they've booked on twice.
Help someone please
This is the reminder script that I'm using
function Reminder() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var s=ss.getActiveSheet();
var r1=s.getDataRange().getRow()+1;//Start past the header row!!! Doh!
var r2=s.getDataRange().getLastRow();
var mn,m,days,d=new Date(),coursedate=new Date(),dlen=8.64e7,i,course,r,year;//8.64e7
var subject="Just a gentle reminder that you or colleague(s) from your setting have a Paediatric First Aid course coming up in the next week or so. ";
var recipient, body, tail="Please be aware that Entrust (formerly Staffs Early Years) will make a charge for non attendance so";
tail+=" please make sure that you familiarise yourself with the times and dates of the course. ";
tail+="If you need help finding the venue then please follow the link below to find the venue and print off a map if required.";
tail+="\n\nhttp://www.blithfieldsafety.co.uk/venues/";
for (r=r1;r<=r2;r++) {
recipient=s.getRange(r,9).getValue();
course=s.getRange(r, 2).getValue();
body=subject+"\nCourse Details\n"+course+"\n\nDelegate Name: "+s.getRange(r,4).getValue()+"\n\n"+tail; //changed (r,2) to (r,4)
mn=course.match(/Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/)[0];
m=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"].indexOf(mn);
days=course.match(/(\d{1,2})(st|nd|rd|th)/g);
year=d.getFullYear();
for (i in days) {
coursedate.setMonth(m);
coursedate.setDate(days[i].slice(0,-2));
if ((coursedate-d)/dlen<14 && s.getRange(r,16).getValue()!="mail sent") {
GmailApp.sendEmail(recipient, subject, body);
s.getRange(r,16).setValue("mail sent");//arbitrarily picked col 15
}
}
}
};
It looks like the time remaining until the course begins is checked with this line:
if ((coursedate-d)/dlen<14 && s.getRange(r,16).getValue()!="mail sent") {
That line is making a date calculation, and checking if the value of a cell had the text "mail sent" in it. If the cell didn't have mail sent AND the date calculation is true, then an email gets sent. Notice that the divisor dlen is a value that was hard coded.
var mn,m,days,d=new Date(),coursedate=new Date(),dlen=8.64e7,i,course,r,year;//8.64e7
The value of the variable dlen is set to a constant value of 8.64e7. But I'm guessing that the math no longer works right for some reason.
The value of dlen is 86400000.
There are 24 hours in a day, 60 minutes in an hour, and 60 seconds in a minute.
24 * 60 * 60 = 86,400 seconds in a day. So, obviously, the 86,400,000 number is a multiple of the number of seconds in a day.
I'm guessing that there is a new course every month?
You can add a Logger.log() statement to your code, and run it so see what the variable values are:
Logger.log('coursedate: ' + coursedate);
If you put that line in your code:
for (i in days) {
coursedate.setMonth(m);
Logger.log('coursedate: ' + coursedate);
coursedate.setDate(days[i].slice(0,-2));
Logger.log('coursedate: ' + coursedate);
Logger.log('d: ' + d);
if ((coursedate-d)/dlen<14 && s.getRange(r,16).getValue()!="mail sent") {
That would tell you what the values of the variables coursedate and d are right when the calculation is being made.
Then you could hopefully figure out why the math is the way it is. To view the Logger.log() output, click on the View menu, and choose the Logs menu item. A list of logged values will be in the window.

UIDatePicker, time, and 1970

I've come across a small issue that I've been chewing on for a day or two now.
Using the Apple example project called DateCell I lifted its UIDatePicker and set it to time. I used this particular code as it did the animated slide on/off the screen.
My workflow is to set four values, start time, lunch time out, lunch time in, and stop time. I set these values by using [NSDate date].
I then use NSCalander:component calls to do some math such as "add 30 minutes to start time to get lunch time out," and "start time - lunch time out - 8 hours to get stop time."
The initial setup goes just fine. Clicking on the start time cell brings up the picker, and selecting a time change the other three times following my simple math formula's.
If the second row is selected, the lunch time out, the wheel comes up again to pick your lunch time out time. However this is where my problems start. It seems that my UIDatePickerModeTime wheel returns a date portion for January 1, 1970. And its this date portion that messes up my math formulas.
My question is, what can I do to fix this?
I've tried setting an initial, minimum time, in the XIB for the picker. This sort of works but when you pick a time on the wheel, the wheel spins itself to the time set in the XIB. This method doesn't have a clean feel to it.
I've tried settings the initWithTimeInterval class methods, but these block out times and isn't what I'm looking for I think.
I've also tried the NSDateFormatter:stringFromDate|dateFromString calls, and these had no affect.
What I have not done yet:
Custom defined date/time string.
Rebuilding UIDatePicker:Time from scratch
Am I over looking anything?
Thanks.
I've solved my problem, and here is how I addressed it.
Before I get to my answer I'm still really new to Objective-C and object oriented programming so my vocabulary doesn't know how to describe some of the things I've tried explaining. So take this into account when reading this.
Using UIDatePicker in time mode, i.e. you go into your NIB/XIB file and set your UIDatePicker object to 'time', will only return time. This is where I went wrong.
Using the NSDateComponent or any of the NSCalendar methods will bring out the date component of the picker. Thus you'll see January 1st, 1970 in the NSLog returns for example.
I had to find a new way of doing my math and manipulation of the times I was getting from the picker.
What I ended up using is NSTimeInterval, dateByAddingTimeInterval, and timeIntervalSinceDate. Research showed that NSTimeInterval is also a float type, so I used float to do some math as well.
Here an example -
if (indexPath.row == [labelArray indexOfObjectIdenticalTo:#"Clock Out"])
{
NSTimeInterval diffClockInOutToLunch = [outToLunch timeIntervalSinceDate:clockIn];
float remainingInTheDay = 28800 - diffClockInOutToLunch;
self.clockOut = [inFromLunch dateByAddingTimeInterval:remainingInTheDay];
cell.detailTextLabel.text = [self.dateFormatter stringFromDate:clockOut];
self.clockOutIndex = indexPath;
return cell;
}
I'm using a TableView to display my fields. When this 'if' statement is tripped it will populate the detailTextLabel of the line displaying "Clock Out." Visually the phrase "Clock Out" will be on the left side of the row, the time will be on the right side.
diffClockInOutToLunch is defined as a NSTimeInterval type. The operation being performed is timeIntervalSinceDate which essentially subtracts the value of outToLunch from the value of clockIn. Imagine outToLunch as being 11:00pm and clockIn as being 6:00am. This difference is 5 hours. NSTimeInterval stores values as seconds only so this difference of 5 hours is 18000 seconds.
I then perform a normal math operation using float. In this case I want to find out how many hours remain in the work day. This assumes the hours worked in a day is 8 hours. Because NSTimeInterval returns seconds, I converted 8 hours into seconds (28,800 seconds) and then subtract diffClockInOutToLunch from 28800. Now remainingInTheDay is equal to to 10800, or 3 hours.
The next operation I perform is set clockOut to the time our work day is finished. To do this I use the dateByAddingTimeInterval operation, which also is a NSDate method, so whatever it returns will be in a date/time format. In this operation we add remainingInTheDay (10,800 seconds) to inFromLunch (11:30am for example). Our clockOut time is now 2:30pm which is then sent through my DateFormatter and returned as a string to the cell of the TableView and also stored for later use.
Here's another example, from further down in my code -
- (void)clockInChanged
{
// Set clockIn value
self.clockIn = self.pickerView.date;
// Change the outToLunch time
self.outToLunch = [self.pickerView.date dateByAddingTimeInterval:5*60*60];
UITableViewCell *outToLunchCell = [self.tableView cellForRowAtIndexPath:outToLunchIndex];
outToLunchCell.detailTextLabel.text = [self.dateFormatter stringFromDate:outToLunch];
// Change the inFromLunch time
self.inFromLunch = [outToLunch dateByAddingTimeInterval:30*60];
UITableViewCell *inFromLunchCell = [self.tableView cellForRowAtIndexPath:inFromLunchIndex];
inFromLunchCell.detailTextLabel.text = [self.dateFormatter stringFromDate:inFromLunch];
// Change the clockOut time
NSTimeInterval diffClockInOutToLunch = [outToLunch timeIntervalSinceDate:clockIn];
float remainingInTheDay = 28800 - diffClockInOutToLunch;
self.clockOut = [inFromLunch dateByAddingTimeInterval:remainingInTheDay];
UITableViewCell *clockOutCell = [self.tableView cellForRowAtIndexPath:clockOutIndex];
clockOutCell.detailTextLabel.text = [self.dateFormatter stringFromDate:clockOut];
}
In this example, we've previously determined that the row pertaining to "Clock In" time was selected ("Touch Up Inside" if you will) and we drop to this method.
What happens in this method is whenever clockIn is changed using the picker, the times displayed in outToLunch, inFromLunch, and clockOut automatically update and are displayed.
This example shows that we capture the value on the picker (self.pickerView.date) as clockIn. We then use clockIn to seed our mess of dateByAddingTimeInterval's and so forth.
So. This is how I managed my times using UIDatePicker (which is set to time mode).
The short answer would be I was using the wrong methods to work with what my picker was turning.
I hope this helps you and hopefully it'll be here if I need it again too ;)