Processing time and date software development is a very common operation,Almost all programming languages provide a corresponding API to facilitate the development of the date and time to process,Swift is no exception,such as DateFormatter ,You can use it any Date Custom format is converted into a text string。
--but, DateFormatter very slow! It is a very time-consuming operation,From creation to use,Each step takes up a lot of time,If it is not necessary,Almost all senior programmer will not recommend you use this object。
C commonplace language
Swift is a C language can be called directly,In addition to the Foundation in DateFormatter ,In fact, you can also choose C strftime ,It is a lot less than the former advanced features,But if you need just the formatted output time,that strftime It will be very easy to use。
Advantages (relative DateFormatter Speaking):
- high speed,The same operation DateFormatter want 1.4 ms to complete,and strftime It takes only 0.2 ms,Even faster!
- Small memory footprint,Since there is no DateFormatter As complex functions,Very conserve memory resources。
Shortcoming:
- Code may not be on DateFormatter So easy to operate,After all, the C language API,Even with Swift to call,It will be very painful;
- It does not support time zone,Up to get the current time zone and the time zone utc;
- Format operation is relatively small,Common has,But if you want to DateFormatter So many formats,You may need to deal with it yourself。
Code
Fortunately,Complex code also only needs to disposable packages,Usually show time communicating with the server or the user,Time zone does not require complex operations,So most of the business scenario can be used strftime instead DateFormatter of。
We wrote a Date The extension to encapsulate this operation:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
extension Date { func formattedTime(format: String) -> String? { let resultSize = format.count + 200 var result = [Int8](repeating: 0, count: resultSize) var currentTime = time(nil) var time = localtime(¤tTime).pointee guard strftime(&result, resultSize, format, &time) != 0 else { return nil } return String(cString: result, encoding: .utf8) } } |
Some difficult place here is the need to use C pointers,But Swift again pointer well hidden,In short,We use a Int8 The array to hold the returned string C,It is then converted into the final string returns in Swift。
format
Code written,The rest formatting problems, strftime and DateFormatter The format is not the same,The latter is generally directly corresponds to the macro,such as yyyy It represents a four-digit year,and yy It is a two-digit year,The former are used placeholders, %Y Equivalent to yyyy , %Y It is equivalent to yy ......
strftime See a complete list of symbols Here(English),Here(Chinese)
use
The next line of code can be used for the formatting of the output date and time:
1 |
print(Date().formattedTime(format: "%y|%Y|%m|%d|%I|%H|%M|%S|%w")) |
😎
References
- https://github.com/aciidb0mb3r/SPM-PkgConfig/blob/master/Sources/POSIX/strftime.swift
- https://www.runoob.com/python/att-time-strftime.html
- https://wiki.jikexueyuan.com/project/c/strftime.html
- https://stackoverflow.com/questions/30932465/how-to-format-string-in-swift-using-strftime
Original article written by LogStudio:R0uter's Blog » Swift strftime use quick format date and time
Reproduced Please keep the source and description link:https://www.logcg.com/archives/3309.html