Are there any existing implementations in R to parse ISO8601 strings into POSIXt objects? The ISO8601 spec allows date/times to be printed in a variety of (non-overlapping) formats, so one probably needs to do some regular expression magic to detect the format and feed that to strptime.
Doing this properly might actually be quite challenging, however something that detects the most common formats would already be very helpful. I can hardly imagine I am the first one to run into this, but I am having a hard time finding good implementations.
Strictly speaking, you can't. I don't need to know anything about r or cran (or even what they are) to tell you that, because I know ISO 8601 well enough to know that just knowing something is ISO 8601 is not enough to unambiguously know that what is meant by it, especially in the shorter forms.
Find out what profile of ISO 8601 the other party is using. If they don't know what you're talking about, then you will be doing them a favour when you point out what I just said in the paragraph above. As I wrote once elsewhere,
Unfortunately many people think of a particular profile they are familiar with when they hear “ISO 8601”, other people know that using 8601 is a Good Thing but are not familiar with the details of implementation. Hence a spec or requirements document might mention 8601 but not be more explicit than that. In such cases it’s important to seek clarification rather than assume that the format you think of as “ISO 8601” is the correct one to use.
So, tell them "'ISO 8601' is not specific enough, I need to know exactly what you are doing, what your limits on precision are." (And possibly what your policy on dates prior to 1582 and perhaps again prior to 0001 are, your policy on leap-seconds, and a few other things left open but the standard)
Then whatever you're dealing with should be easy enough: Aside from this point of ambiguity, it is a pretty straight-forward standard. It should just be thought of as a standard about defining date formats, more than one that defines a date format.
See .parseISO8601 in the xts package for one implementation. I doubt this will work "out of the box", but it should give you a good idea how to implement your specific needs.
This looks promising: http://cran.r-project.org/web/packages/parsedate
parsedate: Recognize and Parse Dates in Various Formats, Including All
ISO 8601 Formats
Parse dates automatically, without the need of specifying a format.
Currently it includes the git date parser. It can also recognize and
parse all ISO 8601 formats.
t <- strptime("2013-08-20T14:56:37", "%FT%T")
worked well enough for me for most cases. It already fails on fractions of seconds though and does not include solutions to all the problems Jon Hanna mentioned. (And which make working with time data types so unbelievably difficult.)
Related
So quite a few programming languages (eg c, c#, php, etc.) have adopted a similar way of specifying datetime formats using letters:, eg:
MM/dd/yyyy HH:mm:ss
my question is in two parts:
1.Is this specification even the same between the languages I mention
2.Is there any standardisation of these format definitions (e.g under an ISO) or are these just language specific implementations that sometimes overlap (perhaps because the examples I gave are all c based)?
Abstract question. Is there "data decorator" bundle for Symfony2? Or how would you solve elegantly data localization/formatting problems?
For example:
float - decimals formatting, monetary data needs monetary formatting
integers - formatting - 1'000'000 vs 1.000.000
dates, times
etc
I know that Symfony1 had some kind decorating going on in templates which would suit this exactly. It wasn't perfect, though and arised other problems.
Anybody knows, is there something? Or what would be the "correct" way for solving this problem?
Your question as it stands is too vague and answers can be considered primarily opinion-based, but you're aware that the native PHP functions do everything that you're asking for, right?
Float, integer formatting: number_format
Date/Time formatting: DateTime class and its subsequent format functions
Twig even uses the equivalent functions to achieve exactly what you're asking for: number_format and the date filters.
This would be the correct standard - and although I have literally no experience with Symfony1, I haven't had any issue deploying a dozen Symfony2 projects using just these two functions to achieve the data decorating that you desire.
I'm trying to retrieve the date only from a java.util.date. I know I can set the time to 0 but I'm searching for a cleaner, more concise way if possible. Does this exist?
Thanks!
Krt_Malta
The cleaner way is to use Joda Time to start with, where there are separate concepts of LocalDate, LocalTime, LocalDateTime, DateTime, Instant etc.
You can translate between java.util.Date and the Joda types, but I would suggest you stick to using Joda for as much of the code as you can, and only translate when you really need to.
Sorry if you'd hoped to avoid an extra library - but Joda is so much better than the built-in API, it's well worth investing in the effort IMO. Note that within the Java types you'll need to determine which time zone you're interested in - java.util.Date defines an instant in time, which may occur in as many as three different dates around the world depending on your time zone.
Another option is the truncate method in the DateUtils library. You put in the date and the field up to which you are interested (in this case CALENDAR.DAY_OF_MONTH). The rest will be set to 0. Effectively:
DateUtils.truncate(Calendar.getInstance(), Calendar.DAY_OF_MONTH).getTime()
did the job.
More info about the method is at: http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/time/DateUtils.html#truncate%28java.lang.Object,%20int%29
I'm a bit of a polyglot when it comes to programming languages, and most of the languages I use have Error/Exception handling of some sort.
In most languages there's a default implementation of error ID's with their associated messages, but I've never found a list of production codes to base my own error codes off of.
Does such a thing exist?
If not would it be useful, or just noise that most programmers ignore?
The closest thing I can think of is POSIX error constants (though their numeric values are not standardized.)
Short answer - no, it doesn't exist. Every OS, platform and piece of software pretty much has its own error IDs. These are not synchronized or based on any standard set.
I would say that apart from the common errors, this would indeed just be noise, and even with the common one, one one need to standardize them and ensure they are used universally.
I'm writing some children's Math Education software for a class.
I'm going to try and present problems to students of varying skill level with randomly generated math problems of different types in fun ways.
One of the frustrations of using computer based math software is its rigidity. If anyone has taken an online Math class, you'll know all about the frustration of taking an online quiz and having your correct answer thrown out because your problem isn't exactly formatted in their form or some weird spacing issue.
So, originally I thought, "I know! I'll use an expression parser on the answer box so I'll be able to evaluate anything they enter and even if it isn't in the same form I'll be able to check if it is the same answer." So I fire up my IDE and start implementing the Shunting Yard Algorithm.
This would solve the problem of it not taking fractions in the smallest form and other issues.
However, It then hit me that a tricky student would simply be able to enter most of the problems into the answer box and my expression parser would dutifully parse and evaluate it to the correct answer!
So, should I not be using an expression parser in this instance? Do I really have to generate a single form of the answer and do a string comparison?
One possible solution is to note how many steps your expression evaluator takes to evaluate the problem's original expression, and to compare this to the optimal answer. If there's too much difference, then the problem hasn't been reduced enough and you can suggest that the student keep going.
Don't be surprised if students come up with better answers than your own definition of "optimal", though! I was a TA/grader for several classes, and the brightest students routinely had answers on their problem sets that were superior to the ones provided by the professor.
For simple problems where you're looking for an exact answer, then removing whitespace and doing a string compare is reasonable.
For more advanced problems, you might do the Shunting Yard Algorithm (or similar) but perhaps parametrize it so you could turn on/off reductions to guard against the tricky student. You'll notice that "simple" answers can still use the parser, but you would disable all reductions.
For example, on a division question, you'd disable the "/" reduction.
This is a great question.
If you are writing an expression system and an evaluation/transformation/equivalence engine (isn't there one available somewhere? I am almost 100% sure that there is an open source one somewhere), then it's more of an education/algebra problem: is the student's answer algebraically closer to the original expression or to the expected expression.
I'm not sure how to answer that, but just an idea (not necessarily practical): perhaps your evaluation engine can count transformation steps to equivalence. If the answer takes less steps to the expected than it did to the original, it might be ok. If it's too close to the original, it's not.
You could use an expression parser, but apply restrictions on the complexity of the expressions permitted in the answer.
For example, if the goal is to reduce (4/5)*(1/2) and you want to allow either (2/5) or (4/10), then you could restrict the set of allowable answers to expressions whose trees take the form (x/y) and which also evaluate to the correct number. Perhaps you would also allow "0.4", i.e. expressions of the form (x) which evaluate to the correct number.
This is exactly what you would (implicitly) be doing if you graded the problem manually -- you would be looking for an answer that is correct but which also falls into an acceptable class.
The usual way of doing this in mathematics assessment software is to allow the question setter to specify expressions/strings that are not allowed in a correct answer.
If you happen to be interested in existing software, there's the open-source Stack http://www.stack.bham.ac.uk/ (or various commercial options such as MapleTA). I suspect most of the problems that you'll come across have also been encountered by Stack so even if you don't want to use it, it might be educational to look at how it approaches things.