Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: best way to convert UTC to local time?



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.



Messages sorted by: Reverse Date, Date, Thread, Author