I Want to Switch Time Scrubber to Do 15 Min Intervals

I want to Switch Time scrubber to do 15 min intervals

For you case, it's better to use some portions scaling. try this code below. I believe it's pretty straightforward.

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var slider: UISlider!
@IBOutlet weak var label: UILabel!

let ticksCount: Float = 13*4 // 15mins intervals count from 8am till 9pm
let calendar = NSCalendar.currentCalendar()
let todayDate = NSDate()

override func viewDidLoad() {
super.viewDidLoad()

slider.minimumValue = 0
slider.maximumValue = ticksCount
}

@IBAction func onValueChanged(slider: UISlider) {

let minutesFromDayStart = 8 * 60 + Int(slider.value) * 15 // total minutes from day start, assuming we use intervals from 8am
let timeSlotDate = dateWithTimeSlotForDate(todayDate, minutesFromDayStart: minutesFromDayStart)
showDate(timeSlotDate)
}

private func showDate(date: NSDate) {
let df = NSDateFormatter()
df.dateStyle = .MediumStyle
df.timeStyle = .MediumStyle

label.text = df.stringFromDate(date)
}

private func dateWithTimeSlotForDate(date: NSDate, minutesFromDayStart: Int) -> NSDate {

let dayStart = dayStartDateForDate(date)
return calendar.dateByAddingUnit(.Minute, value: minutesFromDayStart, toDate: dayStart, options: NSCalendarOptions(rawValue: 0))!
}

private func dayStartDateForDate(date: NSDate) -> NSDate {
return calendar.startOfDayForDate(date)
}
}

How to convert 10-minute time blocks to 1-minute intervals in R

Just merge your xts object with the sequence of times you want.

Data <- structure(list(datetime=structure(c(1322641920, 1322642520, 1322643120,
1322643720, 1322644320, 1322644920, 1322645520), class=c("POSIXct", "POSIXt"),
tzone=""), count=c(100L, 60L, 10L, 10L, 200L, 180L, 190L)),
.Names=c("datetime", "count"), row.names=c(NA, -7L), class="data.frame")

data_xts <- as.xts(Data[,-1], order.by=Data$datetime)
data_xts <- merge(data_xts, seq(start(data_xts)-60*9, end(data_xts), by="1 min"))

Then you can use na.locf or similar to fill in the missing values, if you want.

data_xts <- na.locf(data_xts)

Slider with real time in Label

If you just need a time chooser with slider, use the following code in the sliderChanged: handler:

- (IBAction)onSliderChange:(id)sender {

UISlider *slider = (UISlider *)sender;

NSUInteger numberOfSlots = 24*4 - 1; //total number of 15mins slots

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"h-mm a"];

NSDate *zeroDate = [dateFormatter dateFromString:@"12-00 AM"];

NSUInteger actualSlot = roundf(numberOfSlots*slider.value);
NSTimeInterval slotInterval = actualSlot * 15 * 60;

NSDate *slotDate = [NSDate dateWithTimeInterval:slotInterval sinceDate:zeroDate];
[dateFormatter setDateFormat:@"h-mm a"];

self.timeLabel.text = [dateFormatter stringFromDate:slotDate];
}

Roundoff Timespan to 15 min interval

I think you want something like this:

public static class TimeSpanExtensions
{
public static TimeSpan RoundToNearestMinutes(this TimeSpan input, int minutes)
{
var totalMinutes = (int)(input + new TimeSpan(0, minutes/2, 0)).TotalMinutes;

return new TimeSpan(0, totalMinutes - totalMinutes % minutes, 0);
}
}

If you pass in 15 as your chosen interval for rounding, the function will first add 7 mins, then round down to the nearest 15 mins. This should give you what you want.

Because the above is written an extension method, you can use it like this:

var span1 = new TimeSpan(0, 10, 37, 00).RoundToNearestMinutes(15);
var span2 = new TimeSpan(0, 10, 38, 00).RoundToNearestMinutes(15);

The first one becomes 10:30, and the second one becomes 10:45 (as desired).

R: Split observation values by and aggregate to time intervals

Here's a data.table approach which allows you to use SQL-type queries to sort/filter data and perform operations.

DATA

> p
obs name start end diff_corr
1: C2 C2 2017-05-22 04:25:00 2017-05-22 04:26:30 1.4673913
2: C2 C2 2017-05-22 04:26:30 2017-05-22 04:27:30 0.9782609
3: C2 C2 2017-05-22 04:27:30 2017-05-22 04:28:00 0.4891304
4: C2 C2 2017-05-22 04:28:00 2017-05-22 04:30:00 1.9565217
5: C2 C2 2017-05-22 06:03:00 2017-05-22 06:03:30 0.4891304
6: C2 C2 2017-05-22 06:03:30 2017-05-22 06:05:30 1.9565217
7: C2 C2 2017-05-22 06:05:30 2017-05-22 06:06:00 0.4891304
8: C2 C2 2017-05-22 06:06:00 2017-05-22 06:06:20 0.3260870
9: C2 C2 2017-05-22 06:06:20 2017-05-22 06:07:00 0.6521739
10: b b 2017-06-09 04:23:00 2017-06-09 04:26:00 2.9670330
11: b 981 2017-06-09 04:23:00 2017-06-09 04:26:00 2.9670330
12: b 1627 2017-06-09 04:23:00 2017-06-09 04:26:00 2.9670330
13: b b 2017-06-09 04:26:00 2017-06-09 04:27:00 0.9890110
14: b b 2017-06-09 04:27:00 2017-06-09 04:33:00 5.9340659
15: b 981 2017-06-09 04:27:00 2017-06-09 04:33:00 5.9340659
16: b 1627 2017-06-09 04:27:00 2017-06-09 04:33:00 5.9340659
17: b b 2017-06-09 04:33:00 2017-06-09 04:35:00 1.9780220
18: b b 2017-06-09 04:35:00 2017-06-09 04:37:00 1.9780220
19: b b 2017-06-09 04:37:00 2017-06-09 04:39:00 1.9780220
20: b b 2017-06-09 04:51:00 2017-06-09 04:52:00 0.9890110

CODE

library(data.table)
library(lubridate)
p <- as.data.table(p)
p[, .(new_diff = mean(diff_corr)), .(tme_start = round_date(start, unit = "15min"))]

OUTPUT

> p[, .(new_diff = mean(diff_corr)), .(tme_start = round_date(start, unit = "15min"))]
tme_start new_diff
1: 2017-05-22 04:30:00 1.2228261
2: 2017-05-22 06:00:00 0.7826087
3: 2017-06-09 04:30:00 3.3626374
4: 2017-06-09 04:45:00 0.9890110

What is Data.Table doing?

Since you aren't familiar with data.table, here's a very quick, elementary description of what is happening. General form of the data.table call is:

DT[select rows, perform operations, group by] 

Where DT is the data.table name. Select rows is a logical operation e.g. say you want only observations for C2 (name), the call would be DT[name == "C2",] There is no operation required to be performed and no grouping. If you want the sum of diff_corr column for all name == "C2", the call becomes DT[name == "C2", list(sum(diff_corr))]. Instead of writing list() you can use .(). The output will now have a only one row and one column called V1 which is the sum of all diff_corr when name == "C2". The column doesn't have a lot of information so we assign it a name (can be the same as the old one): DT[name == "C2", .(diff_corr_sum = sum(diff_corr))]. Suppose you had another column called "mood" which reported the mood of the person making the observation and can assume three values ("happy", "sad", "sleepy"). You could "group by" the mood: DT[name == "C2", .(diff_corr_new = sum(diff_corr)), by = .(mood)]. The output would be three rows corresponding to each of the moods and one column diff_corr_new. To understand this better try playing around with a sample dataset like mtcars. Your sample data doesn't have enough complexity etc. to allow you to explore all of these functions.

Back to the answer - other variations

It's not clear from the question or comments if you want to round based on start or end. I used the former but you can change that. The example above uses mean but you can perform any other operations you may need. The other columns seem more or less redundant since they are strings and you can't do much with them. You could use them to further sort the results in the by entry (last field in the code). Below are two examples using obs and name respectively. You can also combine all of them together.

> p[, .(new_diff = mean(diff_corr)), .(tme_start = round_date(start, unit = "15min"), obs)]
tme_start obs new_diff
1: 2017-05-22 04:30:00 C2 1.2228261
2: 2017-05-22 06:00:00 C2 0.7826087
3: 2017-06-09 04:30:00 b 3.3626374
4: 2017-06-09 04:45:00 b 0.9890110

> p[, .(new_diff = mean(diff_corr)), .(tme_start = round_date(start, unit = "15min"), name)]
tme_start name new_diff
1: 2017-05-22 04:30:00 C2 1.2228261
2: 2017-05-22 06:00:00 C2 0.7826087
3: 2017-06-09 04:30:00 b 2.6373626
4: 2017-06-09 04:30:00 981 4.4505495
5: 2017-06-09 04:30:00 1627 4.4505495
6: 2017-06-09 04:45:00 b 0.9890110

Is there a way to do repetitive tasks at intervals?

The function time.NewTicker makes a channel that sends a periodic message, and provides a way to stop it. Use it something like this (untested):

ticker := time.NewTicker(5 * time.Second)
quit := make(chan struct{})
go func() {
for {
select {
case <- ticker.C:
// do stuff
case <- quit:
ticker.Stop()
return
}
}
}()

You can stop the worker by closing the quit channel: close(quit).

Setting time on UISlider change

I think you are overcomplicating what you are doing. You don't need to create a date for the slot with a string based on the 12 or 24 hour format that the user is using, you can just use an NSDate and let the formatter display the date appropriately.

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UISlider *slider;
@property (strong, nonatomic) NSDateFormatter *formatter;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.formatter = [NSDateFormatter new];
[self.formatter setLocale:[NSLocale currentLocale]];
[self.formatter setDateStyle:NSDateFormatterNoStyle];
[self.formatter setTimeStyle:NSDateFormatterShortStyle];

// Lazy way to set up the initial time
[self sliderMoved:self.slider];

}

#pragma mark - Actions

- (IBAction)sliderMoved:(UISlider *)sender {
NSUInteger slot = sender.value;
NSDate *slotDate = [self timeFromSlot:slot];
self.timeLabel.text = [self.formatter stringFromDate:slotDate];
}

#pragma mark - Private methods

/**
Converts a slot integer to a valid time in 30 minute increments

@param slot The slot number

@return An NSDate for the time representing the slot

@warning slot should be between 0 and 47
*/

- (NSDate *)timeFromSlot:(NSUInteger)slot{
if ((slot > 47)) {
return nil;
}

NSDateComponents *components = [NSDateComponents new];
[components setMinute:30 * slot];

return [[NSCalendar currentCalendar] dateFromComponents:components];
}

@end

This is the complete view controller implementation that does what you seem to want. The full project is available here if you don't want to create a test project yourself.

Database cleaning at regular intervals

The answer given by home in the comment is what I wanted - Check this: running periodic task at server side for servlet JSP MVC website There are a number of different ways to implement schedulers.

How to use Column value as Interval to increase date in Oracle SQL?

You can use numToDSInterval():

time + numToDSInterval(adjust, 'second')

It might be cleaner to explicitly convert the string to a number:

time + numToDSInterval(to_number(adjust), 'second')

If adjust always belongs to range 00-59, you can also use to_dsinterval():

time + to_dsinterval('0 00:00:' || adjust)


Related Topics



Leave a reply



Submit