NSDateFormatter string format for showing milliseconds - objective-c

I need to retrieve the current date with this format:
yyyy-MM-dd-hh-mm-ss-XXX
But which are the letters to use for the milliseconds?

As is documented by Unicode, S means fractional second:
Fractional Second - rounds to the count of letters. (example is for 12.34567)
Use as many S as needed for the given precision.

The letters for miliseconds are SS, so you should do:
yyyy-MM-dd-hh-mm-ss-SSS

Related

SQLite strftime() returning null with integers

I have a date with the column name "date" in my table "productions" stored as integer with Unix format ( example: 1548263300000). And I want to retrieve only the year. When I do:
SELECT strftime('%Y ',date) as year
FROM productions
it returns null.
When I change the type to TEXT into my table and I store the date in string format ( example 2020-05-01 ), the same sql returns me "2020" which is correct and what I was looking for.
Why strftime() doesn't work with integers since the SQLite documentation say you can work with TEXT,INTEGER and REAL for dates? How to use date functions with integers?
extra information:
In this tutorial, they also use strftime with integers and it seems to work for them, so I understand from that, that the functions are available no matter what type you use ( text,int,real): link
when I use:
SELECT strftime('%Y',DATETIME(ROUND(date/ 1000), 'unixepoch'))
FROM productions;
it works fine, but I don't understand why I have to do all this when I use integers but when I use text, it works directly.
You can use strftime() but you have to add the 'unixepoch' modifier:
strftime('%Y', date / 1000, 'unixepoch')
so your date / 1000 is recognized as the number of seconds since 1970-01-01.
From Date And Time Functions:
The "unixepoch" modifier (11) only works if it immediately follows a
timestring in the DDDDDDDDDD format. This modifier causes the
DDDDDDDDDD to be interpreted not as a Julian day number as it normally
would be, but as Unix Time - the number of seconds since 1970.

Covert ZULU time to PST

I am trying to covert start_time which is in yulu format to pst.
Start_time sample: 2020-02-04T04:36:42:211Z
from_unixtime(unix_timestamp(sub string(start_time,1,17),'yyyy-MM-ddThh:mm:ss.SSSZ),'yyyy-MM-dd hh:mm:ss)
But I am getting output as NULL.
Please help.
Escape T, Z in the string. Note the use of double-quotes for the pattern and T and Z are escaped with a single-quote.
select from_unixtime(unix_timestamp('2020-02-04T04:36:42:211Z',"yyyy-MM-dd'T'HH:mm:ss:SSS'Z'")
,'yyyy-MM-dd HH:mm:ss')
Also, you don't need a substring as you are matching the pattern for the full string.
It is better to use FROM_UTC_TIMESTAMP because UNIX_TIMESTAMP returns seconds, you will lose the millisecond component of your timestamp.
FROM_UTC_TIMESTAMP(UNIX_TIMESTAMP(2020-02-04T04:36:42:211Z, "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'"), 'PST')
Sometime it might possible that because of "T" and "Z" our result get distorted.In that case we can use:
from_utc_timestamp(CONCAT(substring('2020-02-04T04:36:42:211Z',1,10)," ",substring('2020-02-04T04:36:42:211Z',12,12)),'PST')

This code gives you tomorrow after midday

Consider this:
Dim n as Long
n = Now
Why does this give you tomorrow after midday?
It's because Now returns a double to represent the time of day as well as the day itself. The fractional part is used to model the time of day and at noon that value is exactly one half.
Also, the implicit cast to Long rounds the value. This is unlike the casts of c and c++ which truncate the fractional part.
You could use Fix(Now) to truncate.

Qlikview Timestamp formatting upto microseconds?

In qlikview I can get timestamp in milliseconds, by setting timestamp format as :-
SET TimestampFormat='MM/DD/YYYY hh:mm:ss.fff';
I want to know if there is a way to get time stamp in qlikview upto microseconds.
Formula to optain microseconds from TimeField:
((frac(TimeField) * 86400000) - floor(frac(TimeField) * 86400000)) * 1000 as Micro
And I would use this formula for formatting:
Timestamp(TimeField - (Micro/86400000000)) & Num(floor(Micro), '000') as TimeStamp
As far as I can determine from the QlikView help, there is no format specifier for microseconds, only for milliseconds.
If you need to obtain the microsecond value from a time, I quickly threw the below together (it can probably be done a bit neater). Here I assume your input time field is called TimeField. We can obtain the number of milliseconds using:
=((TimeField-num(date(floor(TimeField)))-
num(maketime(hour(TimeField),minute(TimeField),second(TimeField))))*24*60*60)*1000
For the sake of simplicity, I will call the above formula MillisecondCount. Then, using this field, we can then calculate the number of microseconds:
=floor(((MillisecondCount)-floor(MillisecondCount))*1000)
Finally, the full formula to obtain microseconds becomes:
floor(((((TimeField-num(date(floor(TimeField)))-
num(maketime(hour(TimeField),minute(TimeField),second(TimeField))))*24*60*60)*1000)
-floor(((TimeField-num(date(floor(TimeField)))-
num(maketime(hour(TimeField),minute(TimeField),second(TimeField))))*24*60*60)*1000))*1000)
You can then just format this with num() and append it to your time-stamp string.

Format string for NSDateFormatter to produce milliseconds

Since my date is well formatted now all there is left for me to do is to print milliseconds too.
I already tried setDateFormat yyyy/mm/dd hh:mm:ss:ms
That didn't work. Any advice?
Try to use 'SS' specifier (with number of S's equal to number of digits you want to get - 3 in your case), e.g.
[formatter setDateFormat:#"yyyy/MM/dd hh:mm:ss:SSS"];
Try this format:
"yyyy/MM/dd HH:mm:ss.SSS"