处理时间和日期的更简单方法? [英] Simpler way to deal with Time and Date?

查看:109
本文介绍了处理时间和日期的更简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试学习Haskell,因此,如果已经提出此要求,我们感到很抱歉,但是我找不到能正确回答我的疑问的问题.

So I am trying to learn Haskell, so I am sorry if this has already been asked, but I could not find a question that properly answered my doubts.

所以给我的问题是UTCTime什么是获得以下内容的最简单直接的方法:

So my question is given UTCTime what is the easiest and straightforward way of getting the following:

  • 年份
  • 日期(例如:24)
  • 日期(例如:星期一)
  • 小时
  • 分钟
  • 第二

还有一种简便的方法,可以仅使用Asia/Tokyo或类似的字符串将UTCTime转换为本地时间.

Also if there is convenient way of converting UTCTime to some local time using just Asia/Tokyo or similar strings.

我找不到提供所有这些功能的单个库,并且感到困惑,很坦白地说,我需要在不同类型之间进行转换并使用多个库来实现上述目标.也许我看起来不太正常.

I couldn't find a single library that gave all of these features and got confused and to be quite honest irritated that I need to convert between different types and use multiple libraries to achieve the above. Maybe I was not looking properly.

到目前为止,我的努力:

My effort so far:

import Data.Time

-- Difference between JST and UTC in seconds
-- JST is 9 hours ahead of UTC
utcJSTDiff :: Int
utcJSTDiff = 32400

getHour :: UTCTime -> Int
getHour time = todHour (timeToTimeOfDay (utctDayTime time))

getMinutes :: UTCTime -> Int
getMinutes time = todMin (timeToTimeOfDay (utctDayTime time))

isReminderHour :: Int -> Bool
isReminderHour hour
    | hour == morningReminderHour || hour == eveningReminderHour = True
    | otherwise = False

isReminderMins :: Int -> Bool
isReminderMins mins
    | mins <= reminderMinuteLimit = True
    | otherwise = False

timeforReminder :: UTCTime -> Bool
timeforReminder time
    | isReminderHour (getHour time) && isReminderMins (getMinutes time) = True
    | otherwise = False

utcToJST :: UTCTime -> UTCTime
utcToJST = addUTCTime (realToFrac utcJSTDiff)

main :: IO()
main = do
    currentTime <- getCurrentTime
    let jstTime = utcToJST currentTime
    if timeforReminder jstTime
        then action1
        else action2

推荐答案

固定为使用timezone-olson/timezone-series进行正确的时区处理.

Fixed to use timezone-olson / timezone-series for correct timezone processing.

您应该能够使用time软件包执行几乎所有操作,只是在Linux系统上正确的时区处理将需要timezone-olsontimezone-series软件包;我不确定如何在Windows系统上执行此操作.也许其他人有更好的方法.

You should be able to do almost everything with the time package, except that proper timezone processing will require timezone-olson and timezone-series packages on Linux systems; I'm not sure how to do it on Windows systems. Maybe someone else has a better way.

代码如下:

module Time where

import Data.Time                             -- package "time"
import Data.Time.Calendar.WeekDate           -- package "time"
import Data.Time.LocalTime.TimeZone.Olson    -- package "timezone-olson"
import Data.Time.LocalTime.TimeZone.Series   -- package "timezone-series"

-- |POSIX-specific timezone lookup
lookupTimeZone :: String -> IO TimeZoneSeries
lookupTimeZone tz =
  getTimeZoneSeriesFromOlsonFile ("/usr/share/zoneinfo/" ++ tz)

data MyTime = MyTime
  { year :: Integer
  , month :: Int
  , week :: Int
  , dayOfMonth :: Int
  , dayOfWeek :: Int
  , hour :: Int
  , minutes :: Int
  , seconds :: Int
  } deriving (Show)

getMyTime :: TimeZoneSeries -> UTCTime -> MyTime
getMyTime tz t =
  let LocalTime day (TimeOfDay hh mm ss) = utcToLocalTime' tz t
      (yr, mn, dom) = toGregorian day
      (_,  wk, dow) = toWeekDate day
  in MyTime yr mn wk dom dow hh mm (round ss)

main :: IO ()
main = do
  currentTime <- getCurrentTime
  -- Note: if you want time in local timezone, use:
  -- tz <- getTimeZoneSeriesFromOlsonFile "/etc/localtime"
  tz <- lookupTimeZone "Asia/Tokyo"
  print $ getMyTime tz currentTime

一些注意事项:

  • lookupTimeZone如果在/usr/share/zoneinfo/your/timezone
  • 找不到时区文件,将引发异常.
  • 如果用周"表示一年中的某周",则可能要查阅toWeekDate的文档,以准确了解其定义.
  • 如果要以字符串格式表示月份或天数,则可以使用Data.Time.Format中的函数(仍在time包中)以当前(或某些其他)语言环境格式化它们.
  • lookupTimeZone will throw an exception if it can't find the timezone file at /usr/share/zoneinfo/your/timezone
  • If by "week", you meant "week of the year", you may want to consult the documentation for toWeekDate to see exactly how that's defined.
  • If you want months or days in string format, you can use the functions in Data.Time.Format (still in the time package) to format them in the current (or some other) locale.

这篇关于处理时间和日期的更简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆