How do I clear an NA flag for a posix value? - r

Sample data in csv format. Save in a file broken_posix.csv
Date
3/10/2012 23:00
3/11/2012 0:00
3/11/2012 1:00
3/11/2012 2:00
3/11/2012 3:00
3/11/2012 4:00
3/11/2012 5:00
3/11/2012 6:00
3/11/2012 7:00
3/11/2012 8:00
3/11/2012 9:00
3/11/2012 10:00
3/11/2012 11:00
3/11/2012 12:00
3/11/2012 13:00
3/11/2012 14:00
3/11/2012 15:00
3/11/2012 16:00
3/11/2012 17:00
3/11/2012 18:00
3/11/2012 19:00
3/11/2012 20:00
3/11/2012 21:00
3/11/2012 22:00
3/11/2012 23:00
3/12/2012 0:00
3/12/2012 1:00
3/12/2012 2:00
3/12/2012 3:00
3/12/2012 4:00
3/12/2012 5:00
3/12/2012 6:00
3/12/2012 7:00
3/12/2012 8:00
3/12/2012 9:00
3/12/2012 10:00
3/12/2012 11:00
So I have this file broken_posix.csv. I can read the file just fine with
a_var <- read.csv("broken_posix.csv")
Then I can convert it to posix using
a_var_posixct = as.POSIXct(strptime( as.character( a_var$Date) , '%m/%d/%Y %H:%M'))
or with
a_var_posixlt = strptime(as.character( a_var$Date) , '%m/%d/%Y %H:%M')
The problem occurs now though because when I use posixct, then I get 4 NA values in my string every year. When I use posixlt I get one NA value on March 11,2012 at 2 (daylight savings time)
You'll see what I mean when you run
which(is.na(a_var_posixct))
which(is.na(a_var_posixlt))
a_var_posixct[4]
a_var_posixlt[4]
The fourth value is always a NA value whenever you apply an operation even though it is clearly a date value for posixlt.
I've tried omitting the value only to end up messing up the rest of the posix string.
I've tried setting the posix string as itself, in an attempt to clear the NA flag, to no effect.
I've even tried setting it as a character value only to lose the hour and minute formatting.
I think that this situation occurs because of daylight savings time. It's very frustrating to deal with because when I try to run other functions on the dates I have to try to avoid the NA values since I can't change them. I could aggregate the data by day, and or just use date objects but that doesn't seem like the right method.

Using a time zone without daylight saving time fixes this kind of problems for me.
a_var_posixlt = strptime(as.character( a_var$Date) , '%m/%d/%Y %H:%M',tz="GMT")

from ?as.POSIXct
Character input is first converted to class "POSIXlt" by strptime: numeric input is first converted to "POSIXct". Any conversion that needs to go between the two date-time classes requires a timezone: conversion from "POSIXlt" to "POSIXct" will validate times in the selected timezone. One issue is what happens at transitions to and from DST, for example in the UK
as.POSIXct(strptime('2011-03-27 01:30:00', '%Y-%m-%d %H:%M:%S'))
as.POSIXct(strptime('2010-10-31 01:30:00', '%Y-%m-%d %H:%M:%S'))
are respectively invalid (the clocks went forward at 1:00 GMT to 2:00 BST) and ambiguous (the clocks went back at 2:00 BST to 1:00 GMT). What happens in such cases is OS-specific: one should expect the first to be NA, but the second could be interpreted as either BST or GMT (and common OSes give both possible values). Note too (see strftime), OS facilities may not format invalid times correctly.
Your 4 NA's will presumably be on the hour when the clocks change twice a year.

I can't reproduce the problem, but here are some things you can try.
The first thing to check is that your data is correct. Copy and paste the dataset from this question, and run it again on your machine. Do you still get the error? If not, you probably have a typo in your dataset. Also try the line suggested by David Robinson.
as.POSIXlt(strptime("3/11/2012 2:00", '%m/%d/%Y %H:%M'))
Does this return NA?
Another source of weird date-related NAs is the locale. Check what yours is using
Sys.getlocale("LC_TIME")
Then change it to a different one. The available locales differ depending upon your OS (it's a mess), but take a look at example(Sys.setlocale).

Related

Lower and upper limit for time in Ionic2

I am trying to make a form where user need to book a time slot between 8:00 AM to 4:00 PM. All the time slots should be in 30 mins slot.
For example - 8:00 AM, 8:30 AM, 9:00 AM, 9:30 AM, 10:00 AM, 10:30 AM, 11:00 AM, 11:30 AM, 12:00 PM, 12:30 PM, 1:00 PM, 1:30 PM, 2:00 PM, 2:30 PM, 3:00 PM, 3:30 PM,4:00 PM.
I need to make sure that user select time between 8:00 AM to 4:00 PM.
How can i achieve this by using ionic2 date time.
This is what i tried but its not working-
<ion-item>
<ion-label>Start Time</ion-label>
<ion-datetime displayFormat="h:mm A" hourValues='8,9,10,11,12,13,14,15,16' minuteValues="0,30" pickerFormat="h mm A" [(ngModel)]="event.timeStarts"></ion-datetime>
</ion-item>
i've try with DateTime component but i think you could do this pickerFormat="H mm". this's not consistent with display format but you could avoid user pick a wrong time, for example 1:30AM as pickerFormat="h mm A" does.

R- Dealing with changes in my time series period

I have data, in hourly resolution, which looks like this:
Time traffic
6/7/2005 7:00 56718587433
6/7/2005 8:00 76456162968
6/7/2005 9:00 82534038485
6/7/2005 10:00 88796995092
...
7/28/2005 10:00 51528036132
7/28/2005 11:00 69610584123
7/28/2005 12:00 76364975533
7/28/2005 13:00 81281257078
The data looks like this:
As one can see, there is an obvious seasonality of 1 day (each cycle), of 1 week (7 cycles) - but in a week there is a patern of [5 days-2 days, 5 days 2 days...].
My question is how can I capture this pattern?
Here is what I've done to capture the daily and the weekly seasonality:
library(forecast)
data<-read.csv("internet-traffic-data-in-bits-fr.csv")
model <- msts(data[,2], seasonal.periods=c(24,168))
model.fit <- tbats(model)
plot(forecast(model.fit))
And this is what I get when I plot the model:
How can I improve it?

Military time to am/pm conversion using moment.js

I have a military time that I am trying to convert to a 12 hr am/pm format as shown below
console.log(moment("13:00", 'HH:mm').format('HH:mm a'));
console.log(moment("15:00", 'HH:mm').format('HH:mm a'));
But I am getting
13:00 pm
15:00 pm
as the output, when it should be 01:00 pm and 03:00 pm. Am I missing something on the code ?
moment docs are pretty clear on formatting - make sure to check them. "H" is 24 hour time. You want "h" instead (or hh for 0 prefixed).
console.log(moment("13:00", 'HH:mm').format('hh:mm a'));

SSIS - Calculate an interval of dates with datetime fields

Let's assume that I have some dates like these:
2014-01-23 14:52 (today)
2014-01-22-15:35
2014-01-21 10:35
2014-01-20 09:45
2014-01-19 17:58
2014-01-18 14:05
2014-01-17 13:22
Now I need to take into account only the 5 previous days to the current day, so for me they will be:
2014-01-22-15:35
2014-01-21 10:35
2014-01-20 09:45
2014-01-19 17:58
2014-01-18 14:05
In SSIS I wrote the next instruction in a conditional split task:
date > DATEADD("DD",-5,GETDATE()) && date < DATEADD("DD",-1,GETDATE())
But the result that I am having depends of the HOUR in which I execute the work flow.
So, for instance, if I execute it TODAY (2014-01-23) at 13:42. I am not going to seeing 2014-01-22-15:35 because it is after a complete day consider the hour (13:42)
and what I need is to see all the data that have a date at any moment from yesterday.
My question is, how can I indicate that I need ALL the dates of the previous days from today at 00:00?. In other words, how can I compute this interval for ALL the hours of the previous 5 days without taking into consideration the hour of execution.
if you are doing this in SQL you can try to cast the dateTime to a date
Declare #today date = getdate()
Declare #now datetime = getdate()
Cast(#now as date) between dateadd("DD",-5,#today) AND dateadd("DD",-1,#today)
let me know if that helps?

Extracting hour from time stamp in R

I would like to extract the hour of the day as numeric only from a column in R where the times are set up like this;
0:00
1:00
10:00
11:00
12:00
13:00
etc.
so I would like to only extract 10,11,12,13,etc...
I cannot find a way to do this in an efficient way.
Try gsub(x = timestamps, pattern = ":\d{2}", replacement = "") - you could also strsplit on the colon, but that means you have to parse the resulting list.

Resources