zsh-users
 help / color / mirror / code / Atom feed
* best way to convert UTC to local time?
@ 2013-06-11 21:51 TJ Luoma
  2013-06-12  2:01 ` Lawrence Velázquez
  2013-06-12  4:42 ` Bart Schaefer
  0 siblings, 2 replies; 3+ messages in thread
From: TJ Luoma @ 2013-06-11 21:51 UTC (permalink / raw)
  To: Zsh-Users List


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 :-)




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

* Re: best way to convert UTC to local time?
  2013-06-11 21:51 best way to convert UTC to local time? TJ Luoma
@ 2013-06-12  2:01 ` Lawrence Velázquez
  2013-06-12  4:42 ` Bart Schaefer
  1 sibling, 0 replies; 3+ messages in thread
From: Lawrence Velázquez @ 2013-06-12  2:01 UTC (permalink / raw)
  To: TJ Luoma; +Cc: zsh-users

On Jun 11, 2013, at 5:51 PM, TJ Luoma <luomat@gmail.com> wrote:

> 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.

Well, if you're willing to use an external command, you could go with date(1):

    for F in "$@"; do
        [[ -f "$F" && -r "$F"]] || continue
        DATE_ADDED_UTC=$(mdls -raw -name kMDItemDateAdded "$F")
        [[ "$DATE_ADDED_UTC" != "(null)" ]] || continue
        DATE_ADDED_LOCAL=$(date -jf '%F %T %z' "$DATE_ADDED_UTC" '+%F')
        echo "The file $F was added on $DATE_ADDED_LOCAL"
    done

vq

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

* Re: best way to convert UTC to local time?
  2013-06-11 21:51 best way to convert UTC to local time? TJ Luoma
  2013-06-12  2:01 ` Lawrence Velázquez
@ 2013-06-12  4:42 ` Bart Schaefer
  1 sibling, 0 replies; 3+ messages in thread
From: Bart Schaefer @ 2013-06-12  4:42 UTC (permalink / raw)
  To: TJ Luoma, Zsh-Users List

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.


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

end of thread, other threads:[~2013-06-12  4:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-06-11 21:51 best way to convert UTC to local time? TJ Luoma
2013-06-12  2:01 ` Lawrence Velázquez
2013-06-12  4:42 ` 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).