zsh-users
 help / color / mirror / code / Atom feed
* Convert UTC time to local time using strftime and zsh/datetime
@ 2015-02-21  4:41 TJ Luoma
  2015-02-21  5:02 ` Kurtis Rader
  0 siblings, 1 reply; 3+ messages in thread
From: TJ Luoma @ 2015-02-21  4:41 UTC (permalink / raw)
  To: Zsh-Users List

[-- Attachment #1: Type: text/plain, Size: 1325 bytes --]

I have a screenshot of my iPhone which shows that it was taken at 11:42 a.m.

When I look at the file in Finder, it shows that it was taken at 11:42 a.m.
on Feb 11

You can see a screenshot here:
http://images.luo.ma/skitch/iPhonePhotos-1142am-20150220-231455.jpg

When I use `mdls -raw -name kMDItemContentCreationDate test.png` (a Mac
command to get the Creation Date), I get

2015-02-11 16:42:30 +0000

as the answer. This is, I believe, UTC. So I need to convert that to local
(US/Eastern) time.

I'm not sure how to do that, so heres what I did

1) Load the appropriate module

> zmodload zsh/datetime

2) get the Unix EPOCH time from the date/time stamp we have:

> strftime -r "%Y-%m-%d %H:%M:%S +0000" "2015-02-11 16:42:30 +0000"
1423690950

3) Convert '1423690950' to US/Eastern time:

> strftime "%Y-%m-%d %H:%M:%S" "1423690950"
2015-02-11 16:42:30

But that's still giving me '16' as the hour. It should be 11, shouldn't it?

4) Try again, this time making sure that I define the time zone:

> TZ=US/Eastern strftime "%Y-%m-%d %H:%M:%S" "1423690950"
2015-02-11 16:42:30

Same time.

OK, so I'm obviously missing something. I checked '1423690950' on various
online "Unix time" converters and they confirm that it's 16:42 my time, but
since I know the picture was taken at 11:42, I'm wondering what I did wrong.

Tj

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Convert UTC time to local time using strftime and zsh/datetime
  2015-02-21  4:41 Convert UTC time to local time using strftime and zsh/datetime TJ Luoma
@ 2015-02-21  5:02 ` Kurtis Rader
  2015-02-21 18:23   ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Kurtis Rader @ 2015-02-21  5:02 UTC (permalink / raw)
  To: TJ Luoma; +Cc: Zsh-Users List

[-- Attachment #1: Type: text/plain, Size: 2354 bytes --]

If you believe the formatted date/time string is in the UTC timezone you
need to

TZ=UTC strftime -r "%Y-%m-%d %H:%M:%S +0000" "2015-02-11 16:42:30 +0000"

Which should yield 1423672950 as the seconds since the UNIX epoch.

I use the following Python program to convert seconds since the UNIX epoch
to UTC and local time. For example (note that I'm on the west coast of the
USA, UTC-8):

$ time2str 1423672950
UTC   2015-02-11 16:42:30
Local 2015-02-11 08:42:30



#!/usr/bin/env python2.7
from __future__ import print_function

import sys
import time

TIME_FMT = '%Y-%m-%d %H:%M:%S'

seconds_epoch = float(sys.argv[1])
utc = time.gmtime(seconds_epoch)
local = time.localtime(seconds_epoch)

print('UTC   {}'.format(time.strftime(TIME_FMT, utc)))
print('Local {}'.format(time.strftime(TIME_FMT, local)))



On Fri, Feb 20, 2015 at 8:41 PM, TJ Luoma <luomat@gmail.com> wrote:

> I have a screenshot of my iPhone which shows that it was taken at 11:42
> a.m.
>
> When I look at the file in Finder, it shows that it was taken at 11:42 a.m.
> on Feb 11
>
> You can see a screenshot here:
> http://images.luo.ma/skitch/iPhonePhotos-1142am-20150220-231455.jpg
>
> When I use `mdls -raw -name kMDItemContentCreationDate test.png` (a Mac
> command to get the Creation Date), I get
>
> 2015-02-11 16:42:30 +0000
>
> as the answer. This is, I believe, UTC. So I need to convert that to local
> (US/Eastern) time.
>
> I'm not sure how to do that, so heres what I did
>
> 1) Load the appropriate module
>
> > zmodload zsh/datetime
>
> 2) get the Unix EPOCH time from the date/time stamp we have:
>
> > strftime -r "%Y-%m-%d %H:%M:%S +0000" "2015-02-11 16:42:30 +0000"
> 1423690950
>
> 3) Convert '1423690950' to US/Eastern time:
>
> > strftime "%Y-%m-%d %H:%M:%S" "1423690950"
> 2015-02-11 16:42:30
>
> But that's still giving me '16' as the hour. It should be 11, shouldn't it?
>
> 4) Try again, this time making sure that I define the time zone:
>
> > TZ=US/Eastern strftime "%Y-%m-%d %H:%M:%S" "1423690950"
> 2015-02-11 16:42:30
>
> Same time.
>
> OK, so I'm obviously missing something. I checked '1423690950' on various
> online "Unix time" converters and they confirm that it's 16:42 my time, but
> since I know the picture was taken at 11:42, I'm wondering what I did
> wrong.
>
> Tj
>



-- 
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Convert UTC time to local time using strftime and zsh/datetime
  2015-02-21  5:02 ` Kurtis Rader
@ 2015-02-21 18:23   ` Bart Schaefer
  0 siblings, 0 replies; 3+ messages in thread
From: Bart Schaefer @ 2015-02-21 18:23 UTC (permalink / raw)
  To: Zsh-Users List

On Feb 20,  9:02pm, Kurtis Rader wrote:
}
} TZ=UTC strftime -r "%Y-%m-%d %H:%M:%S +0000" "2015-02-11 16:42:30 +0000"
} 
} Which should yield 1423672950 as the seconds since the UNIX epoch.

Except it doesn't unless you have already done "export TZ", because
strftime is not one of the special builtins that really do alter the
process environment when you prefix them with a variable assignment.
So in my environment where TZ starts out not set, this still returns
the epoch in my local time zone, e.g. 1423701750.

This is guaranteed to fix the timezone issue:

    (){ local -x TZ=UTC;
	strftime -r "%Y-%m-%d %H:%M:%S %z" "2015-02-11 16:42:30 +0000" }

but note that %z to match the timezone is a glibc extension to strptime
and may not work everywhere.

Also note that strptime is not defined to perform timezone conversions;
even though glibc strptime can be told that a timezone is present, it
parses it and then throws it away.  In TJ's original formulation

} > > strftime -r "%Y-%m-%d %H:%M:%S +0000" "2015-02-11 16:42:30 +0000"

the +0000 is just a string which is matched but ignored.  Either way,
this is why the TZ=UTC environment setting is needed.

The next question is whether the strftime builtin should implicitly
perform "local -x TZ" to make Kurtis' example do the expected thing.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-02-21 18:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-02-21  4:41 Convert UTC time to local time using strftime and zsh/datetime TJ Luoma
2015-02-21  5:02 ` Kurtis Rader
2015-02-21 18:23   ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).