I try to make all date in the fire base at the same time zone for users from all the world.
How I got the same result from this two lines of code , thought I changed the timezone of the date and still the local date give the same timestamp as UTC time zone?
print(Timestamp.fromDate(DateTime.now()).toDate());
print(Timestamp.fromDate(DateTime.now().toUtc()).toDate());
print(Timestamp.fromDate(DateTime.now()).toDate());
print(Timestamp.fromDate(DateTime.now().toUtc()).toDate());
The DateTime type has a DateTime.utc() constructor you can use to create a UTC time:
print(DateTime.now());
print(DateTime.now().toUtc());
print(DateTime.utc(
DateTime.now().year,
DateTime.now().month,
DateTime.now().day,
DateTime.now().hour,
DateTime.now().minute,
DateTime.now().second,
).toUtc());
By default, the DateTime.now() constructor is in the user's local time:
print(DateTime.now().isUtc); // false
Related
I am storing dates from a Swift Project in Firestore without any problem. Dates are converted into UTC format and stored in Firestore as a Timestamp. All good.
Then, back on clientside, I can read them back and apply the TimeZone.current and the date/time are adjusted accordingly based on the timezone the user is currently in.
So, as en example, a time originally of:
9:00 pm Melbourne time (which is GMT+10),
shows as 7:00 am if the user is in New York.
Great.
But I have some items that I want to adjust for timezones (as per above) and others I don't.
So say I have two items the same as the above example, but one is an alarm and I want to keep at the time it was originally set for regardless of the new timezone... So still keep it at 9:00 pm.
I have a Bool flag saved in my database to say ignoreTimezone but I'm lost as to how to do this in Swift when reading back the timestamp from Firestore in UTC format and get it back to the original 9:00 pm.
All the Q&A's I've found are all about converting timezones etc. but not really on this example of ignoring one and keeping the date and time set to the timezone they were originally set for.
Thanks in advance for any help and/or suggestions.
Question updated as recommended
I have now incorporated the suggested code. So have a calendar extension:
extension Calendar {
func offsetFromMidnight(for date: Date) -> TimeInterval {
return date.timeIntervalSince(startOfDay(for: date))
}
}
Then I carry out the recommended steps.
Take an offset from midnight, in this case, the current Date():
let offsetSinceMidnight = UInt64(Calendar.current.offsetFromMidnight(for: Date()))
This value is then stored on the server.
I'm currently in Melbourne (Australia), so the date and time item used for testing is July 9 # 2:00pm.
When it is retrieved on the client end in a different timezone, I'm using the recommended code:
//Create a calendar for the target timezone
guard let chicagoTimeZone = TimeZone(identifier: "America/Chicago") else { fatalError() }
var chicagoCalendar = Calendar(identifier: .gregorian)
chicagoCalendar.timeZone = chicagoTimeZone
//Calculate midngiht in the target calendar
let chicagoMidnight = chicagoCalendar.startOfDay(for: Date())
//calculate the same time-of-day in the new timezone
let adjustedChicagoTime = Date(timeInterval: TimeInterval(offsetSinceMidnight), since: chicagoMidnight)
The output is set to the correct time, 2:00pm in Chicago, but because of the differnent dates (Chicago is still July 8th), then the midnight timeinterval is being applied on the wrong date. So I get July 8 # 2:00pm.
I'm assuming I will also need to capture the original date components to apply the offsetSinceMidnight to a date in the newTimeZone that has matching date components??? Or is there a better approach to this?
Date objects store an instant in time, anywhere in the world. They don't capture the idea of a time-of-day regardless of time zone.
To do that I would suggest calculating an offsetFromMidnight value.
Edited to fix return value.
extension Calendar {
func offsetFromMidnight(for date: Date) -> TimeInterval {
return date.timeIntervalSince(startOfDay(for: date))
}
}
You'd call that function in the user's current calendar to get the seconds since midnight in the user's current time zone. Save that to your database. (You could round to a long integer with very little loss of precision.)
I happen to BE in the NYT time zone (EDT) so using that as the destination time zone won't work for me since it won't change anything. Instead, I'll show code to convert from my timezone to GMT:
//Run on user's local machine (in EDT in my case):
let offsetSinceMidnight = UInt64(Calendar.current.offsetFromMidnight(for: Date()))
//Save offset to FireStore
Then if you want that same time of day in a new timezone, you'd use code like this:
//Create a calendar for the target time zone (or the user's local time zone on the destination machine)
guard let gmt = TimeZone(abbreviation: "GMT") else { fatalError() }
var gmtCalendar = Calendar(identifier: .gregorian)
gmtCalendar.timeZone = gmt
//Read time offset from FireStore
let offsetFromNYC = Calendar.current.offsetFromMidnight(for: Date())
//Calculate midnight in target calendar
let gmtMidnight = gmtCalendar.startOfDay(for: Date())
//Calculate the same time-of-day in the GMT time zone
let gmtTimeToday = Date(timeInterval: TimeInterval(offsetSinceMidnight), since: gmtMidnight)
print(gmtTimeToday)
Note that the above will give you the same hours/minutes/seconds as the offsetFromMidnight time.
Edit:
If your goal is to set an alarm to the next future time-of-day in the local time zone, you'd need to add logic to check if the computed date/time is in the past and adjust:
//Change adjustedChicagoTime to a var
var adjustedChicagoTime = Date(timeInterval: TimeInterval(offsetSinceMidnight), since: chicagoMidnight)
//If the alarm time is in the past, add a day to the date.
if adjustedChicagoTime < Date() {
adjustedChicagoTime = Calendar.current.date(byAdding: .day,
value: 1, to: adjustedChicagoTime, wrappingComponents: false)
}
Edit #2:
After a back-and-forth, it sounds like you sometimes want to save a date and time that's independent of time zone, like 9:30 AM on 10 July. If I create that date in EDT, and you view it in Melborne, it's ALWAYS 9:30 AM on 10 July.
Other times, you want to upload and download dates & times that honor time zones.
In order to easily do both, I would suggest saving 2 different string date/time fields to FireStore, one with a time zone, and one without. The one with timezone (or rather offset from GMT) would capture a moment in time around the world, and could be converted to a local time.
The one without time zone would describe a day/month/year/hours/minutes in local time.
You could generate/parse those strings in Swift using date formatters like this:
let baseFormatString = "YYYY-MM-dd'T'HH:mm"
let timeZoneFormatString = baseFormatString + "ZZZ"
let noTimeZoneFormatter = DateFormatter()
noTimeZoneFormatter.dateFormat = baseFormatString
let timeZoneFormatter = DateFormatter()
timeZoneFormatter.dateFormat = timeZoneFormatString
Note that by default a date formatter uses the system's time zone, so the "no time zone formatter" would assume the local time zone. If you use it to convert a date string to a date, it will assume the date is in the local time zone.
DateTime.fromMillisecondsSinceEpoch() method will return the DateTime since 1970-1-1 0:0:0.0 + milliseconds
Actually, if we pass 1 milliseconds, it will return 1970-1-1 8:0:0.1, this confused me.
Try passing isUtc: true. If you don't, the DateTime will be based on your local time zone. From the DateTime.fromMillisecondsSinceEpoch documentation:
If isUtc is false then the date is in the local time zone.
My question is very similar to Get "time with time zone" from "time without time zone" and the time zone name (I think). I just want to do it in Swift.
Anyway, I am trying to write a function with the following signature:
func combine(_ date: Date, with timeZone: TimeZone) -> Date?
What it does is that it basically takes in a date and returns a "zoned" date. If the date does not exist in the time zone, it returns nil.
To avoid being an XY question, here is a screenshot:
I'm asking the user for a date and a time zone and I want to combine these two into one single Date.
I'll try my best to explain. I will express dates in the format of timeIntervalFrom1970 to make it as clear as possible.
Say I pass in 0 as the date and GMT-1 as the time zone, it'll return 3600. 0 is 1970-1-1 00:00:00. 1970-1-1 00:00:00 in GMT-1 is 1970-1-1 01:00:00 in GMT, which is 3600.
This is how I tried to implement this:
return date.addingTimeInterval(-timeZone.secondsFromGMT(for: date))
This seems to work most but not all of the time. However, I don't think it returns the correct results if DST gets involved and the whole thing becomes messy. It also feels "math-ish". I would prefer an approach without math, using only the Foundation API methods.
So, How can I implement this method?
In your example, the API gives you a Date, but you want to interpret
that as "2017/08/18 8:08" in some given time zone. Assuming that
the eureka forms UI element uses the timezone of the current calendar
for display, you can convert the date to DateComponents, and back
to a Date with a different timezone.
func combine(_ date: Date, with timeZone: TimeZone) -> Date? {
var cal = Calendar.current
let comp = cal.dateComponents([.era, .year, .month, .day, .hour, .minute, .second], from: date)
cal.timeZone = timeZone
return cal.date(from: comp)
}
nil will be returned if the day/time combination does not exist
in the other timezone.
I am displaying the current time using this code:
let UTCDate = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd hh:mm a"
formatter.timeZone = TimeZone(identifier:"GMT")
let defaultTimeZoneStr = formatter.string(from: UTCDate)
Can someone help me to check if this time is in which timezone(eg:central timezone, eastern timezone)....
Unless the time is specified as a string with some kind of time zone indicator, such as "2017-04-14 10:00:00 EDT" or "2017-04-14 10:00:00 -0400", there's no way to tell what time zone for a given time value.
The Swift way to store times as Date values, which simply specify a number of seconds before or after January 1, 2001, UTC, and to display any time value using the calendar and time zone that makes the most sense for the user. Usually, this means using the time zone settings, because in most cases -- but not all cases -- that time zone setting will match the time zone where the user is.
If you want the abbreviated name of the user's current time zone setting, use this:
Calendar.current.timeZone.abbreviation()! // returns "EDT" for me;
// I’m in the eastern time zone
// and on daylight saving time
Or if you prefer getting the time zone by geographic identifier, use this:
Calendar.current.timeZone.identifier // returns "America/New_York" for me
Or if you want the full name of the time zone, try this (and play with the parameters):
// Returns "Eastern Standard Time" for me
Calendar.current.timeZone.localizedName(for: .standard, locale: Locale.current)
I'm currently parsing a time string and saving it to the db (Postgresql):
event.Time, _ := time.Parse("3:04 PM", "9:00 PM")
// value of event.Time now is: 0000-01-01 21:00:00 +0000 UTC
db.Create(&event)
It's giving me this error: pq: R:"DateTimeParseError" S:"ERROR" C:"22008" M:"date/time field value out of range: \"0000-01-01T21:00:00Z\"" F:"datetime.c" L:"3540"
event.Time's type is time.Time.
I also tried setting event.Time's type to string and using time data type in postgresql:
type Event struct {
Time string `gorm:"type:time
}
But now I'm getting an error when fetching records in the db:
sql: Scan error on column index 4: unsupported driver -> Scan pair: time.Time -> *string
Investigated this issue further. Currently, there's no support in GORM for any Date/Time types except timestamp with time zone
See this part of code from dialect_postgres.go:
case reflect.Struct:
if _, ok := dataValue.Interface().(time.Time); ok {
sqlType = "timestamp with time zone"
}
So basically I see two options for you:
Either use varchar(10) in DB, and string in Go, an simply save it as "9:00 PM" (where 10 is some number that suits you)
Or use timestamp with time zone in DB, time.Time in Go, and format your date part as a constant date, 01/01/1970, for example:
time.Parse("2006-01-02 3:04PM", "1970-01-01 9:00PM")
In that case you'll have to omit the date part in your presentation, but if you plan to select by date range, that could work better for you.
You can set an arbitrary database-specific type with Gorm using sql tag
type Event struct {
Time time.Time `sql:"type:timestamp without time zone"`
}
When updating the DATETIME field in SQL, the Go string must be in this format: time.Now().Format(time.RFC3339).
From Postgres perspective the error stems from there being no year 0000. If you don't the date you may just be able to add 1 year to the converted timestamp giving '0001-01-01T21:00:00+00' which is a valid Postgres timestamp.
select '0000-01-01T21:00:00+00'::timestamptz at time zone 'UTC'
ERROR: date/time field value out of range: "0000-01-01T21:00:00+00"
Gives he same error. And just as a demonstration 1 day before 0001-01-01 gives:
select '0001-01-01T21:00:00+00'::timestamptz at time zone 'UTC' - interval '1 day' "day_before_1/1/1";
--day_before_1/1/1
--0001-12-31 21:00:00 BC