Converting server date with Z format to local nsdate - ios

I need to convert this server date (Given from kinvey request) into local timezone.
I'm using the following code:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-ddTHH:mm:ss.sTZD"
print(dateFormatter.dateFromString(newValue))
The date format is this:
ect = "2016-08-28T16:30:06.553Z" or
lmt = "2016-08-28T16:30:06.553Z"
When I print the date it is nil, do you know what I'm doing wrong ?. I think it could be the end of the dateFormat

If your app can target only iOS7+, you can use format symbols described in:
Fixed Formats (in Data Formatting Guide)
Unicode Technical Standard #35 version tr35-31
second | S | 1..n | 3456 | Fractional Second - truncates (like other
time fields) to the count of letters. (example shows display using
pattern SSSS for seconds value 12.34567)
zone | X | 1 | -08,+0530,Z | The
ISO8601 basic format with hours field and optional minutes field. The
ISO8601 UTC indicator "Z" is used when local time offset is 0. (The
same as x, plus "Z".)
So, to parse fractional second, use uppercase 'S',
and 'X' for timezone including "Z" as UTC.
Try this:
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSX"
(I escaped 'T' as it may be used as another time formatting symbol in the future.)
PS. Though I couldn't have found a thread describing the date format which interprets "Z" as UTC+0000, ignoring or removing it may not be a bad solution, if some conditions met. Please find your best solution.

Related

Cannot parse string to time with timezone offset included RFC3339 with seemingly contradictory errors

I am using Golang and time.Time to Parse a given string into a time object.
Using RFC3339 and time.Parse here is an example of my code:
t, err := time.Parse(time.RFC3339, "2020-08-08T00:22:44Z07:00")
if err != nil {
return nil, err
}
I get the follow errors.
When I include timezone offset I am getting:
ERRO[0002] parsing time "2020-08-08T00:22:44Z07:00": extra text: 07:00
When I don't include the timezone offset I am getting:
ERRO[0002] parsing time "2020-08-08T00:15:36" as "2006-01-02T15:04:05Z07:00": cannot parse "" as "Z07:00"
How do I avoid this issue when parsing time into a structured object?
The presence of the character Z in the Go time.RFC3339 constant "2006-01-02T15:04:05Z07:00" does not mean that a date conforming to the pattern is supposed to include a Z followed by the time zone offset.
In fact, a date with Z followed by anything else is not a valid RFC3339 date. Hence, your first error extra text: 07:00
The Z stands for "Zulu Time", i.e. UTC time zone. From the RFC3339 specs:
Z A suffix which, when applied to a time, denotes a UTC
offset of 00:00; often spoken "Zulu" from the ICAO
phonetic alphabet representation of the letter "Z".
So the Z alone is already providing the time zone information, that is, UTC.
As #Flimzy noted in the comments, 2020-08-08T00:22:44Z would be a valid RFC3339 date.
t, err := time.Parse(time.RFC3339, "2020-08-08T00:22:44Z")
if err != nil {
panic(err)
}
fmt.Println(t) // 2020-08-08 00:22:44 +0000 UTC
Now, if you read the RFC3339 standard further, you see this definition:
time-zone = "Z" / time-numoffset
time-numoffset = ("+" / "-") time-hour [[":"] time-minute]
Which means that the time zone part of the date is either a Z or the offset. Clearly, since the Z already represents the offset 00:00, you can't have one more +/-HH:mm offset in the same date string.
But this also means that Z or the +/-HH:mm must be present. So if you remove both of them, you get your second error: cannot parse "" as "Z07:00"
The parser is attempting to read the "2020-08-08T00:15:36" string as RFC3339 so it expects either a Z or an offset after the seconds (or milliseconds, if any).
In conclusion, the Z07:00 in the Go time.RFC3339 pattern is just a representation of the fact that the date string is supposed to include a time zone. A valid RFC3339 date string must include either Z or the offset.

Swift- reformatting time after reading from JSON

I am trying to reformat the time that I am reading off from a String from a JSON API. It is reading from the JSON fine because it gives me the result- the String "2020-05-12T00:00:00:00.000 How can I convert this string it is giving in to an NSDate because I have looked at previous questions and the solution they are giving me is giving me the error that leads to the string having a value of nil
Try using following as format - "yyyy-MM-dd'T'HH:mm:ss:SS.SSS"
let dateString = "2020-05-12T00:00:00:00.000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss:SS.SSS"
let date = dateFormatter.date(from: dateString)!
print(date)
// Prints Date object
// 2020-05-12 00:00:00 +0000
The last 00.000 tells us fractional of a second, which is represented by SS.SSS
Refer to this page for more date formatter related queries.
Two things to note about the fractional second. First, it does not round, it just shows the appropriate places (otherwise the 2 digit fractional second would show 7 ms as “01”). Secondly, the Unicode standard lets you use as many as you want, so “SSSSS” is valid. However, DateFormatter will not show anything below milliseconds, it will just show 0s below that. So 123456789 nanoseconds with a “SSSSSSSSS” format string will show up as “123000000”

How to get local time as a formatted string in Lua

I need a date time string formatted as %Y-%m-%d %H:%M:%S.
I can't figure out how to use Lua's standard functions os.date() and os.time() to achieve that.
os.date is the function you are looking for. Its first optional parameter, format, does what you want:
os.date('%Y-%m-%d %H:%M:%S')
--> 2019-04-02 10:50:52
From the Lua 5.3 manual on os.date:
os.date ([format [, time]])
Returns a string or a table containing date and time, formatted according to the given string format.
If format starts with '!', then the date is formatted in Coordinated Universal Time.
If format is not "*t", then date returns the date as a string, formatted according to the same rules as the ISO C function strftime.
You can learn more about the formatting rules of C's strftime here.
In case you don't get your local time for whatever reason you can simply add the required offset.
local timeShift = 3 * 60 * 60 -- +3 hours
os.date('%Y-%m-%d %H:%M:%S', os.time() + timeShift)
--> 2019-04-02 18:24:15 for 15:24:15 UTC
I use lib https://github.com/Tieske/date. Get localtime -
date(true):addminutes("your offset"):fmt('%Y-%m-%d %H:%M:%S'),

Parsing string timestamp with time zone in 3-digit format followed by 'Z'

In the Hadoop infrastructure (Java-based) I am getting timestamps as string values in this format:
2015-10-01T04:22:38:208Z
2015-10-01T04:23:35:471Z
2015-10-01T04:24:33:422Z
I tried different patters following examples for SimpleDateFormat Java class without any success.
Replaced 'T' with ' ' and 'Z' with '', then
"yyyy-MM-dd HH:mm:ss:ZZZ"
"yyyy-MM-dd HH:mm:ss:zzz"
"yyyy-MM-dd HH:mm:ss:Z"
"yyyy-MM-dd HH:mm:ss:z"
Without replacement,
"yyyy-MM-dd'T'HH:mm:ss:zzz'Z'"
In fact, this format is not listed among examples. What should I do with it?
Maybe those 3 digits are milliseconds, and time is in UTC, like this: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"? But it still should look like "2015-11-27T10:50:44.000-08:00" as standardized format ISO-8601.
Maybe, this format is not parsed correctly in the first place?
I use Ruby, Python, Pig, Hive to work with it (but not Java directly), so any example helps. Thanks!
I very strongly suspect the final three digits are nothing to do with time zones, but are instead milliseconds, and yes, the Z means UTC. It's a little odd that they're using : instead of . as the separator between seconds and milliseconds, but that can happen sometimes.
In that case you want
"yyyy-MM-dd'T'HH:mm:ss:SSSX"
... or use
"yyyy-MM-dd'T'HH:mm:ss:SSS'Z'"
and set your SimpleDateFormat's time zone to UTC explicitly.

In what format is this date string?

Im trying to convert a bunch of NSStrings into NSDate objects. Here is an example string:
2013-04-25T15:51:30.427+1.00
But I can't figure out what format it is in, so far I have (The question marks are the bits I'm stumped with):
yyyy-MM-ddTHH:mm:ss????zzz
The main problem I'm having is with the '.427' part although if I'm making a mistake elsewhere, let me know :)
Does anyone have any ideas? Or could point me to a list of all the possible date format specifiers? I found this: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx which is useful but it doesn't appear to have any specifier for the '.427' part that I'm stuck on.
Any help is appreciated, Thanks.
The proper format is yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ.
See Unicode Date Format Patterns.
Also, the ZZZZZ format for the +00:00 timezone format was added to iOS 6 and is not supported under iOS 5 or earlier.
It is a valid date format as specified by ISO 8601, according to W3
The formats are as follows. Exactly the components shown here must be
present, with exactly this punctuation. Note that the "T" appears
literally in the string, to indicate the beginning of the time
element, as specified in ISO 8601.
Year:
YYYY (eg 1997) Year and month:
YYYY-MM (eg 1997-07) Complete date:
YYYY-MM-DD (eg 1997-07-16) Complete date plus hours and minutes:
YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00) Complete date plus hours, minutes and seconds:
YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00) Complete date plus hours, minutes, seconds and a decimal fraction of a
second
YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
where:
YYYY = four-digit year
MM = two-digit month (01=January, etc.)
DD = two-digit day of month (01 through 31)
hh = two digits of hour (00 through 23) (am/pm NOT allowed)
mm = two digits of minute (00 through 59)
ss = two digits of second (00 through 59)
s = one or more digits representing a decimal fraction of a second
TZD = time zone designator (Z or +hh:mm or -hh:mm)
In your case, the date has millisecond accuracy, try adding .sss to the format

Resources