Date arithmetic in Erlang - datetime

I have a date in this format {Y,M,D}. Is there any good supporting libraries or, tricks I can use to simply, say subtract three months from this date without running into problem with invalid dates, leap years, etc.
My latest similar use is in MySql where you can type:
Select '2011-05-31' - Interval 3 Month;
which yields '2011-02-28'. I am not interested in how to write this library myself, that is what I would like to avoid.

1> calendar:gregorian_days_to_date(calendar:date_to_gregorian_days({2011, 7, 14}) - 90).
{2011,4,15}
http://www.erlang.org/doc/man/calendar.html

Use edate - specifically the shift function.
> edate:shift({2011,5,31}, -3, months).
{2011,2,28}
Under the hood it uses the calendar module, so it correctly deals with all of the corner cases.

I created a GitHub gist with some useful utilities for this sort of thing. Feel free to steal from it if you want.
https://gist.github.com/104903

Related

Why filter ( as.Date(opp_date) == Sys.Date() ) is bringing yesterday's data?

I love using dplyr; I use it for everything. But, the problem I'm experimenting today is the following:
I'm trying to simply filter all rows fromm my opps table where opp_date is from today. So, when I use filter(opps, as.Date(opp_date) == Sys.Date()) it's bringing today's data but also yesterday's too, from 19:00:00 onwards.
To clarify any possible problem:
opp_date field is POSIXct class
Sys.Date() returns correctly my current date and time (just to check, Sys.time() brings the correct time and date: "2017-07-21 10:06:04 COT")
Any idea here? Thanks to the community for all the great inputs :)
The issue must be due to different time-zones; by default, R uses your system's local time-zone.
Try to explicitly set the environment variable as follows:
Sys.setenv(TZ='UTC')
Cannot add a comment due to low repputation, so posting this as an answer.
As I got to know recently, R dates are a formatting nightmare, especially through the base functions. Checkout lubridate package. You may want to convert your date column using dmy_hms function. It's easy and vectors are supported by default. Try it and let me know if the problem persists.
And please always try to provide sample data. Otherwise people cannot reproduce your problems.

OSB Xquery :Date calculation

All,
I want to do a date subtract operation in Xquery, OSB 12C.Basically, have to check if an input date is < 6 months from the system date.
i.e- how to do in xquery : (SystemDate - inputDate) < 6 months
Have went through:
https://docs.oracle.com/cd/E13214_01/wli/docs92/xref/xqdtopref.html but not able to compile any of the function listed there. Have also added the namespace in xquery. Do I need to add any library.
Any pointer is highly appreciated.
OSB has some extensions for dates, which you can read about here.
But, you might not even need that for simple stuff like you mentioned.
All,
Issue got resolved.Please find the link for the logic:http://developer.marklogic.com/pipermail/general/2015-February/016462.html
Subtracting dates give you dayTimeDuration in XQuery.
Following code is a simple and accurate way to check time intervals.
days-from-duration(fn:current-date() - $inputDate) < 180
180 is number of days you need to validate.

Alternative to sqlite OR a better way to handle date / time fields in sqlite

My data tends to be medium to large but never qualifies as "BIG" data. The data is almost always complexly relational. For the purposes I'm talking about here, 10-50 tables with a total size of 1-10 GB. Nothing more. When I deal with data bigger than this, I'll stick it into Postgres or SQL Server.
Overall, I like SQLite, but the data I work with has lots and lots of date / datetime fields and dealing with date fields in SQLite makes my head hurt and when I move data back and forth between R and SQLite, my dates often get mangled.
I am either looking for a file-based alternative to SQLite that is easy to work with from R.
OR
Better techniques/packages for moving data in/out of SQLite and R without mangling the dates. My goal is to stop mangling my dates. For example, when I use dbWriteTable from the RSQLite package my dates are usually messed up in a way that makes them impossible to work with.
My primary workstation is running Ubuntu but I work in an office dominated by Windows. If suggesting an alternative to SQLite, +++ for an alternative that works on both platforms (or more).
Use epoch times and dates (days from origin, seconds from origin). The conversion using epochs into R POSIXct or Date is fast (strings are very slow).
Edit: Another alternative, after re-reading and considering the size of your data:
You could simply save the tables directly in R format, perhaps with a small piece of extra metadata describing the key relationships between tables. You would have to create your own conventions and all, but it's definitely smoother (no impedance mismatches).
Also, I'm personally very partial to the package data.table. It's fast and has a syntax which is pure R but has a nice mapping onto SQL concepts. E.g. in dt[i, j, by=list(...)], i corresponds to "where", j correspond to "select", and by to "group by" and there are facilities for joins as well, although I wrote infix wrappers around those so it was easier to remember.
I typically do my data processing work exclusively in R (after an initial pull from SQLITE), and I find data.table more faster and practical than massive SQLDF queries.
http://datatable.r-forge.r-project.org/
sqlite wants to read the data in the standard format "YYYY-MM-DD HH:MM:SS" (you can omit the time part if you don't need it)---I don't know of a way to read arbitrary date strings. This results in a normalized date being stored.
On output, you want to format the date using sqlite functions to whatever your other software needs---check the options of strftime().
For instance, Octave likes the day number since year 0, so if I have a table mydata with column "date", I'd do
select julianday(mydate)-1721059.666667 from mydata
The magic number is julianday("0000-01-01T00:00:00-04:00") and compensates for the fact that julianday starts in year 4017BC or something like that, whereas Octave counts from year 0.

get day of the week from yyyy-mm-dd

I would like to convert a yyyy-mm-dd to something like this:
"Saturday, 2 October 2009"
I would like also to have the option to modify the language both day of the week and month (make it customizable)
thanks in advance ;)
Since you never told us the language, here's a discussion on how to do it in COBOL.
Assuming .NET (from the datetime tag):
DateTime.Parse("2009-10-02").ToString("D", CultureInfo.CreateSpecificCulture("en"));
In C, you would use a combination of the localtime() and strftime() functions. They should handle internationalization more or less automatically, if your application is set up for it.
Convert the date into an integer, representing the days since a specific date, then add a number and apply the modulus operator with the operand 7. That will give you a number 0-6 that represents the day of week.
However, most languages have this functionality built-in.

Operating with time intervals like 08:00-08:15

I would like to import a time-series where the first field indicates a period:
08:00-08:15
08:15-08:30
08:30-08:45
Does R have any features to do this neatly?
Thanks!
Update:
The most promising solution I found, as suggested by Godeke was the cron package and using substring() to extract the start of the interval.
I'm still working on related issues, so I'll update with the solution when I get there.
CRAN shows a package that is actively updated called "chron" that handles dates. You might want to check that and some of the other modules found here: http://cran.r-project.org/web/views/TimeSeries.html
xts and zoo handle irregular time series data on top of that. I'm not familiar with these packages, but a quick look over indicates you should be able to use them fairly easily by splitting on the hyphen and loading into the structures they provide.
So you're given a character vector like c("08:00-08:15",08:15-08:30) and you want to convert to an internal R data type for consistency? Check out the help files for POSIXt and strftime.
How about a function like this:
importTimes <- function(t){
t <- strsplit(t,"-")
return(lapply(t,strptime,format="%H:%M:%S"))
}
This will take a character vector like you described, and return a list of the same length, each element of which is a POSIXt 2-vector giving the start and end times (on today's date). If you want you could add a paste("1970-01-01",x) somewhere inside the function to standardize the date you're looking at if it's an issue.
Does that help at all?

Resources