I'm trying to format the current datetime in XSLT with an explicit UTC offset (and no other literals and no millis), like: 20140710163601+0200.
However, this <x:value-of select="format-dateTime(current-dateTime(), '[Y0001][M01][D01][H01][m01][s01][z]')"/> gives me this: 20140710164200GMT+02:00. Note that I do not want the GMT part.
If there is no offset, I get 20140710144546.
Is there any way to force an explicit offset and set it to the format I want? Obviously, I could do some string manipulation, but maybe there's a library function I'm overlooking. And then there's the no-timezone result I have to force the format for.
Note that it's no problem for me to build a function around this, but rather I'd use something built in or more elegant.
The XSLT 2.0 spec of format-dateTime() is a bit muddled about timezones, so it may depend on which processor you are using. In 3.0 it's specified that you get the format you want with [Z0000]. Recent versions of Saxon implement the function according to the 3.0 spec, but other processors may well do something different. You might be better off using timezone-from-dateTime() to extract the timezone, and then formatting it using format-number().
Try using (capital) Z instead of (lower-case) z.
Related
I am just printing the ISO datetime with timezone as per the below documentation
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a003169814.htm
This is my code
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-mm-dd'T'hh:mm:ss.nnnnnn+|-hh:mm");
df.setTimeZone(tz);
dateTimeWithTimeZone = df.format(new Date());
However i am getting this exception
Illegal pattern character 'n'
I cant use this format directly in Java ?
java.time
dateTimeWithTimeZone = Instant.now().toString();
System.out.println(dateTimeWithTimeZone);
When I ran this snippet just now, I got this output:
2019-03-18T22:28:13.549319Z
It’s not clear from the page you link to, but it’s an ISO 8601 string in UTC, so should be all that you need. I am taking advantage of the fact that the classes of java.time produce ISO 8601 output from their toString methods. The linked page does show the format with hyphens, T and colons (2008-09-15T15:53:00+05:00), it shows another example with decimals on the seconds (15:53:00.322348) and a third one with Z meaning UTC (20080915T155300Z), so I would expect that the combination of all three of these would be OK too.
The format you used in the quesiton seems to try to get the offset as +00:00 rather than Z. If this is a requirement, it’s only a little bit more complicated. We are using an explicit formatter to control the variations within ISO 8601:
DateTimeFormatter iso8601Formatter
= DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSSSSxxx");
dateTimeWithTimeZone = OffsetDateTime.now(ZoneOffset.UTC).format(iso8601Formatter);
System.out.println(dateTimeWithTimeZone);
2019-03-18T22:28:13.729711+00:00
What went wrong in your code?
You tried to use the formatting symbols from your source with SimpleDateFormat. First, you should never, and especially not in Java 8 or later, want to use SimpleDateFormat. That class is notoriously troublesome and long outdated. Second, some of its format pattern letters agree with the symbols from your source, some of them don’t, so you cannot just use the symvol string from there. Instead you need to read the documentation and find the correct format pattern letters to use for year, month, etc. And be aware that they are case sensitive: MM and mm are different.
Link
Oracle Tutorial: Date Time
explaining how to use java.time.
Like this:
const RFC_3339 = 'YYYY-MM-DDTHH:mm:ss';
moment.utc().format(RFC_3339);
I need the timestamp to have a 'Z' at the end. Is there a better way than just +'Z'?
It should match the python code on the backend:
RFC_3339_FMT = "%Y-%m-%dT%H:%M:%SZ"
You can simply use format().
As the docs says:
As of version 1.5.0, calling moment#format without a format will default to moment.defaultFormat. Out of the box, moment.defaultFormat is the ISO8601 format YYYY-MM-DDTHH:mm:ssZ.
As of version 2.13.0, when in UTC mode, the default format will return Z as the offset, instead of +00:00
I think that the toISOString() function does what you need, right?
See the documentation here.
Will it be possible to suppress messages such as "Using date format..." when using a function like?
> ymd(vec)
Using date format %Y%m%d
Whilst these are good to see when you are casting a vector, it can be annoying in some circumstances.
Looking at the ymd code, it callse parse_date, which gives those annoying messages via the command message.
Looking at ?message, there is a corresponding suppressMessages:
suppressMessages(ymd(x))
(Note - other similar functions are suppressWarnings, suppressPackageStartupMessages, and capture.output, all of which I have had to use in the past to stop unexpected bits of text turning up (I was outputting some bits to an HTML file and these didn't want these to be in it)).
Manny, suppressMessages() is the only way to go at the moment. But I like your idea of an argument. I've put it on the todo list for lubridate. You could also use strptime() once you have the format for a vector of date-times.
As I am trying to plot a few financial time series in Mathematica, I just ran into a problem illustrated in the figure below :
It seems the data are no longer dealt with after Year 2000
Is there a way to fix that ?
What would be the best format to export time series from Bloomberg or Excel to use them in Mathematic (Using version 8).
I do know about the FinancialData function. However, not knowing the exact symbols, it makes it extremely difficult to use Mathematica directly for this.
Why not to use WolframAlpha[...] function - it imports native to Mathematica format and goes up to current dates:
timeseries = WolframAlpha["msft close Jan 1, 2010 to Jan 21 2011",
{{"DateRangeSpecified:Close:FinancialData", 1}, "TimeSeriesData"}];
DateListPlot[timeseries]
That was just an example of input. I am not sure what kind of data you need exactly, but you can get a lot of them via WolframAlpha function. Read this:
1) WolframAlpha
2) Data Formats in Wolfram|Alpha
Use the DateFunction option to tell DateListPlot how to convert dates:
DateFunction -> (DateList[{#, {"MonthNameShort", "YearShort"}}] &)
(The parentheses are important.)
Here's a function to convert those date strings to a format Mathematica can handle better:
dateConv = With[{s = StringSplit[#, "-"]}, {DateList[{s[[2]], "YearShort"}][[1]],
DateList[s[[1]]][[2]]}] &
You can try
DateListPlot[data, DateFunction -> dateConv]
EDIT: Originally I tried DateList[{"Nov-11", {"MonthNameShort", "YearShort"}}] but this tells me String "Nov-
11" cannot be interpreted as a date in format {"MonthNameShort",
"YearShort"}.. Perhaps a bug?
I'm writing a simple web scraper in Common Lisp (SBCL) as a learning exercise, & would like to sort by date. To do this, I'll need to parse dates in the format "MM/DD/YYYY" into universal time.
I could simply tokenise the string & pass the bits into encode-universal-time, but I figure that there must be a built-in function (or popular third-party package) for date parsing. I'd greatly appreciate someone recommending one :-)
This answer is very late but the local-time library is featureful and widely used. It is based on the article The long painful history of time.
It supports :
Time and date arithmetic
ISO 8601 timestring formatted output and parsing
Reader macros to embed timestrings directly in code
Timezone handling (will read unix tzfile format)
Conversion between universal and unix time epochs
Julian date calculation
See the net-telent-date and simple-date-time libraries for Common Lisp. The former has a parse-time function you can use (see parse-time.lisp). Both are included in the QuickLisp library collection.
You could try net-telent-date, which has PARSE-TIME which I think will do what you want.
It's now 2022, and net-telent-date is on github and is also deprecated. Better to find something else.
Many implementations have a UNIX interface and, in same cases, this includes the strptime function.
Antik handles dates and times and includes date/time parsers. The result is a "timepoint" which by default is UTC (CL's "universal-time" is something different, but it can be converted to that).
I use local-time and cl-date-time-parser:
edit: and chronicity for parsing natural language dates and times.
(local-time:parse-timestring "2019-11-13T18:09:06.313650+01:00") ;; OK
(local-time:parse-timestring "2019-11-13") ;;OK
This fails with local-time by default:
(local-time:parse-timestring "2019/11/13")
but it works with Chronicity:
(chronicity:parse "2019/11/13")
#2019-11-13T00:00:00.000000+01:00
and we can set the date separator of local-time to "/":
(local-time:parse-timestring "2019/11/13" :date-separator #\/) ;; OK
There is also the time and datetime separators.
Now a format like ""Wed Nov 13 18:13:15 2019" will fail. We'll use the
cl-date-time-parser library:
(cl-date-time-parser:parse-date-time "Wed Nov 13 18:13:15 2019")
;; 3782657595
;; 0
It returns the universal time which, in turn, we can ingest with the
local-time library:
(local-time:universal-to-timestamp *)
;; #2019-11-13T19:13:15.000000+01:00