Skip to content

Instantly share code, notes, and snippets.

@tardieu
Last active January 26, 2017 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tardieu/b6a9c4d53d56d089c58089ba8f6274b5 to your computer and use it in GitHub Desktop.
Save tardieu/b6a9c4d53d56d089c58089ba8f6274b5 to your computer and use it in GitHub Desktop.
Several ways to compose an HTTP date in Swift
import Foundation
// get current date as a series of integers
// (could be done differently...)
var theTime = time(nil)
var timeStruct = tm()
gmtime_r(&theTime, &timeStruct)
let wday = Int(timeStruct.tm_wday)
let mday = Int(timeStruct.tm_mday)
let mon = Int(timeStruct.tm_mon)
let year = Int(timeStruct.tm_year) + 1900
let hour = Int(timeStruct.tm_hour)
let min = Int(timeStruct.tm_min)
let sec = Int(timeStruct.tm_sec)
let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
let days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
func twoDigit(_ num: Int) -> String {
return (num < 10 ? "0" : "") + String(num)
}
let twoDigit = ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09",
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
"20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
"30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
"40", "41", "42", "43", "44", "45", "46", "47", "48", "49",
"50", "51", "52", "53", "54", "55", "56", "57", "58", "59",
"60", "61", "62", "63", "64", "65", "66", "67", "68", "69",
"70", "71", "72", "73", "74", "75", "76", "77", "78", "79",
"80", "81", "82", "83", "84", "85", "86", "87", "88", "89",
"90", "91", "92", "93", "94", "95", "96", "97", "98", "99"]
// interpolation + func
func httpDate() -> String {
return "\(days[wday]), \(twoDigit(mday)) \(months[mon]) \(year) \(twoDigit(hour)):\(twoDigit(min)):\(twoDigit(sec)) GMT"
}
// interpolation + array
func httpDate1() -> String {
return "\(days[wday]), \(twoDigit[mday]) \(months[mon]) \(year) \(twoDigit[hour]):\(twoDigit[min]):\(twoDigit[sec]) GMT"
}
// append + array
func httpDate2() -> String {
var s = days[wday]
s.append(", ")
s.append(twoDigit[mday])
s.append(" ")
s.append(months[mon])
s.append(" ")
s.append(twoDigit[year/100])
s.append(twoDigit[year%100])
s.append(" ")
s.append(twoDigit[hour])
s.append(":")
s.append(twoDigit[min])
s.append(":")
s.append(twoDigit[sec])
s.append(" GMT")
return s
}
// memcpy + array
func httpDate3() -> String {
var s = "XXX, XX XXX XXXX XX:XX:XX GMT"
s.append("") // force alloc
let ptr = s._core._baseAddress!
memcpy(ptr, days[wday]._core._baseAddress!, 3)
memcpy(ptr.advanced(by: 8), months[mon]._core._baseAddress!, 3)
memcpy(ptr.advanced(by: 5), twoDigit[mday]._core._baseAddress!, 2)
memcpy(ptr.advanced(by: 12), twoDigit[year/100]._core._baseAddress!, 2)
memcpy(ptr.advanced(by: 14), twoDigit[year%100]._core._baseAddress!, 2)
memcpy(ptr.advanced(by: 17), twoDigit[hour]._core._baseAddress!, 2)
memcpy(ptr.advanced(by: 20), twoDigit[min]._core._baseAddress!, 2)
memcpy(ptr.advanced(by: 23), twoDigit[sec]._core._baseAddress!, 2)
return s
}
var s = ""
var now = mach_absolute_time()
for _ in 0..<1000000 {
s = httpDate()
}
print(s)
print("interpolation + func: \(Double(mach_absolute_time() - now) / 1e9)s\n")
now = mach_absolute_time()
for _ in 0..<1000000 {
s = httpDate1()
}
print(s)
print("interpolation + array: \(Double(mach_absolute_time() - now) / 1e9)s\n")
now = mach_absolute_time()
for _ in 0..<1000000 {
s = httpDate2()
}
print(s)
print("append: \(Double(mach_absolute_time() - now) / 1e9)s\n")
now = mach_absolute_time()
for _ in 0..<1000000 {
s = httpDate3()
}
print(s)
print("memcpy: \(Double(mach_absolute_time() - now) / 1e9)s\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment