zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: "TJ Luoma" <luomat@gmail.com>, "Zsh-Users List" <zsh-users@zsh.org>
Subject: Re: best way to convert UTC to local time?
Date: Tue, 11 Jun 2013 21:42:02 -0700	[thread overview]
Message-ID: <130611214202.ZM6862@torch.brasslantern.com> (raw)
In-Reply-To: <83808ECC-34C4-42F6-B47B-70F26A6C03BD@gmail.com>

On Jun 11,  5:51pm, TJ Luoma wrote:
} 
} This is what I have come up with:
} 
} zmodload zsh/datetime
} 
} DATE_ADDED_UTC=$(mdls -raw -name kMDItemDateAdded "$F")
} DATE_ADDED_UTC_EPOCH=$(TZ=UTC strftime -r "%Y-%m-%d %H:%M:%S +0000"
}  "$DATE_ADDED_UTC")
} DATE_ADDED_LOCAL=$(strftime "%Y-%m-%d" "$DATE_ADDED_UTC_EPOCH")
} 
} This works, but three separate `strftime` calls seems inefficient. I 
} mean, it's not like it takes a long time to run or anything, I'm just 
} wondering if there's a "better" way.

I count only two strftime calls?

Without writing something like mdls but that returns the field as an
epoch timestamp, anything you use including the "date" command Larry
suggested is going to do the same sort of parse/convert that you're
doing with strftime here, so I'm just going to mention that you can
do the strftime calls themselves "better".

If you use the -s SCALAR option to strftime, you can skip the command
substitution and its inherent fork and I/O:

TZ=UTC strftime -r -s DATE_ADDED_UTC_EPOCH "%Y-%m-%d %H:%M:%S +0000" \
	"$DATE_ADDED_UTC"
strftime -s DATE_ADDED_LOCAL "%Y-%m-%d" "$DATE_ADDED_UTC_EPOCH"

You could cut out another step by using mdls -nullMarker to assign a
fixed date to files that don't have one, instead of testing "(null)"
to skip those files:

TZ=UTC strftime -r -s DATE_ADDED_UTC_EPOCH "%Y-%m-%d %H:%M:%S +0000" \
	$(mdls -raw -name kMDItemDateAdded \
	       -nullMarker "2013-06-11 00:00:00 +0000" "$F")

Also you could do [[ -f $F && -r $F ]] in a single condition, but that's
not going to make much difference unless you're processing a really
large number of files.


      parent reply	other threads:[~2013-06-12  4:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-11 21:51 TJ Luoma
2013-06-12  2:01 ` Lawrence Velázquez
2013-06-12  4:42 ` Bart Schaefer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=130611214202.ZM6862@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=luomat@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).