From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 28000 invoked by alias); 11 Jun 2013 22:00:01 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 17831 Received: (qmail 7477 invoked from network); 11 Jun 2013 21:59:53 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,DKIM_VALID,FREEMAIL_FROM,NML_ADSP_CUSTOM_MED,RCVD_IN_DNSWL_LOW autolearn=no version=3.3.2 Received-SPF: neutral (ns1.primenet.com.au: 66.111.4.27 is neither permitted nor denied by SPF record at _netblocks3.google.com) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=from:to:subject:date:message-id :mime-version:content-type; s=smtpout; bh=LnGiu8j1PO4tgKo+gMe9K6 8QV7E=; b=RoEu6gv1D1MVhAzaZazPr5DzmlH3FcE92vqnSh2WKo0a+A2GkGATqX uqdEahdyWQxiJUa8tLMRycOzEUFDFU4AIv73Ps2P1JaPsAVpnGiTgzyBiVIqpetx JNNkPqSY03KHUBadCt4bM0A7JVFIs26VMNBK3nsN5cllmvKhKMwBA= X-Sasl-enc: eyrbJEhNrmDcIGC1Zk07yIuY4Nd1p3BdaTyC3/ojNg5o 1370987517 From: "TJ Luoma" To: "Zsh-Users List" Subject: best way to convert UTC to local time? Date: Tue, 11 Jun 2013 17:51:54 -0400 Message-ID: <83808ECC-34C4-42F6-B47B-70F26A6C03BD@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; format=flowed X-Mailer: MailMate (1.5.4r3323) 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 :-)