zsh-users
 help / color / mirror / code / Atom feed
From: "TJ Luoma" <luomat@gmail.com>
To: "Zsh-Users List" <zsh-users@zsh.org>
Subject: best way to convert UTC to local time?
Date: Tue, 11 Jun 2013 17:51:54 -0400	[thread overview]
Message-ID: <83808ECC-34C4-42F6-B47B-70F26A6C03BD@gmail.com> (raw)


I am trying to write a script which will sort files into sub-folders 
based on the date that the file was _added_ to the current folder it is 
in.

This is what I have come up with:

	zmodload zsh/datetime

	for F in "$@"
	do

			# if the input IS NOT a file (i.e. is a link or directory), skip it
		[[ -f "$F" ]] || continue

			# see if the input from the user is readable, if not, go on to the 
next one
		[[ -r "$F" ]] || continue

			# look for Spotlight metadata for "Date Added" (kMDItemDateAdded)
			# This will be something like "2013-06-08 02:46:38 +0000"
		DATE_ADDED_UTC=$(mdls -raw -name kMDItemDateAdded "$F")

			# if we get '(null)' then the metadata doesn't exist. Which shouldn't 
happen. But might.
		[[ "$DATE_ADDED_UTC" == "(null)" ]] && continue

			# If we get here, we have the date added, but that date is based on 
UTC time,
			# not local time (currently EDT), so we need to convert it.
			
			# convert that timestamp to seconds-since-epoch
			# this will give us something like '1370677598'
		DATE_ADDED_UTC_EPOCH=$(TZ=UTC strftime -r "%Y-%m-%d %H:%M:%S +0000" 
"$DATE_ADDED_UTC")

			# convert that 'seconds-since-epoch' to a local time
			# 2013-06-08
		DATE_ADDED_LOCAL=$(strftime "%Y-%m-%d" "$DATE_ADDED_UTC_EPOCH")

		echo "The file $F was added on $DATE_ADDED_LOCAL"

	done

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.

TjL

p.s. - as far as I know there's no other way to get the "Date Added" 
info. If I'm wrong, please let me know :-)




             reply	other threads:[~2013-06-11 22:00 UTC|newest]

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

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=83808ECC-34C4-42F6-B47B-70F26A6C03BD@gmail.com \
    --to=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).