Please I Need a Function to Convert a UTC DateTime to GMT Format.
This Function Should Receive the UTC DateTime as a TDateTime and Return The GMT Time as a String.
Google Does Not Seem to Help Much.
Here is an Illustration
Current Value in UTC DateTime := 7/9/2014 11:36:51 PM
Needed Value in GMT Time := 2014-07-09 23:36:51
Delphi Version : XE5
Thanks.
Use FormatDateTime for this.
The documentation describes the format string options in detail. The format string that you need is:
yyyy-mm-dd hh:nn:ss
Related
I am trying to convert an ISO String to PST time. For some reason, it is converting it to my local timzone instead of to PST. The PST for this string below is "7:40 PM" for reference. Here is the code I am running:
var now = moment('2018-07-09T02:40:38.818Z').tz('America/Los_Angeles').format('MM-DD-YYYY h:mm:ss a');
alert(now);
JS Fiddle w/ Moment installed:
https://jsfiddle.net/john23/5gbv2csj/5/
Consider this example:
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println(time.Parse(time.RFC3339, time.RFC3339))
}
The output is:
0001-01-01 00:00:00 +0000 UTC parsing time "2006-01-02T15:04:05Z07:00": extra text: 07:00
Why can't time.Parse() handle a layout as a value? What's missing here?
UPDATE: Cutting off the time zone value (but not the 'Z' delimiting the time from the zone) fixes it:
fmt.Println(time.Parse(time.RFC3339, "2015-09-15T11:50:00Z"))
Why can't time.Parse() handle time zone info when using time.RFC3339 as the layout string?
http://play.golang.org/p/p3fHfJNHVK
UPDATE: JimB's answer led me to read from RFC3339 and I found these examples that clarify further:
Here are some examples of Internet date/time format.
1985-04-12T23:20:50.52Z
This represents 20 minutes and 50.52 seconds after the 23rd hour of
April 12th, 1985 in UTC.
1996-12-19T16:39:57-08:00
This represents 39 minutes and 57 seconds after the 16th hour of
December 19th, 1996 with an offset of -08:00 from UTC (Pacific
Standard Time). Note that this is equivalent to 1996-12-20T00:39:57Z
in UTC.
The time.RFC3339 format is a case where the format string itself isn't a valid time. You can't have a Z and an offset in the time string, but the format string has both because the spec can contain either type of timezone specification.
Both of these are valid RFC3339 times:
"2015-09-15T14:00:12-00:00"
"2015-09-15T14:00:13Z"
And the time package needs to be able to parse them both using the same RFC3339 format string.
As noted, 2006-01-02T15:04:05Z07:00 is an invalid IETF RFC-3339 time format. Here's an explanation.
The reason you cannot have both Z and an offset is that they are both ways to represent a time offset. Z is equivalent to +00:00 indicating a zero hour/minute offset, or no offset. You cannot say both +00:00 offset and +07:00 offset in the same time representation.
The following is the Z definition in the RFC-3339 Section 2:
https://www.rfc-editor.org/rfc/rfc3339#section-2
Z A suffix which, when applied to a time, denotes a UTC
offset of 00:00; often spoken "Zulu" from the ICAO
phonetic alphabet representation of the letter "Z".
Of note, while Z is equivalent to +00:00, they are both different from -00:00 which indicates known UTC time with an unknown offset, as described in RFC-3339 Section 4.3:
https://www.rfc-editor.org/rfc/rfc3339#section-4.3
4.3. Unknown Local Offset Convention
If the time in UTC is known, but the offset to local time is unknown,
this can be represented with an offset of "-00:00". This differs
semantically from an offset of "Z" or "+00:00", which imply that UTC
is the preferred reference point for the specified time. RFC2822
[IMAIL-UPDATE] describes a similar convention for email.
When UTC date is formatted by DateFormat#format(), I expect to get a String of UTC date formatted, but not.
Please look at the code below. These tests are passed.
My question is why does DateFormat#format() return a time of 12:00 past UTC date? What am I missing here?
Date date(int millisecondsSinceEpoch, bool isUtc) =>
new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, isUtc:isUtc);
DateFormat df = new DateFormat("yyyy-MM-dd hh:mm");
// JST (+9.00)
expect(date(0, false).toString(), equals("1970-01-01 09:00:00.000"));
expect(df.format(date(0, false)), equals("1970-01-01 09:00"));
expect(date(0, true).toString(), equals("1970-01-01 00:00:00.000Z"));
// Why 12 o'clock?
expect(df.format(date(0, true)), equals("1970-01-01 12:00"));
Your format string you've written uses lowercase hh which is a 12 hour clock. Use uppercase HH for the hours to get a 24-hour clock. The output you are getting in the last line is currently saying 12 midnight, rather than 00 hours in a 24-hour clock.
See the explicit pattern syntax in DateFormat
Code:
DateTimeFormat dateFormat = DateTimeFormat
.getFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date date = dateFormat.parse("Wed May 30 18:33:22 PDT 2012");
Result:
Uncaught java.lang.IllegalArgumentException: Wed May 30 18:33:22 PDT 2012
Why?
I've checked and doublechecked my pattern against the docs. Plugging the pattern into a SimpleDateFormat tester gives the expected results as well, although that's obviously not a conclusive test.
Parsing of the date has limited support as per the docs you mentioned:
The time zone support for parsing is limited. Only standard GMT and RFC format are supported. Time zone specification using time zone id (like America/Los_Angeles), time zone names (like PST, Pacific Standard Time) are not supported.
More specifically
In the current implementation, timezone parsing only supports
GMT:hhmm, GMT:+hhmm, and GMT:-hhmm.
So, the following code works fine:
DateTimeFormat dateFormat = DateTimeFormat
.getFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date date = dateFormat.parse("Thu May 31 09:45:21 GMT-07:00 2012");
GWT.log("Date "+date);
How can I parse a time string, such as Sun May 27 13:02:04 +0200 2012, to a tuple in UTC, using python3? The closest I can get is time.strptime(str, '%a %b %d %H:%M:%S %z %Y'), but it seems the offset is parsed but ignored. The above example is parsed to:
time.struct_time(tm_year=2012, tm_mon=5, tm_mday=27, tm_hour=13, tm_min=1, tm_sec=35, tm_wday=6, tm_yday=148, tm_isdst=-1)
but I expected tm_hour=11.
Related: Convert string timestamp (with timezone offset) to local time. . ? python, Parsing time string in Python. Answers to both suggests the use of dateutil, but I'd prefer using the standard library, if possible.
tm_hour=11 would be incorrect. The time in the timestamp is 13:02:04, no matter what the offset is.
What you want to do is to convert that timestamp into a time in GMT, which the standard library won't do for you. So you will just have to extract the offset yourself (which is trivial) and then subtract it from the time.
I'd also recommend you to use the datetime library for this, date/time manipulation is much easier there, you can easily create a timedelta object from the offset and subtract that from the datetime object.