When I'm on the simulator and click the button it keeps adding 50. I think it may not be saving "latestTerminationDate", or maybe not pulling it. Thank you for any help
#IBOutlet weak var coalRunButton: UIButton!
var coalMayham = 1
#IBAction func coalRunButton(_ sender: Any) {
if let buttonPress = UserDefaults.standard.object(forKey: "buttonPress") as? Date,
let terminationDate = UserDefaults.standard.object(forKey: "latestTerminationDate") as? Date {
var terminationDuration = buttonPress.timeIntervalSince(terminationDate)
if terminationDuration >= 86400 {
_ = 86400
do { UserDefaults.standard.set(Date(), forKey: "latestTerminationDate")
UserDefaults.standard.synchronize()
let dailyCoalAccumulate = ((Int(terminationDuration)) * coalMayham) + Int(coalPile.text!)!
coalPile.text = String(dailyCoalAccumulate)
UserDefaults.standard.set(Data(), forKey:"totalCoal")
}
} else {
terminationDuration = 50
UserDefaults.standard.set(Date(), forKey: "latestTerminationDate")
UserDefaults.standard.synchronize()
let dailyCoalAccumulate = ((Int(terminationDuration)) * coalMayham) + Int(coalPile.text!)!
coalPile.text = String(dailyCoalAccumulate)
UserDefaults.standard.set(Data(), forKey:"totalCoal")
}
} else {
let terminationDuration = 40
UserDefaults.standard.set(Date(), forKey: "latestTerminationDate")
UserDefaults.standard.synchronize()
let dailyCoalAccumulate = ((Int(terminationDuration)) * coalMayham) + Int(coalPile.text!)!
coalPile.text = String(dailyCoalAccumulate)
UserDefaults.standard.set(Data(), forKey:"totalCoal")
}
}
This line of code:
let terminationDate = UserDefaults.standard.object(forKey: "latestTerminationDate") as? Date
Is not returning what you think it does. Because in this line of code:
UserDefaults.standard.set(Date(), forKey: "latestTerminationDate")
the Date() you are entering looks like: "2018-10-04 15:30:22 +0000"
which can not be compared to 86400
For that reason:
if terminationDuration >= 86400
fails and reverts to:
else {
terminationDuration = 50
EDIT
FIX EXAMPLE:
func getMillisecondsNow() -> Int64{
let currentDate = Date()
return getMillisecondsFromDate(date: currentDate)
}
func getMillisecondsFromDate(date: Date) -> Int64{
var d : Int64 = 0
let interval = date.timeIntervalSince1970
d = Int64(interval * 1000)
return d
}
func getTimeDifferenceFromNowInMilliseconds(time: Int64) -> Int64{
let now = getMillisecondsNow()
let diff: Int64 = now - time
return diff
}
This will be in milliseconds so you will need to use 86400000 (milliseconds in 24 hrs) instead of 86400 (seconds in 24 hrs)
Now in your code simply use this to save the termination time in Milliseconds:
let now = getMillisecondsNow()
UserDefaults.standard.set(now, forKey: "latestTerminationDate")
and this code to retrieve and get the time difference in Milliseconds:
let terminationTime = UserDefaults.standard.object(forKey: "latestTerminationDate") as? Int64
let timeDiff = getTimeDifferenceFromNowInMilliseconds(time: terminationTime)
if timeDiff >= 86400000 {
}
else {
}
Related
On my project I need to work with hours and min.
I can't find to much info online.
here my issue:
let time1 = "22:00"
let time2 = "20:00"
Question 1:
how do I subtract the time1 - time2 ?
I'm expecting result = 02:00 h
i start writing some code, converting this time to date..
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm"
var time1d = formatter.date(from: time1)!
let time2d = formatter.date(from: time2)!
but now how do I subtract this two hours?
Question 2:
let time1 = "22:00"
let time2 = "20:00"
How do i sum time1 + time2 ? for example should give me result 42:00 hours
thanks for the help
This is not a Date. If you are only gonna work with hours and minutes and the input will always be a string properly formatted you should struct your data.
Create a Time structure
struct Time {
let hour: Int
let minute: Int
}
And a custom initializer. This assumes your string is always properly formatted 00:00:
extension Time {
init?(string: String) {
guard string.count == 5,
Array(string)[2] == ":",
let hour = Int(string.prefix(2)),
let minute = Int(string.suffix(2)),
0...59 ~= minute else {
return nil
}
self.hour = hour
self.minute = minute
}
}
For displaying your Time struct property you can conform it to CustomStringConvertible and provide a custom description
extension Time: CustomStringConvertible {
var description: String {
String(format: "%02d:%02d", hour, minute)
}
}
Regarding adding and subtracting you can make your Time struct conform to AdditiveArithmetic and implement the required operators:
extension Time: AdditiveArithmetic {
static func - (lhs: Time, rhs: Time) -> Time {
let minutes = lhs.minute - rhs.minute + lhs.hour * 60 - rhs.hour * 60
return .init(hour: minutes/60, minute: minutes%60)
}
static func + (lhs: Time, rhs: Time) -> Time {
let minutes = lhs.minute + rhs.minute + lhs.hour * 60 + rhs.hour * 60
return .init(hour: minutes/60, minute: minutes%60)
}
static var zero: Time { .init(hour: 0, minute: 0) }
}
Playground testing:
let time1 = Time(string: "22:00")!
let time2 = Time(string: "20:00")!
let time3 = time1-time2
print(time3)
let time4 = time1+time2
print(time4)
Those will print
02:00
42:00
First we create the calculateDifference function which uses dateComponents function provided by Swift.
let time1 = "22:00"
let time2 = "20:00"
func formattedTime(_ time: String) -> Date? {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "HH:mm"
return formatter.date(from: time)
}
func calculateDifference(_ from: String, _ to: String) -> (hour: Int, minutes: Int) {
guard let fromTime = formattedTime(from),
let toTime = formattedTime(to) else {
return (0,0)
}
let calendar = Calendar.current
let components = calendar.dateComponents([.hour, .minute], from: fromTime, to: toTime)
return (components.hour ?? 0, components.minute ?? 0)
}
Then we create the calculateSum which uses the calculateDifference internally to get the hours by supplying the default hour as 00:00
func calculateSum(_ time1: String, _ time2: String) -> (hour: Int, minutes: Int) {
let defaultTime = "00:00"
let calculatedTime1 = calculateDifference(defaultTime, time1)
let calcaultedTime2 = calculateDifference(defaultTime, time2)
return(calculatedTime1.hour + calcaultedTime2.hour,
calculatedTime1.minutes + calcaultedTime2.minutes)
}
Now if we run the below, we get the required results. Of course, some formatting will be required.
let difference = calculateDifference(time2, time1)
print("\(difference.hour):\(difference.minutes)")
let sum = calculateSum(time1, time2)
print("\(sum.hour):\(sum.minutes)")
I try to check the speed of upload by sending an image to a server via FTP, I know it is not very sharp, but I have no alternative. Issue number one is to test time, this code is always giving me 0 seconds maybe it is right maybe not, but the main issue id that I cannot even divide the size in mb of image by time in seconds, since time elapsed is expressed in dateComponent, how to do it?
using this code
func pushfileUpload() {
print("uploading...")
let startDate = NSDate()
//*****************************************************************
//find file in app bundle
let imageChecked = findFileInBundle(nameWithNoEx: "image", extension: "jpg")
//convert to Data
let imageData = imageChecked.jpegData(compressionQuality: 1)
//instanziate the class
let ftpUploader = FTPUpload.init(baseUrl: Constants.kHostname, userName: Constants.kUsername, password: Constants.kPassword, directoryPath: Constants.kFolder)
ftpUploader.send(data: imageData!, with: "image") { (result) in
if result {
print("result is \(result)")
} else {
print("no result")
}
}
//*****************************************************************
print("...uploaded")
let endDate = NSDate()
let difference = timeDifference(date1: startDate as Date, date2: endDate as Date)
// print("Time difference is : \(difference)")
//1 converto to string
let differenceString = String(difference)
//2 pick first 3 ints
let array = differenceString.compactMap{Int(String($0))}
//3 create new int
let newInt = array[0...3]
var newString = ""
for i in newInt {
newString.append(i.description)
}
var fromIntToString = Int(newString)
fromIntToString = fromIntToString! * 1000
let speed = 1500 / fromIntToString!
print("speed: \(speed)")
}
func timeDifference(date1: Date, date2: Date) -> Int {
let calendar = NSCalendar.current
var compos:Set<Calendar.Component> = Set<Calendar.Component>()
// compos.insert(.second)
compos.insert(.nanosecond)
let difference = calendar.dateComponents(compos, from: date1, to: date2)
// print("diff in seconds= \(difference.second!)") // difference in seconds
print("diff in nanoseconds = \(difference.nanosecond!)") // difference in nanoseconds
let newValue = difference.nanosecond!
return newValue
}
//UPADTED code
func pushfileUpload() {
print("uploading...")
let startDate = Date()
//*****************************************************************
//find file in app bundle
let imageChecked = findFileInBundle(nameWithNoEx: "image", extension: "jpg")
//convert to Data
let imageData = imageChecked.jpegData(compressionQuality: 1)
//instanziate the class
let ftpUploader = FTPUpload.init(baseUrl: Constants.kHostname, userName: Constants.kUsername, password: Constants.kPassword, directoryPath: Constants.kFolder)
ftpUploader.send(data: imageData!, with: "image") { (result) in
if result {
print("result is \(result)")
//-----------------------------------------------------
//Your code to calculate elapsed time belongs here
let endDate = Date()
let elapsed = endDate.timeIntervalSinceReferenceDate -
startDate.timeIntervalSinceReferenceDate
print("The download took \(elapsed) seconds.")
print("speed is \(1500 / elapsed)")
//-----------------------------------------------------
} else {
print("no result")
}
}}
prints on console
The download took 1.281269907951355 seconds.
speed is 1170.7135168720042
As others have said, you need to move your code that calculates total time inside your closure:
func pushfileUpload() {
print("uploading...")
let startDate = Date()
//*****************************************************************
//find file in app bundle
let imageChecked = findFileInBundle(nameWithNoEx: "image", extension: "jpg")
//convert to Data
let imageData = imageChecked.jpegData(compressionQuality: 1)
//instanziate the class
let ftpUploader = FTPUpload.init(baseUrl: Constants.kHostname, userName: Constants.kUsername, password: Constants.kPassword, directoryPath: Constants.kFolder)
ftpUploader.send(data: imageData!, with: "image") { (result) in
if result {
print("result is \(result)")
//-----------------------------------------------------
//Your code to calculate elapsed time belongs here
let endDate = Date()
let elapsed = endDate.timeIntervalSinceReferenceDate -
startDate.timeIntervalSinceReferenceDate
print("The download took \(elasped) seconds."
//-----------------------------------------------------
} else {
print("no result")
}
}
...
As others have mentioned, there's no reason to deal with date components. The method timeIntervalSinceReferenceDate gives you a double precision count of seconds for a date, so it's easy to do math on dates to figure out the difference between them. You can evaluate the difference to as many decimal places as you want.
I am new at Swift the code builds just fine but the greater than (>) dose not work. I'm trying to producing at a number in the "totalCoal" label, but never goes over the "coalPileHolding" Second label. I know that this code can be way better But i am trying to get the basic first. I also know that the timeDiffernt ">" dose not work also so somehow I am missing something. Thank you for your help
#IBOutlet weak var coalPileHoldingLabel: UILabel!
func loadBigCoalPile () {
var coalPileHolding = Int ()
if UserDefaults.standard.object(forKey: "coalPileResearch") == nil {
coalPileHolding = 0 } else {
coalPileHolding = UserDefaults.standard.object(forKey: "coalPileResearch") as! Int}
if coalPileHolding == 1 {
let coalPileHolding = 200
coalPileHoldingLabel.text = String(coalPileHolding) }
if coalPileHolding == 2 {
let coalPileHolding = 300
coalPileHoldingLabel.text = String(coalPileHolding) }
if coalPileHolding == 3 {
let coalPileHolding = 400
coalPileHoldingLabel.text = String(coalPileHolding) }
#objc func buttonIsInAction(){
}
#IBOutlet weak var coalRunButton: UIButton!
#IBAction func coalRunButton(_ sender: Any) {
func getMillisecondsNow() -> Int64{
let currentDate = Date()
return getMillisecondsFromDate(date: currentDate)
}
func getMillisecondsFromDate(date: Date) -> Int64{
var d : Int64 = 0
let interval = date.timeIntervalSince1970
d = Int64(interval * 1000)
return d
}
func getTimeDifferenceFromNowInMilliseconds(time: Int64) -> Int64{
let now = getMillisecondsNow()
let diff: Int64 = now - time
return diff
}
var terminationTime = Int64()
if UserDefaults.standard.object(forKey: "latestTerminationDate") == nil {
terminationTime = getMillisecondsNow()
UserDefaults.standard.set(terminationTime, forKey:"latestTerminationDate")
}
else {
terminationTime = UserDefaults.standard.object(forKey: "latestTerminationDate") as! Int64 }
let timeDiff = getTimeDifferenceFromNowInMilliseconds(time: terminationTime)
let timeDiffernt = Int(timeDiff)
let now = getMillisecondsNow()
UserDefaults.standard.set (now, forKey: "latestTerminationDate")
if timeDiffernt > 86400000 { _ = 86400000}
var methodOfCut = Int ()
var machineryButton = Int ()
var qualityOfWorkers = Int ()
if UserDefaults.standard.object(forKey: "methodOfCut") == nil {
methodOfCut = 0 } else {
methodOfCut = UserDefaults.standard.object(forKey: "methodOfCut") as! Int}
if UserDefaults.standard.object(forKey: "machineryButton") == nil {
machineryButton = 0 } else {
machineryButton = UserDefaults.standard.object(forKey: "machineryButton") as! Int}
if UserDefaults.standard.object(forKey: "qualityOfWorkers") == nil {
qualityOfWorkers = 0 } else {
qualityOfWorkers = UserDefaults.standard.object(forKey: "qualityOfWorkers") as! Int}
let coalMayham = (machineryButton) + (qualityOfWorkers) + (methodOfCut)
let (dailyCoalAccumulate) = ((timeDiffernt) * (coalMayham) + 1) / 10000
var coalPileHolding2 = 0
if let coalPile = Int(coalPileLabel.text!) {
let totalCoal = (dailyCoalAccumulate) + coalPile
coalPileHolding2 = Int(coalPileHoldingLabel.text!) ?? 0
if totalCoal > coalPileHolding2 { coalPileHolding2 = totalCoal }
coalPileLabel.text = String(totalCoal)
UserDefaults.standard.set(totalCoal, forKey:"totalCoal")}
callOutLabel.text = String(dailyCoalAccumulate)}}
That mix of numeric types (Int32, Float, Int) is rather confusing. In general you want to use Int or Double. All other variants should only be used when absolutely necessary, for example if an API requires a different type. So lets assume that dailyCoalAccumulate is Int and switch everything else to Int too:
let coalPileHolding = 0
if let coalPile = Int(coalPileLabel.text!) {
let totalCoal = dailyCoalAccumulate + coalPile
let coalPileHolding = Int((coalPileHoldingLabel.text as! NSString).intValue)
if totalCoal > coalPileHolding {
let coalPileHolding = totalCoal
}
coalPileLabel.text = String(totalCoal)
UserDefaults.standard.set(totalCoal, forKey:"totalCoal")
}
callOutLabel.text = String(dailyCoalAccumulate)
Here the intValue API of NSString returns Int32 but I immediately convert it to a regular Int. But of course there is a better way to do this without having to bridge to the Objective-C NSString. If the string doesn't contain a number intValue simply returns zero. We can produce the same behavior when we use the Int initializer to convert the string and then replace the nil value with zero: Int(coalPileHoldingLabel.text!) ?? 0.
Then we have three different variables named coalPileHolding. Since they are defined in different scopes they can share the same name, but are still different variables. My guess is that you want to actually update the coalPileHolding variable. Otherwise the assignment in the inner if makes no sense - the compiler even warns about that.
So lets change coalPileHolding to var and update its value.
var coalPileHolding = 0
if let coalPile = Int(coalPileLabel.text!) {
let totalCoal = dailyCoalAccumulate + coalPile
coalPileHolding = Int(coalPileHoldingLabel.text!) ?? 0
if totalCoal > coalPileHolding {
coalPileHolding = totalCoal
}
coalPileLabel.text = String(totalCoal)
UserDefaults.standard.set(totalCoal, forKey:"totalCoal")
}
callOutLabel.text = String(dailyCoalAccumulate)
I was converting the timestamp to time but my timeStampToDate is giving this output "Optional(1476775542548)" due to which it crashes.So how i can remove this Optional string.
let timeStampToDate = (String(describing:merchant.post["timestamp"])) as String
let timeSt = Date(jsonDate:"/Date(\(timeStampToDate))/")
merchantOpenLbl.text = Date().onlyTimee(date: timeSt!)
init?(jsonDate: String) {
// "/Date(1487058855745)/"
let prefix = "/Date("
let suffix = ")/"
let scanner = Scanner(string: jsonDate)
// Check prefix:
guard scanner.scanString(prefix, into: nil) else { return nil }
// Read milliseconds part:
var milliseconds : Int64 = 0
guard scanner.scanInt64(&milliseconds) else { return nil }
// Milliseconds to seconds:
var timeStamp = TimeInterval(milliseconds)/1000.0
// Read optional timezone part:
var timeZoneOffset : Int = 0
if scanner.scanInt(&timeZoneOffset) {
let hours = timeZoneOffset / 100
let minutes = timeZoneOffset % 100
// Adjust timestamp according to timezone:
timeStamp += TimeInterval(3600 * hours + 60 * minutes)
}
// Check suffix:
guard scanner.scanString(suffix, into: nil) else { return nil }
// Success! Create NSDate and return.
self.init(timeIntervalSince1970: timeStamp)
}
Wrapped the optional value that you getting from merchant.post["timestamp"].
if let timeStampToDate = merchant.post["timestamp"] as? String {
print(timeStampToDate)
let timeSt = Date(jsonDate:"/Date(\(timeStampToDate)))/")
merchantOpenLbl.text = Date().onlyTimee(date: timeSt!)
}
Note: If it is still not works then you need to show us declaration of Date(jsonDate:)
Edit: If it is not string then try like this way
if let timeStampToDate = merchant.post["timestamp"] {
print(timeStampToDate)
let timeSt = Date(jsonDate:"/Date(\(timeStampToDate)))/")
merchantOpenLbl.text = Date().onlyTimee(date: timeSt!)
}
You can use guard let to wrap the optional value. Replace your code with below code.
guard let timeStampToDate = merchant.post["timestamp"] as? String else {
return
}
let timeSt = Date(jsonDate:"/Date(\(timeStampToDate))/")
merchantOpenLbl.text = Date().onlyTimee(date: timeSt!)
let timeStampToDate = (String(describing:merchant.post["timestamp"])) as String
let timeSt = Date(jsonDate:"/Date(\(timeStampToDate!))/")
merchantOpenLbl.text = Date().onlyTimee(date: timeSt!)
Edit
if let timeStampToDate = (String(describing:merchant.post["timestamp"])) as? String {
let timeSt = Date(jsonDate:"/Date(\(timeStampToDate))/")
merchantOpenLbl.text = Date().onlyTimee(date: timeSt!)
}
I have a String in following format
"/Date(573465600000-0800)/"
How do I convert this to regular NSDate object?
The first part "573465600000" is the time since the Unix epoch
in milliseconds, and the second part "-0800" is a time zone
specification.
Here is a slight modification of Parsing JSON (date) to Swift
which also covers the time zone part:
extension NSDate {
convenience init?(jsonDate: String) {
let prefix = "/Date("
let suffix = ")/"
let scanner = NSScanner(string: jsonDate)
// Check prefix:
if scanner.scanString(prefix, intoString: nil) {
// Read milliseconds part:
var milliseconds : Int64 = 0
if scanner.scanLongLong(&milliseconds) {
// Milliseconds to seconds:
var timeStamp = NSTimeInterval(milliseconds)/1000.0
// Read optional timezone part:
var timeZoneOffset : Int = 0
if scanner.scanInteger(&timeZoneOffset) {
let hours = timeZoneOffset / 100
let minutes = timeZoneOffset % 100
// Adjust timestamp according to timezone:
timeStamp += NSTimeInterval(3600 * hours + 60 * minutes)
}
// Check suffix:
if scanner.scanString(suffix, intoString: nil) {
// Success! Create NSDate and return.
self.init(timeIntervalSince1970: timeStamp)
return
}
}
}
// Wrong format, return nil. (The compiler requires us to
// do an initialization first.)
self.init(timeIntervalSince1970: 0)
return nil
}
}
Example:
if let theDate = NSDate(jsonDate: "/Date(573465600000-0800)/") {
println(theDate)
} else {
println("wrong format")
}
Output:
1988-03-04 00:00:00 +0000
Update for Swift 3 (Xcode 8):
extension Date {
init?(jsonDate: String) {
let prefix = "/Date("
let suffix = ")/"
let scanner = Scanner(string: jsonDate)
// Check prefix:
guard scanner.scanString(prefix, into: nil) else { return nil }
// Read milliseconds part:
var milliseconds : Int64 = 0
guard scanner.scanInt64(&milliseconds) else { return nil }
// Milliseconds to seconds:
var timeStamp = TimeInterval(milliseconds)/1000.0
// Read optional timezone part:
var timeZoneOffset : Int = 0
if scanner.scanInt(&timeZoneOffset) {
let hours = timeZoneOffset / 100
let minutes = timeZoneOffset % 100
// Adjust timestamp according to timezone:
timeStamp += TimeInterval(3600 * hours + 60 * minutes)
}
// Check suffix:
guard scanner.scanString(suffix, into: nil) else { return nil }
// Success! Create NSDate and return.
self.init(timeIntervalSince1970: timeStamp)
}
}
Example:
if let theDate = Date(jsonDate: "/Date(573465600000-0800)/") {
print(theDate)
} else {
print("wrong format")
}
var date:NSDate = NSDate(timeIntervalSince1970: timeInterval)
let myTimeStamp = "1463689800000.0"
let dateTimeStamp = NSDate(timeIntervalSince1970:Double(myTimeStamp)!/1000) //UTC time
let dateFormatter = NSDateFormatter()
dateFormatter.timeZone = NSTimeZone.localTimeZone() //Edit
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.dateStyle = NSDateFormatterStyle.FullStyle
dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle
let strDateSelect = dateFormatter.stringFromDate(dateTimeStamp)
print(strDateSelect) //Local time
let dateFormatter2 = NSDateFormatter()
dateFormatter2.timeZone = NSTimeZone.localTimeZone()
dateFormatter2.dateFormat = "yyyy-MM-dd"
let date3 = dateFormatter.dateFromString(strDateSelect)
datepicker.date = date3!
Swift 4
let date = Date(timeIntervalSince1970: TimeInterval(1463689800000.0))