I'm writing a Fitnesse test for a web application. One of the items to test is a drop-down box, whose value is determined by the current date, in DD/MM/YYYY format.
I'd thought that using the !today variable in the Fitnesse suite might be a useful way of setting a variable, but I've run into the problem that Fitnesse expresses the date as (for example) 11 Mar, 2011, where I need 11/03/2011. I can get the date in numberic format using the -xml modifier, but I'm still left with a pretty huge string like 2011-03-11T05:51:22.
Is there a way of getting substrings of this, and then piping those into page variables, or am I barking up entirely the wrong tree here?
Thanks!
!today (MM/dd/yyyy) produces 09/17/2012. You can use any format codes you like. It uses the SimpleDateFormat class.
Well, it turns out not entirely the wrong tree :-)
For reference, the !today function has a few other methods, and you can use them to gather individual sections of the date as necessary:
!today (dd) - gives the day of the month, in numeric form
!today (MM) - gives the month of the year, in numeric form
!today (yyyy) - gives the year, in numeric form
There are a few others, but all I ended up using were these. Combine them as necessary, and Robert is your mother's brother, as it were...
This will get you the date you require in Test Cases written in Fitnesse Wiki
${=!today (ddMMyyyy)=}
The ! (Exclamation mark) is interpreted literally, so symbols like !today are not expanded. You can use a plain table:
|class name|
|!today (MM/dd/yyyy)|
Related
If you're used Cocoa for a while you're probably familiar with NSDateFormatter
and NSNumberFormatter. They're handy for creating formatted display strings from dates and numbers, or for converting date or number strings into numeric values, while supporting different languages and locales.
A few weeks ago I stumbled on NSDateComponentsFormatter, which lets you create formatted time intervals like "4 hours, 37 minutes and 17 seconds." Pretty cool.
There's also the related NSDateIntervalFormatter, which creates strings by comparing 2 dates.
Then there are some REALLY obscure NSFormatter subclasses:
NSMassFormatter
NSByteCountFormatter
NSLengthFormatter
NSEnergyFormatter
NSPersonNameComponentsFormatter
EDIT:
From the comments, I've aded NSPersonNameComponentsFormatter.
Searching on "NS*Formatter" in the Xcode help system reveals most of these, but not all. (It looks like the help text has to be indexed correctly in order for searching to work, which is annoying.)
That brings the total I have been able to find to
NSDateIntervalFormatter -Difference between 2 dates
NSDateComponentsFormatter -NSDateComponents to/from string
NSDateFormatter -Formats NSDates as strings
NSNumberFormatter -Formats numbers as strings
NSMassFormatter -Formats mass quantity as strings
NSByteCountFormatter -Formats byte counts in K, MB, GB, etc.
NSLengthFormatter -Formats length values
NSEnergyFormatter -Displays energy qualities in Joules or Calories
NSPersonNameComponentsFormatter - displays localized formatted names
Annoyingly, it looks like many of these formatters don't have a locale property, so it's not very easy to use them to create formatted strings in languages/locales other than the system's default locale. If I'm missing something, please tell me.
Does anybody else know of other formatters I'm missing? These are pretty obscure, but could save you a lot of time if you were to need them.
EDIT #2:
Question part 2: Is there a way to get output from the formatters that lack a locale property in locale's other than the system default locale? It seems silly that they don't ALL have and honor a locale property. It's pretty common to need to generate output for languages/locales other than the current locale.
There's no need to search. The NSFormatter documentation lists all of its subclasses. Look at the top of the page, in the "inherits from" block.
Note that this information is not available in the Xcode 7.3 doc reader. It's only available in the online docs (or by using the excellent Dash reader).
I went into
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks
and then did
for header in **/*.h; do ack -o 'NSFormatter' "$header"; done
which gave me some interesting ones:
NSPersonNameComponentsFormatter
CNContactFormatter
CNPostalAddressFormatter
DRMSFFormatter
MKDistanceFormatter
Doing the same for iPhoneOS.sdk didn't turn up any new NSFormatter subclasses.
What is the standard way to present the ambiguous extra hour when winter time begins?
So far i used localized time formats to display and parse dates and times. E.g. 1. January 2014, 15:27.
I'm using location based time zones like "Europe/Berlin".
And i can't just change to plain GMT offsets because i do need to perform calculations on the dates. Otherwise i would get the wrong absolute time when moving across DST change dates.
All this works fine except for the one hour at the end of DST (e.g. October 26th 2014, 2am-3am) which occurs twice. I need to present it in a way that i can later parse again.
Is there a stadardized format? Do i just add a custom symbol? Do i use the GMT offset additionally to the geographic time zone? And how do i know when to use this special format - because i don't want to print it all the time, since it's redundant most of the year.
The answer by Matt Johnson is correct and insightful.
Java 8 & java.time.*
Let me add another to his list, from the new java.time.* classes bundled with Java 8 and defined by JSR 310. These new classes are inspired by Joda-Time but are re-architected.
The default used by java.time.ZonedDateTime is one concatenated string using square brackets around the time zone name and no spaces.
2014-10-26T13:49:48.278+01:00[Europe/Berlin]
#MattJohnson Feel free to merge this answer's content with yours if you wish.
There isn't a standard that combines everything. The closest you can get is with two fields. One which would be an ISO8601/RFC3339 Date+Time+Offset, and another which would be the IANA/Olson time zone.
Depending on your platform, you may have a single object that represents both, such as a DateTime in Joda-Time or a ZonedDateTime in Noda Time. But there is no standardized representation of this as a string.
Here are some that I have seen though:
Two completely separate strings:
"2014-10-26T02:00:00+01:00"
"Europe/Berlin"
One concatenated string, space separated:
"2014-10-26T02:00:00+01:00 Europe/Berlin"
One concatenated string with a space and using parentheses:
"2014-10-26T02:00:00+01:00 (Europe/Berlin)"
One concatenated string without any space, but with square brackets: (thanks Basil)
"2014-10-26T02:00:00+01:00[Europe/Berlin]"
As JSON, with some predetermined field names:
{
value: "2014-10-26T02:00:00+01:00",
zone: "Europe/Berlin"
}
As XML, with some predetermined attribute names:
<TimeStamp Value="2014-10-26T02:00:00+01:00" Zone="Europe/Berlin" />
As XML, with some predetermined element names:
<TimeStamp>
<Value>2014-10-26T02:00:00+01:00</Value>
<Zone>Europe/Berlin</Zone/>
</TimeStamp>
Any of these would be acceptable. Pick the one that fits your situation, or adapt to something similar.
Regarding your question:
... how do i know when to use this special format ...
When you're recording an event that has already passed and cannot be changed, then you do not need to store the time zone. The date+time+offset value alone is sufficient. Otherwise, you need both.
In my application I am reading from one database and writing to a second. The app is quick and dirty so I am reading / writing using AsString on both the FieldByName and ParamByName of the queries.
This works for all my use cases apart from where the data type is Date or DateTime
As far as I can tell FieldByName.AsString uses the system ShortDateTime format to return dates as (in my case) dd/mm/yyyy. The database expects the date to be written in as yyyy-mm-dd
According to Delphi Basics I should be able to set ShortDateFormat to what I need, but it appears that in XE5 this is no longer the case (correct?)
Further digging on here returns these two questions that use TFormatSettings to override the local settings. However, both of these use the resulting FormatSettings in StrToDate and FormatDateTime directly.
So two questions
1) Can I tell my application to override the System ShortDateFormat?
2) If so, How (I have a Plan B if not)?
The use of a global variable for the date and time formats was a mistake committed long ago by the original RTL designers. Functions that rely on the global format settings, like the single parameter StrToDate are retained for backwards compatibility, but you should not be using them.
For conversions between date/time and string you should:
Initialise a TFormatSettings instance with your date format.
Call the two parameter StrToDate, passing your TFormatSettings to convert from a string to a date.
Call FormatDateTime overload that accepts a TFormatSettings when converting in the other direction.
Now, to the main thrust of your question. You should not be using strings at all for your dates and times in the scenario you describe. Use AsDateTime rather than AsString. If you happen to have a database column that does store a date/time as a string, then you'll should use the TFormatSettings based conversion functions to work around that design fault.
If you are absolutely dead set on doing this all with strings, and I cannot persuade you otherwise, then you need to use FormatSettings.ShortDateFormat from SysUtils to control your short date formatting.
My variables in Stata are of the form:
First Name: Allen
Last Name: Von Schmidt
Birth Year: 1965
County: Cape May
State: New Jersey
First Name: Lee Roy
Last Name: McBride
Birth Year: 1967
County: Cook
State: Illinois
I would like to outsheet them to create quote and comma separated rows in a .txt as:
"Allen,"Von Schmidt","1965","Cape May","New Jersey"
"Lee Roy","McBride","1967","Cook","Illinois"
How can I use outsheet (or another command) to do this? Do I need to make the numerics into strings first? Do I need to add a commas to each variable first?
I have tried the following:
outsheet first last birth_year county state using FileName.txt, nolabel delim(",")
This seems to work ok except that it does not put the numeric variables inside "".
I don't understand why you want this, but Stata's practice here as elsewhere is that only strings are placed in double quotes. So, to output numeric variables as if they were strings you do need to convert them first to string variables. The tostring command is designed for this.
But this is an awkward thing to do, and on the whole a bad idea.
First, and easier: if you use tostring you change your data, and numeric operations become impossible on the new string variables. That is relatively easy to work around. Just make sure you save your data first before using tostring and then read it back in again after exporting the data. Or use preserve followed by restore.
Second, and more problematic: you need to worry about loss of detail for any numeric variables that are not integer. tostring does have options that help here, but there are no guarantees of keeping every bit unless you get into nightmare territory of exporting hexadecimal. That's true of outsheet any way, but a warning should do no harm.
I am aware of the history of tostring, as its original author. I'll put on record that although it is a solution for what you appear to want to do, there are pitfalls as above and I don't recommend this way of working.
It would be better to explain why you think you need to do this. outsheet's export of numerics and strings seems to have worked well for export to other software, not least spreadsheets, over many uses.
P.S. as emphasised elsewhere, Stata does not regard " " as separators. They are delimiters for strings, but not separators for fields (or words in Stata's sense).
I am trying to capture a conditional of years in RegEx. Basically, if they implement just a two digit year, I want to make it a four digit year. So if they put :
1/2/08
I want to make it :
1/2/2008
Any ideas?
One [pretty nasty] way using regex:
"1/2/08".sub! /\/(\d{2})$/, '/20\1'
Wouldn't it be better to just parse the string into a date object, though? Then you can treat it as a date properly! :)
You could split on '/' and if the last component has a length of two you prepend 20 and then assemble the date again.
You could split the string up using
(.*/)(..)$
and then substitute with something like
$120$2
to put the string back together (tested with http://www.regexplanet.com/simple/).
You might need to think about what you want to happen for dates in the 20th century - this approach will recognise 21/01/98 as 21/01/2098 which might not be what you want... It might be better to parse the string out properly rather than just blindly regex it!