0

I am working with Alamofire 4.0 and Swift 3.0
I am supposed to POST current date and time with timezone in my web service.
When I try to print in the console, I get correct current time, but when that data is pushed to the web service using Alamofire, the date and time gets changed/increased by 8 hours.
This is the code that I am trying. Please help me and make me understand when and why it is going wrong.

let now = NSDate();
        let formatter = DateFormatter();
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss zzz";
        let defaultTimeZoneStr = formatter.string(from: now as Date);
        print(defaultTimeZoneStr)


        Alamofire.request("http:********", method: .post, parameters:  ["id": 1, "date" : defaultTimeZoneStr, "value": "null", "publisher": "Jack", "title": "Time Warner, "url": "internet/6/"], encoding: JSONEncoding.default).responseString
        {
                response in
                print("Success: \(response.result.isSuccess)")
                print("Response String: \(response.result.value)")
        }

So in console, defaultTimeZoneStr will be my current time. But in the web service, it will be 8 hours late.
Please help.
Thank you.

shallowThought
  • 19,212
  • 9
  • 65
  • 112
iCoder
  • 53
  • 1
  • 8
  • you should check (1) if timezone is included to timeString and (2) parsed also on serverside and (3) put correct in the database (4) correct read from database with timezone (5) print the time with your local timezone – muescha Jan 21 '17 at 08:09
  • you use `"zzz"` -> `"GMT+1"` but there is also the `"Z"` for the `"+0100"` maybe this is better parsed on serverside. – muescha Jan 21 '17 at 08:15
  • for a good answer you need include the server side code and some of the db code – muescha Jan 21 '17 at 08:15
  • http://stackoverflow.com/questions/28016578/swift-how-to-create-a-date-time-stamp-and-format-as-iso-8601-rfc-3339-utc-tim/28016692#28016692 – Leo Dabus Jan 21 '17 at 22:27

2 Answers2

1

zzz returns abbreviated time zone display name.

You should know that:

  1. Time zone display names are not unique. For example, the commonly used PST can stand for both Pacific Standard Time (UTC-08) and Philippine Standard Time (UTC+08).

  2. Time zone display names can be different in different languages. You have not set a locale for the formatter, therefore the name can be random.

In short, don't use zzz if you need a standard format, e.g. for API calls. Use one of the Z or X variants.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
0

It's about your Simulator or Phone Time Zone. It works always with UTC.

In my opinion you always work with UTC 0.

Ali Ihsan URAL
  • 1,894
  • 1
  • 20
  • 43